Skip to content

Containers lifecycle

docker pull - downloads an image from a registry

docker create - creates a container from an image

docker start - starts a container

docker run - docker pull + docker create + docker start

docker pause - pauses the container

docker unpause - resumes the container

docker stop - stops the container gracefully by sending SIGTERM to PID 1

docker kill - forcefully stops the container

docker rm - removes the container

There are several reasons why a container may be in the Exited status:

  • the process inside the container finished execution;
  • the process was terminated by an external command, for example, docker stop or docker kill;
  • the process inside the container terminated due to a bug in the code or an unhandled error, or, for example, the available memory ran out, in which case the program will terminate with an OutOfMemoryException error.

The docker stop command stops the container in a so-called graceful way. Docker sends a termination signal to the container, SIGTERM to the main process (PID 1) of the container. This allows the main process to shut down safely by completing all related operations. For example, releasing resources, closing database connections, saving data. docker stop is considered a safe way to stop a container.

There is another command called docker kill. Unlike docker stop, this command sends the SIGKILL signal and immediately stops the container. It is best used when a container has stopped responding to other commands.

To find out why a container stopped, you can run the following command:

docker container inspect 709f5e5d73e6

Where 709f5e5d73e6 is the container ID. In the output, we are interested in the State field:

Restart Policy

Very often we will need our container to stay running as long as possible, without having to manually restart it after some problem. To achieve this, during docker run we need to add the --restart parameter, which controls the container's behavior after it enters the Exited state. This parameter can have 4 values:

  • no — the default, do not restart the container.
  • on-failure — restart the container only if it exited with an error exit code (exit code is not 0).
  • always — always restart the container, even if it was stopped.
  • unless-stopped — restart the container, unless it was stopped by the user or the Docker daemon is restarting.

Let's try using always:

docker run -d -p 8080:9090 --restart always server:1.0
docker ps

Download image (and run) e.g nginx

docker run -p 8080:80 nginx

or just download image:

docker pull nginx

History of commands that was used during crating of every layer of image:

docker history nginx
#Delete container
docker ps #shows containers
docker ps -a #shows ALL containers even shutdowned containers
docker rm <id_from_previous_command>
#or
docker container rm <id_from_previous_command> #deletes container

#Delete images
docker images #shows all images with IDs
docker rmi <id_from_previous_command> #e.g nginx