If you have started working with Docker containers in your environment, you are undoubtedly seeing the benefits of running containers and the flexibility they bring to your application environment. However, one of the necessary skills when working with containers is understanding how to troubleshoot various issues with your containers. One of those is your Docker container keeps restarting.
What are Docker Containers?
Containers are essentially application images that allow you to run self-contained applications inside an isolated environment. With Docker, you can save tremendous resources running your applications in containers rather than in full virtual machines.
Docker is the most popular container runtime engine used by many businesses and enterprises worldwide. It is the de facto standard in containerization.
Docker file
Using a simple Docker file, you can configure your container as you need and run the container on your Docker host. The Docker file is essentially a config file that contains the configuration for a specific Docker container.
However, there can be issues when you are learning to configure your Docker environment and Docker containers with your container in a restarting loop. If this happens, let’s see what we need to do to start troubleshooting.
Docker compose file
It also provides tools like the Docker compose file that allows spinning up multiple Docker containers simultaneously. A Docker compose file is a YML file that contains the configuration code for your containers that have their configuration written in the compose file.
Troubleshooting Docker containers keeps restarting after reboot
There are a few Docker commands that are extremely powerful when it comes to troubleshooting a restarting loop with a Docker container. Let’s see how we can use the following:
Docker ps and docker-compose ps
Docker inspect
Docker stop and start
Docker logs
Docker ps and Docker-compose ps
The first command you want to use religiously when it comes to troubleshooting Docker containers is the Docker ps command. With the docker ps command, you can see the current state of your running containers and if you have a container that has errors.
The docker ps command gives you the output of the following information:
container id
image – version of the image
command – what process it is running
when created
status
ports
names – comment or friendly name
The status column is one of the critical pieces of information you want to pay attention to with the output of the docker ps command. The status column in the output of the Docker ps command tells you the state of the container, if it is up, exited, or restarting. We want to pay attention to the restarting status as this generally means something is not right with the container, causing it to go into a crash loop.
As you see in the image below, the Prometheus container has a status of Restarting <number of> seconds ago. This status is a sign that something is not correct.
Docker inspect
With the docker inspect command, you can see the volumes that are mounted, the networking, the restart policy, and many other configuration parameters of the container to help with troubleshooting.
Below, we see the binds, port bindings, and restart policy displayed for the container.
Docker stop and docker start
You can if you want to try to stop the container and start the container again, you can issue the command docker stop <container id or name> and then execute docker start <container id or name> to attempt to stop and start the container.
However, generally speaking this will likely see the same results as the state of the container stuck in the restart loop. You most likely still have something configured causing the failure when you issued the docker run command or used docker-compose up -d.
Docker logs
One of the key commands to get to the bottom of why the container is restarting is the docker logs command. With Docker logs, you can see the reason the container was restarted and possibly the fix for the issue. It is a handy tool allowing you to investigate the situation with the restart.
You will likely see the low-level error message in the Docker logs causing the container to exit and restart. After pulling up the logs using the docker logs command, you can see the system is having an issue with the volumes configured. We are getting permission denied when it is attempting to create a file inside the container. This helps to pinpoint a solution to the issue.
To resolve the issue in the above image, it requires fixing permissions in the Docker compose file, setting the default user of the container to be a user with permissions to access the folder.
After resolving the permissions error, I brought down the docker-compose stack and then brought it back up. As you can see now, the Prometheus container is now up and running and stable, without crashing.
Wrapping Up
If you are troubleshooting Docker containers, keeps restarting after reboot, the Docker command line is your friend to troubleshoot issues in the Docker environment. Generally speaking, with the commands listed above, you can get an idea of why the Docker service cannot correctly bring up the container.
0 Comments