Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker run vs create+start: why are created containers different?

Tags:

docker

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?

like image 631
basher Avatar asked Aug 19 '17 12:08

basher


People also ask

Does docker run always create a new container?

docker container run is a shorthand for docker container create and docker container start . So, by definition, it creates a new container every time.

What is the difference between docker commands up run and start?

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.

What is the significance of start command in docker?

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.

What happens with docker start?

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.


1 Answers

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

like image 68
Phil E Avatar answered Sep 17 '22 16:09

Phil E