In practice to start a container I do:
docker run a8asd8f9asdf0
If thats the case, what does:
docker start
do?
In the manual it says
Start one or more stopped containers
This is a very important question and the answer is very simple, but fundamental:
docker run IMAGE_ID
and not docker run CONTAINER_ID
docker stop CONTAINER_ID
, you can relaunch the same container with the command docker start CONTAINER_ID
, and the data and settings will be the same.docker rm container_id
). On the other hand, volumes data survive container's removal unless -v
option is explicitly supplied on the command line. Volume location on the host system can be directly inspected. See this article
&
at the end of the run
command line? I need it as a countinous (24 hours) service
Commented
Nov 14, 2019 at 23:54
docker run hello-world
it prints "Hello from Docker!" but when you create a container by doing docker create hello-world
and then start the container it won't print that output. What's happening there?
Commented
Jul 31, 2020 at 19:46
run
runs an imagestart
starts a container.The docker run
doc does mention:
The
docker run
command first creates a writeable container layer over the specified image, and then starts it using the specified command.That is, docker run is equivalent to the API
/containers/create
then/containers/(id)/start
.
You do not run an existing container, you use docker exec -it
to do it (since docker 1.3), more info at How do I run a command on an already existing Docker container?. And finally you can restart an exited container, more info at How to continue a Docker container which has exited.
-i
to docker run
for an interactive process? I mean, docker run needs an image to run a container.
docker exec
is for running command in a running container.
run
command creates a container from the image and then starts the root process on this container. Running it with run --rm
flag would save you the trouble of removing the useless dead container afterward and would allow you to ignore the existence of docker start
and docker remove
altogether.
run
command does a few different things:
docker run --name dname image_name bash -c "whoami"
docker ps
bash -c "whoami"
. If one runs docker run --name dname image_name
without a command to execute container would go into stopped state immediately. docker remove
before launching container under the same name.How to remove container once it is stopped automatically? Add an --rm
flag to run
command:
docker run --rm --name dname image_name bash -c "whoami"
How to execute multiple commands in a single container? By preventing that root process from dying. This can be done by running some useless command at start with --detached
flag and then using "execute" to run actual commands:
docker run --rm -d --name dname image_name tail -f /dev/null
docker exec dname bash -c "whoami"
docker exec dname bash -c "echo 'Nnice'"
Why do we need docker stop
then? To stop this lingering container that we launched in the previous snippet with the endless command tail -f /dev/null
.
docker start
it again, is that not so?
Commented
Oct 18, 2021 at 15:55
Explanation with an example:
Consider you have a game (iso) image in your computer.
When you run
(mount your image as a virtual drive), a virtual drive is created with all the game contents in the virtual drive and the game installation file is automatically launched. [Running your docker image - creating a container and then starting it.]
But when you stop
(similar to docker stop) it, the virtual drive still exists but stopping all the processes. [As the container exists till it is not deleted]
And when you do start
(similar to docker start), from the virtual drive the games files start its execution. [starting the existing container]
In this example - The game image is your Docker image and virtual drive is your container.
docker run hello-world
it prints Hello from Docker! but when you create a container by doing docker create hello-world
and then start the container by doing docker start 27c833038489
it won't print that output. What's happening there?
Commented
Jul 31, 2020 at 19:47
docker run
by default runs in foreground (use -d
to detach it), whereas docker start
by default runs in background (use -a
to run in foreground). So, docker start -a 27c833038489
WILL print the Hell from Docker message. So much for consistency, eh?
Commented
May 11, 2021 at 13:35
daniele3004's answer is already pretty good.
Just a quick and dirty formula for people like me who mixes up run
and start
from time to time:
docker run [...]
= docker pull [...]
+ docker start [...]
"...docker run is equivalent to the API /containers/create then /containers/(id)/start.
(source: docs.docker.com/engine/reference/commandline/run)
It would have been wiser to name the command "new" instead of "run".
Run creates a container instance of an existing (or downloadable) image and starts it.
execute
?