Related to
docker container started in Detached mode stopped after process execution
https://serverfault.com/questions/661909/the-right-way-to-keep-docker-container-started-when-it-used-for-periodic-tasks
I do understand the difference between docker run
and create
+ start
, but don't understand how the actual containers created in these two ways differ.
Say I create and run a container with
docker run -dit debian:testing-slim
and then stop it. The created container can later be started with
docker start silly_docker_name
and it'll run in the background, because the entry command for the image is bash.
But when a container is first created
docker create --name silly_name debian:testing-slim
and then started with
docker start silly_name
then it'll exit immediately. Why isn't bash started, or how come it exits in this case?
docker container run is a shorthand for docker container create and docker container start . So, by definition, it creates a new container every time.
The run command acts like docker run -ti in that it opens an interactive terminal to the container and returns an exit status matching the exit status of the process in the container. The docker compose start command is useful only to restart containers that were previously created, but were stopped.
The docker start command is one of the basic commands that has formed Docker's foundation. The command is used to start one or more stopped Docker containers. Ace your System Design Interview and take your career to the next level.
When Docker starts it runs any initializers via reexec, if any, then it parses the flags for the Docker executable via the mflag package. This packages lives under pkg/mgflag and is aliased as flag. At this point it can print out version information if necessary or enable debug mode and debug logging.
The difference for a container process that is a shell (like bash
in your debian example) is that a shell started without a terminal+interactive "mode" exits without doing anything.
You can test this by changing the command of a create
'd container to something that doesn't require a terminal:
$ docker create --name thedate debian date
Now if I run thedate
container, each time I run it it outputs the date (in the logs) and exits. docker logs thedate
will show this; one entry for each run.
To be explicit, your docker run
command has flags -dit
: detached, interactive (connect STDIN), and tty are all enabled.
If you want a similar approach with create & start, then you need to allocate a tty
for the created container:
$ docker create -it --name ashell debian
Now if I start it, I ask to attach/interactively to it and I get the same behavior as run:
$ docker start -ai ashell
root@6e44e2ae8817:/#
NOTE: [25 Jan 2018] Edited to add the -i
flag on create
as a commenter noted that as originally written this did not work, as the container metadata did not have stdin connected at the create stage
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With