How to restart Docker service

DockerDockerBeginner
Practice Now

Introduction

Docker is a powerful containerization platform that enables developers to deploy and manage applications efficiently. Understanding how to restart Docker services is crucial for maintaining system stability and resolving potential configuration or performance issues. This tutorial provides comprehensive guidance on restarting Docker services across different scenarios and operating systems. In this lab, you will learn how to restart the Docker daemon and individual containers, and understand the impact of different restart methods.

Restarting the Docker Daemon

The Docker daemon is the background process that manages Docker objects such as images, containers, networks, and volumes. Restarting the Docker daemon is often necessary after making configuration changes or to resolve issues affecting the entire Docker environment.

In this step, you will learn how to restart the Docker daemon using systemd commands.

First, let's check the current status of the Docker service.

sudo systemctl status docker

You should see output indicating whether the Docker service is active and running.

Output of checking Docker service status

Now, let's restart the Docker service. This will stop all running containers managed by the daemon and then start the daemon again.

sudo systemctl restart docker

After the command completes, check the status again to confirm that the Docker service has restarted successfully.

sudo systemctl status docker

You should see that the service has been restarted and is active.

Alternatively, you can stop and start the Docker service separately. First, stop the service:

sudo systemctl stop docker

Check the status to confirm it is inactive:

sudo systemctl status docker

Then, start the service:

sudo systemctl start docker

Finally, check the status one more time to ensure it is active:

sudo systemctl status docker

Restarting the Docker daemon is a fundamental operation for managing your Docker environment. It's important to be aware that this action will stop all running containers.

Restarting Individual Containers

While restarting the entire Docker daemon affects all containers, you often need to restart only a specific container. This is useful for applying container-specific configuration changes, resolving issues with a single application, or simply restarting a service running in a container.

In this step, you will learn how to restart individual Docker containers.

First, let's pull a simple Nginx image and run a container.

docker pull nginx
docker run -d --name my-nginx -p 80:80 nginx

This command pulls the nginx image, runs a container named my-nginx in detached mode (-d), and maps port 80 on the host to port 80 on the container (-p 80:80).

Check that the container is running:

docker ps

You should see the my-nginx container listed with a status of Up.

Now, let's restart the my-nginx container.

docker restart my-nginx

This command sends a stop signal to the container, waits for it to stop gracefully, and then starts it again. By default, Docker waits for 10 seconds for the container to stop before forcefully killing it.

Check the container status again:

docker ps

The container should still be listed as Up, but the STATUS column will show that it was restarted (e.g., Up X seconds).

You can also specify a timeout for the restart operation using the -t flag. For example, to wait for 5 seconds:

docker restart -t 5 my-nginx

This is useful if you need to give your application more or less time to shut down cleanly.

Restarting individual containers provides granular control over your applications without affecting other running containers or the Docker daemon itself.

Configuring Container Restart Policies

Docker allows you to configure restart policies for containers. A restart policy determines whether a container should be automatically restarted by the Docker daemon after it exits. This is a crucial feature for ensuring the availability of your applications.

In this step, you will learn how to configure restart policies for Docker containers.

First, let's stop and remove the previous my-nginx container to start fresh.

docker stop my-nginx
docker rm my-nginx

Now, let's run a new Nginx container with a restart policy of always.

docker run -d --name my-nginx-always --restart=always -p 80:80 nginx

The --restart=always flag tells Docker to always restart the container if it stops, regardless of the exit code. It will also restart the container when the Docker daemon starts.

Check that the container is running:

docker ps

Now, let's simulate a container failure by stopping it manually.

docker stop my-nginx-always

Wait a few seconds and then check the container status again:

docker ps

You should see that the my-nginx-always container has been automatically restarted by the Docker daemon. The STATUS column will indicate that it has been Up for a short period.

Other common restart policies include:

  • no: Do not automatically restart the container (default).
  • on-failure: Restart the container only if it exits with a non-zero exit code (indicating an error). You can optionally specify the maximum number of restart attempts (e.g., on-failure:5).
  • unless-stopped: Always restart the container unless it is explicitly stopped by the user or the Docker daemon is stopped.

Let's try the on-failure policy. Stop and remove the current container:

docker stop my-nginx-always
docker rm my-nginx-always

Run a new container with the on-failure policy:

docker run -d --name my-nginx-on-failure --restart=on-failure -p 80:80 nginx

Check that it's running:

docker ps

Now, let's simulate a failure. We can do this by executing a command inside the container that exits with a non-zero status.

docker exec my-nginx-on-failure sh -c "exit 1"

Check the container status after a few seconds:

docker ps

The container should have been automatically restarted because it exited with a non-zero status.

Restart policies are a powerful tool for ensuring the resilience of your containerized applications. By configuring the appropriate policy, you can automate the recovery of containers that stop unexpectedly.

Summary

In this lab, you have learned how to restart the Docker daemon and individual containers. You explored different methods for restarting the daemon using systemd commands and learned how to restart specific containers using the docker restart command. Furthermore, you learned about Docker's restart policies and how to configure them to automatically restart containers based on different conditions, enhancing the availability of your applications. These skills are fundamental for managing and troubleshooting Docker environments effectively.