Day - 96 of DevOps

Day - 96 of DevOps

Docker Run Command - A step-by-step explanation

Docker Run:

Docker containers are a popular way to run applications in a lightweight, isolated environment. The docker run command is the most common way to create and start Docker containers.

The docker run command is a combination of two commands: docker create and docker start. The docker create command creates a new container from an image, but it does not start the container. The docker start command starts a container that has already been created.

The docker run command combines the functionality of the docker create and docker start commands into a single command. This makes it easier to create and start containers, and it also makes it easier to run applications in containers.

docker run = docker create + docker start

In this article, we will explain the docker run command step-by-step.

Step 1: Check if the image exists locally

The first step that Docker takes when you run the docker run command is to check if the image you specified exists on your local system. If it does not, Docker will pull the image from the Docker Hub registry.

To pull an image, Docker sends a request to the Docker Hub registry and downloads the image file to your local system. Once the image file is downloaded, Docker stores it in a local repository.

You can check if an image exists on your local system by running the docker images command. This command will list all of the images that are currently stored on your local system.

Step 2: Create a new container from the image

Once Docker has verified that the image exists locally, it will create a new container from the image. . This is the first command that docker run combines: docker create.

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

When Docker creates a new container, it allocates some resources from the host system, such as CPU, memory, and storage. Docker also creates a network namespace for the container. This network namespace isolates the container’s network traffic from the network traffic of other containers and the host system.

Step 3: Start the container and run the command

Once the container is created, Docker will start the container and run the command that you specified. This is the second command that docker run combines: docker start.

When Docker starts a container, it executes the entrypoint or command that is specified in the Dockerfile. The entrypoint or command is typically a shell script or an executable program.

Step 4: Attach to the container and print the output to the console

Docker will attach to the container and print the output of the command to the console.

Once the command has finished running, Docker will stop the container and remove it.

So, the docker run command is a combination of the docker create and docker start commands. It allows you to create and start a container with a single command.

Example

Here is an example of a docker run command:

docker run -it ubuntu bash

This command will run a bash shell in an Ubuntu container. The -it options tell Docker to run the container in interactive mode and to attach to the container's stdin and stdout.

When you run this command, you will see a bash prompt in your terminal window. You can then type commands in the terminal window and they will be executed in the Ubuntu container.

To exit the container, type exit and press Enter.

Simple flow.

Start
-> Check if the image exists locally
-> Yes: Create a new container from the image
-> No: Pull the image from the Docker Hub registry
-> Create a new container from the image
-> Start the container and run the command
-> Attach to the container and print the output to the console
-> Stop the container and remove it
End

Its important to note that It is not possible to change the command that you passed initially when you created the container. However, you can create a new container with a different command and then start the new container.

To create a new container with a different command, you can use the docker run command. The docker run command creates a new container from an image and starts it.

Conclusion

The docker run command is a powerful tool that allows you to create and start Docker containers. By understanding the steps that Docker takes when you run the docker run command, you can be more effective in using Docker to run your applications.