DEV Community

Wycliffe A. Onyango
Wycliffe A. Onyango

Posted on

100 Days of DevOps: Day 36

Starting Your First Docker Container

Starting a Docker container is a fundamental skill for anyone working with containerization. While the docker run command may seem simple, it orchestrates a series of powerful actions to bring an application to life. Let's explore the process of starting a container using an exercise.

The Exercise: Starting an Nginx Container

The goal of the exercise is to create and run a container named nginx_1 using the nginx:alpine image. The command to use is:

docker run --name nginx_1 -d nginx:alpine
Enter fullscreen mode Exit fullscreen mode

This single command triggers a multi-step process.

Step 1: The Request

The docker run command tells the Docker daemon to handle everything needed to start the container. It specifies:

  • --name nginx_1: The name you want to give the container for easy identification.
  • -d: The detached flag, which runs the container in the background and prints its ID.
  • nginx:alpine: The image and tag to use. The nginx part is the image name, and :alpine is the tag, which specifies a lightweight version of the image.

Step 2: Image Discovery and Pull

Docker first checks if the nginx:alpine image already exists on your local machine. For my case, the image was not found locally, as indicated by the message Unable to find image 'nginx:alpine' locally.

This triggers the image pull process. Docker connects to Docker Hub (the default public registry) and begins downloading the image layers. You will see the Pull complete messages for each layer of the image. Once all layers are downloaded, Docker has all the necessary components to create the container.

Step 3: Container Creation and Execution

After the image is pulled, Docker uses it as a blueprint to create a new container instance. The container is given a unique ID (e.g., b2fecdf49557...) and configured with the specified name and run mode. Since the -d flag was used, Docker immediately starts the container's main process in the background. The printed ID confirms that the creation and start processes were successful.

Step 4: Verification

The final step is to verify that the container is in a running state. The docker ps command lists all running containers.

docker ps
Enter fullscreen mode Exit fullscreen mode

The output showed:

CONTAINER ID       IMAGE          COMMAND                  CREATED         STATUS          PORTS    NAMES
b2fecdf49557       nginx:alpine   "/docker-entrypoint.…"   16 seconds ago  Up 14 seconds   80/tcp   nginx_1
Enter fullscreen mode Exit fullscreen mode

The STATUS column clearly showed Up 14 seconds, confirming that the nginx_1 container was successfully created and is now running. This completes the entire process, from a simple command to a fully operational container.

Top comments (0)