Docker run vs create
DockerDocker Problem Overview
What is the difference between docker run
and docker create
commands?
I usually use run
but sometimes in documentation I see create
.
Docker's --help
tells
create Create a new container
run Run a command in a new container
Does it mean that run
is used when we need to pass a command to a new container? What's the aim of create
then?
Docker Solutions
Solution 1 - Docker
docker run
= docker create
+ docker start
.
Solution 2 - Docker
From docker documentation
> The docker create command creates a writeable container layer over the
> specified image and prepares it for running the specified command. The
> container ID is then printed to STDOUT. This is similar to docker run
> -d except the container is never started. You can then use the docker start
Solution 3 - Docker
docker create
command creates a writeable container from the image and prepares it for running.
docker run
command creates the container (same as docker create
) and starts it.
Solution 4 - Docker
The other answers have this covered but I thought I'd show the equivalent shell command-lines because it makes it really clear:
$ docker run myimage
is the same as
$ docker start -a $(docker create myimage)
Here, docker create
is used to create a container from the named image and outputs the created container id and docker start
is used to start the container with that id. The -a
option causes the terminal to attach so that the container runs in the foreground which is the default behaviour of docker run
.
A container that has been created but never started will have a Created
status; this can be seen with docker container ls -a
.
Solution 5 - Docker
I'm new to docker and just got around to playing with it;
My take is that docker run essentially does the following: (in the order of..) docker create, docker start, docker attach , since it immediately attaches to the active shell after you do the 'run' command.
Solution 6 - Docker
to create a container:
to start a container:
to create and start with a single command:
Now to understand we must dig deep with create and start.
Process of creating a container is taking the file system from image, and kind of prep it for use in the new container. When we create the container we are just prepping or setting up the file system snapshot to be used to create the container to actually start the container.
So creating container is about the file system starting it is about actually executing the startup the command. And to start the container, we actually execute the start up command that might start up the process.
Lets see it in terminal: When I run command "sudo docker create hello-world" it prints bellow output.
In the output we saw characters printed out. This is the ID of the container that was just created, Now I can actually execute the hello world command inside of this container by running Docker start.
So what happened here, first off we kind of prop the container by getting the file system ready. Then after that we actually executed the primary start up command in there with Docker start.
-a in the docker start command is for watching output from the container and print it out to your terminal.
So there is very small difference between Docker run and docker start, by default Docker run is going to show you all the logs or all the information coming out of the container. By default Docker start is the opposite Docker start is not going to show you information coming out of the terminal.
Now you know when you need to use Run / Create / Start
Solution 7 - Docker
Docker run is basically for running commands in the container.
docker run -it <Container Name> /bin/bash
The above is for creating a bash terminal. And make us use bash commands in the container.
Docker create is to create a container from an Docker Image.
docker create -d /var/lib:/var/lib --name docker-ubuntu ubuntu
The above is to create a docker a container of the name "docker-ubuntu" from the image "ubuntu"