No history yet

Docker Basics

What is Docker?

Think about a shipping container. It's a standard box that can hold almost anything, from bananas to car parts. Because it's standardized, it can be loaded onto any ship, train, or truck without any special adjustments. Docker does the same thing, but for software.

Docker solves the classic developer problem: "It works on my machine." An application might work perfectly for the person who built it, but break when they share it with a teammate or deploy it to a server. This usually happens because of tiny differences in their computer environments, like different operating system versions or software libraries.

Lesson image

Docker packages an application and all its dependencies—libraries, system tools, code, and runtime—into a single, isolated unit called a container. This container is a standardized package that can run consistently on any machine that has Docker installed. It ensures that the application will always run the same, regardless of where it's deployed.

Container

noun

A lightweight, standalone, executable package of software that includes everything needed to run an application: code, runtime, system tools, system libraries, and settings.

Docker's Architecture

Docker uses a client-server architecture. The Docker client is the command-line tool you use to issue commands. The Docker daemon (or server) runs on your machine and does the heavy lifting: building, running, and distributing your containers.

When you type a command like docker run, the client sends this command to the daemon. The daemon then takes care of executing it.

Two other core components are images and registries.

  • Image: A read-only template used to create containers. Think of it as a recipe or a blueprint. Images are often based on other images, with some additional customization. For example, you might start with an Ubuntu image, then add your application and its dependencies to create a new image.
  • Registry: A place to store and distribute images. Docker Hub is the default public registry, where you can find thousands of official and community-built images. When you ask Docker to run an image it doesn't have, it automatically pulls it from Docker Hub.

Getting Started with Docker

First, you need to install Docker. The easiest way is to download Docker Desktop, which is available for Mac, Windows, and Linux. It provides a graphical user interface and includes everything you need.

You can find the official installation instructions on the Docker website.

Once Docker is installed and running, open your terminal or command prompt. Let's run your first container. Type the following command and press Enter:

docker run hello-world

You should see a message that starts with "Hello from Docker!" This confirms that your installation is working correctly.

Here’s what just happened:

  1. The Docker client contacted the Docker daemon.
  2. The daemon searched for a local image named hello-world.
  3. Since it couldn't find one, it pulled the image from the Docker Hub registry.
  4. The daemon created a new container from that image.
  5. The container ran its program, which printed the message, and then it exited.

Managing Images and Containers

You'll often need to see what images and containers you have on your system. These two commands are essential.

To list all the images you have downloaded locally, use:

docker images

You'll see the hello-world image in the list, along with details like its tag (version) and size.

To list the containers on your machine, use:

docker ps

This command shows currently running containers. Since the hello-world container exited as soon as it finished its job, you won't see it. To see all containers, including stopped ones, add the -a flag:

docker ps -a

Now you'll see your hello-world container with a status of "Exited."

Let's try a more useful container. This command will download a lightweight version of the Nginx web server and run it.

docker run --name my-web-server -p 8080:80 -d nginx

Let's break that down:

  • docker run: The command to run a container.
  • --name my-web-server: Gives the container a memorable name.
  • -p 8080:80: This maps port 8080 on your machine to port 80 inside the container. Nginx listens on port 80 by default.
  • -d: Runs the container in detached mode, meaning it runs in the background.
  • nginx: The name of the image to use.

Now, if you open a web browser and go to http://localhost:8080, you will see the Nginx welcome page. Your web server is running inside a completely isolated container.

To stop and remove the container, first use docker stop with the container's name, then docker rm.

# Stop the running container
docker stop my-web-server

# Remove the stopped container
docker rm my-web-server

To remove an image you no longer need, use docker rmi followed by the image name.

docker rmi nginx
Quiz Questions 1/5

What is the primary problem Docker is designed to solve?

Quiz Questions 2/5

In the Docker ecosystem, what is the relationship between an image and a container?

This is just the start. You've learned how to install Docker, understand its basic architecture, and run, manage, and remove containers and images. These are the fundamental building blocks for using Docker in any project.