Docker run vs create
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?
Solution 1:
docker run
= docker create
+ docker start
.
Solution 2:
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 command to start the container at any point.
This is useful when you want to set up a container configuration ahead of time so that it is ready to start when you need it. The initial status of the new container is created.
Solution 3:
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:
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:
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.