The difference between run, start and create commands in docker

Posted by pspfreak101 on Thu, 02 Dec 2021 05:34:47 +0100

For beginners of Docker, the terms Docker start, docker run and docker create may be confusing. This article explains the differences through examples.

catalogue

  1. The difference between Docker running, Docker starting and Docker creation

If you are not familiar with docker and learn it through various tutorials, you may encounter terms such as starting a docker container, running a docker container, or creating a docker container.

These terms are enough to confuse beginners of docker because all three docker commands look similar.

In fact, it is particularly difficult to distinguish between docker run and docker start.

Isn't the running container the same as the starting container? No,

Let me explain to you.

The difference between Docker running, Docker starting and Docker creation

The following are the purposes of these commands:

The Docker create command creates a new container from the Docker image. However, it does not run it immediately.

The docker start command will start any stopped containers. If you use the docker create command to create a container, you can use this command to start it.

The Docker run command is a combination of creation and startup because it creates a new container and starts it immediately. In fact, if Docker run command The above image cannot be found on your system. It can even extract the image from the Docker Hub.

Let's look at it with examples so that you can know things more clearly.

Let's look at it through an example

If you want to follow the example, make sure that you have Install Docker.

Suppose you use the Docker pull ubuntu command to download an Ubuntu image from the Docker Hub.

You can view all available Docker images on the system. In this example, I only have ubuntu (to avoid confusion):

abhishek@itsfoss:~$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              775349758637        5 weeks ago         64.2MB

Now, use the docker create command to create a new container named container-1 docker Container:

abhishek@itsfoss:~$ docker create --name container-1 ubuntu
2d9a8c190e6c9b3cbbc032a87762bfbc92f1dc0dd30abbe9bdb3ed7e74a6480f

You can see that it has created a new container. If you try to see all the running containers, you won't see container-1 because although it was created, it never started.

abhishek@itsfoss:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

If you examine all containers, whether they are running or not, you will see that container-1 has a "created" state:

abhishek@itsfoss:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
2d9a8c190e6c        ubuntu              "/bin/bash"         18 seconds ago      Created                                 container-1

Now, let's use docker run command To create and run a container named container-2:

abhishek@itsfoss:~$ docker run -it -d --name container-2 ubuntu bash
13dc0f4226dc8d9d86e41d927c5616654d8263da2cc8c667aaa5b4dbd7f7e9b3

You can see that container-2 is running because its status is started:

abhishek@itsfoss:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED              STATUS              PORTS               NAMES
13dc0f4226dc        ubuntu              "bash"              About a minute ago   Up About a minute                       container-2

let's Stop this running container:

abhishek@itsfoss:~$ docker stop container-2
container-2
abhishek@itsfoss:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
abhishek@itsfoss:~$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
13dc0f4226dc        ubuntu              "bash"              2 minutes ago       Exited (0) 28 seconds ago                       container-2
2d9a8c190e6c        ubuntu              "/bin/bash"         3 minutes ago       Created                                         container-1

Now we have a stop container that you can use The docker start command starts again It:

abhishek@itsfoss:~$ docker start container-2
container-2
abhishek@itsfoss:~$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
13dc0f4226dc        ubuntu              "bash"              2 minutes ago       Up 2 seconds                            container-2

But what happens to container-1 created with the docker create command? You can start this container using the docker start command, and then use the docker exec Run something specific.

I hope this article will give you a better understanding of docker running, docker starting, and docker creation commands. I suggest you understand Container lifecycle To learn more about this topic.

Topics: PHP Python Java