Learn About Docker Container Statuses
Every docker container that exists on a host has a status (or state) which tells the condition of the container. If you are someone who works on docker, then knowing all those statuses can significantly help you in monitoring container health checks and debugging.
Photo by Steven Lelham on Unsplash
Created Status
When you create a Docker container, you add a writable layer on top of the Docker image. A docker container with create status denotes a very initial status which tells that the container is created from Docker image and yet not started. A container can be seen with in this status if its created with following docker CLI commands:
1. docker create command:
$ docker create [OPTIONS] IMAGE [COMMAND] [ARG...]
Docker create command is mostly used to create a container when you want to set up a container configuration in advance (to speed up creation) and make the container ready to start immediately when you need it. You can use docker start <ContainerID or ContainerName> command to start an already created container.
Example :
$ docker create --name my-container --memory 1GB --network=my-net busybox
The above command will create a new container from a busybox image and allocate 1GB memory and assign a custom network to the container. When this container will be started with the docker start command, then the container will run with the defined configurations.
2. docker run command:
$ docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
docker run = docker create + docker start
This means, when a docker run command is executed then it creates a container from a docker image and also starts it. A container can be seen in a created status with docker run command if it is created successfully but failed to start.
3. docker stack deploy command:
$ docker stack deploy [OPTIONS] STACK
Running Status
A one-shot container accomplishes a particular task and stops, whereas long-running a container runs for an indefinite period of time unless someone stops it manually or the container crashes because of some error. Both types of containers can be seen with this status, though one-shot containers may live with this status for a short period of time.
Running is the main status of a container which tells that the container is created and running without any problem. This status also indicates that the container's primary/root process (PID 1) is up and running. The docker exec command can also be used to run a new command in a running container.
Example:
$ docker run --name my-nginx -d nginx:alpine nginx-debug -g 'daemon off;'
$ docker inspect --format='{{ .State.Status }}' my-nginx
running
Exited Status
A container achieves this status when the primary/root process of the container has exited. This status denotes a graceful exit of the container i.e. - container was running a short-lived program or it received a signal to gracefully stop it.
$ docker run --name hello hello-world
$ docker inspect --format='{{ .State.Status }}' hello
exited
OR
$ docker ps -af name=hello
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a9ed01c4dad7 hello-world "/hello" 2 minutes ago Exited (0) 2 minutes ago hello
Restarting Status
You can see a container in this status when someone restarts a container manually, or the container restarts itself because of some problem. Here is the command to restart a container manually:
$ docker restart [OPTIONS] CONTAINER [CONTAINER...]
When we talk about restarting status of the docker containers, then it’s also good to know that Docker daemon comes with different restart policies. These policies are used by the daemon to automatically handle the start of containers when they exit or restart. They ensure what actions it should take on associated containers on exits/failures.
Here is an example of restart policies using the docker run command with --restart flag:
$ docker run -d --restart unless-stopped alpine
Restart Policies:
- no : The default restart policy is denoted with no, with this policy, a container will never restart automatically.
- on-failure : This will restart the container if the container crashes or an error caused the container to exit
- always : A container with this policy will always restart if it stops.
- unless-stopped : Docker daemon will always try to restart a container with this policy unless someone stops it manually.
Example of docker-compose file with a restart policy:
---
version: "3.5"
services:
redis:
image: redis:alpine
restart: always
Removing Status
A docker container is mostly seen with removing status when someone uses docker rm command to remove the container and the removal/cleanup process is still going on the host.
$ docker rm [OPTIONS] CONTAINER [CONTAINER...]
Example: Here is an example of a container created from debian:jessie image then removed it by docker rm command. docker rm is executed in the background and added some delay to print out the status of the container when it's being removed.
$ docker run --name debian debian:jessie
$ docker rm ubuntu & for run in {1..100}; do; done && docker inspect --format='{{ .State.Status }}' ubuntu
[2] 55131
removing
ubuntu
Paused Status
This status is seen when someone temporarily stops a running Docker container using docker pause command. Docker pause command suspends all the processes running inside a running container and they can be resumed by using docker unpause command. The common use of this pause command is to pause resource-intensive tasks that you can resume at a later date.
$ docker pause CONTAINER [CONTAINER...]
$ docker inspect --format='{{ .State.Status }}' my-nginx
paused
Example of docker unpause:
$ docker unpause CONTAINER [CONTAINER...]
$ docker unpause my-nginx
$ docker inspect --format='{{ .State.Status }}' my-nginx
running
Dead Status
We can see a container with a dead status, most likely because of an error that occurs when the docker daemon attempts to remove the container but cannot do it. The dead state also denotes a partially removed container.
There is not a specific reason for a container to be in a dead state. One reason for this could be if other processes kept resources busy or an issue in the underlying hardware/storage. Typically, in most of the case, dead containers cannot restart, we can remove them. We also call such containers in defunct or zombie status.