The long sequence is as follows:
- You
docker create
a container with its various settings. Some settings may be inherited from the underlying image. It is in a "created" status; its filesystem exists but nothing is running.
- You
docker start
the container. If the container has an entrypoint (Dockerfile ENTRYPOINT
directive, docker create --entrypoint
option) then that entrypoint is run, taking the command as arguments; otherwise the command (Dockerfile CMD
directive, any options after the docker create
image name) is run directly. This process gets process ID 1 in the container and the rights and responsibilities that go along with that. The container is in "running" status.
- The main process exits, or an administrator explicitly
docker stop
s it. The container is in "exited" status.
- Optionally you can restart a stopped container (IME this is unusual though); go to step 2.
- You
docker rm
the stopped container. Anything in the container filesystem is permanently lost, and it no longer shows up in docker ps -a
or anywhere else.
Typically you'd use docker run
to combine these steps together. docker run
on its own does the first two steps together (creates a container and then starts it). If you docker run --rm
it does everything listed above.
(All of these commands are identical to the docker container ...
commands, but I'm used to the slightly shorter form.)
The key point here is that there is some main process that the container runs. Typically this is some sort of daemon or server process, and generally specified in the image's Dockerfile. If you, for example, docker run ... nginx
, then its Dockerfile ends with
CMD ["nginx", "-g", "daemon off;"]
and that becomes the main container process.
In early exploration it's pretty common to just run some base distribution image (docker run --rm -it alpine
) but that's not really interesting: the end of the lifecycle sequence is removing the container and once you do that everything in the container is lost. In standard use you'd want to use a Dockerfile to build a custom image, and there's a pretty good Docker tutorial on the subject.