1

I am trying to understand the life cycle of a container. Downloaded alpine image, built containers using "docker container run" command, all of those containers ran and in "Exited" status. While using "docker container start" command, some of the containers are staying in up status(running) and some or Exited immediately. Any thoughts on why the difference in such behavior around statuses? One difference I observed is, containers staying in up status are modified with respect to file structure from base image.

Hope i was able to put the scenario with proper context. Help me in understanding the concept.

Revanth
  • 299
  • 5
  • 13
  • It looks like a duplicate of https://stackoverflow.com/questions/32427684/what-are-the-possible-states-for-a-docker-container – gbajson Apr 12 '19 at 17:49

1 Answers1

2

The long sequence is as follows:

  1. 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.
  2. 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.
  3. The main process exits, or an administrator explicitly docker stops it. The container is in "exited" status.
  4. Optionally you can restart a stopped container (IME this is unusual though); go to step 2.
  5. 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.

David Maze
  • 130,717
  • 29
  • 175
  • 215