This tutorial will show you how to restart a Docker container from the terminal.
Docker is one of the best tools for quickly deploying applications on a machine. As everything your application needs to run is kept within a container, you can often have software running with a single command.
While Docker will typically automatically restart your container if something goes wrong. There is a chance you may need to manually restart your container. For example, if you made a configuration change that the service needs to restart to load in.
Luckily, Docker has a very well-designed command line interface that makes managing your containers from the terminal a breeze.
The whole management process is even easier if you use a Compose stack to manage your containers.
Restarting a Docker Container using the Terminal
In this section, we will show you how to restart individual Docker containers using the terminal. By the end of these steps, you will know how to get the ID of the container and restart it.
Finding the Docker Container ID or Name
1. The first thing you will need to do before you can restart a Docker container running on your system is to get its ID or name.
The easiest way to get these values is to use Docker’s list container command, as shown below.
docker container list
2. After running the previous command, you will get a list of every Docker container running on your system.
From this list, identify the container that you want to restart and make a note of either the containers name or ID.
For example, if we want to restart the Uptime Kuma Docker container, we will be able to use either the ID “8467aa5c7d77
” or the name “uptimekuma-uptime-kuma-1
“.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8467aa5c7d77 louislam/uptime-kuma:1 "/usr/bin/dumb-init …" 2 days ago Up 7 hours (healthy) 3001/tcp uptimekuma-uptime-kuma-1
b180d9bce829 nginxproxy/nginx-proxy "/app/docker-entrypo…" 2 days ago Up 7 hours 0.0.0.0:80->80/tcp, :::80->80/tcp, 0.0.0.0:443->443/tcp, :::443->443/tcp reverseproxy-nginx-proxy-1
Restarting the Docker Container
3. Once you know the ID or name of the Docker container you want to restart, all you need to do is use the following command within the terminal.
Ensure that you replace “<ID OR NAME>
” with the ID or name belonging to your container. If you want to restart multiple containers at once, just separate each ID/Name by a single space.
docker container restart <ID OR NAME>
For example, if we were to restart the Uptime Kuma container we got in the previous step using its ID, we would run the following command
docker container restart 8467aa5c7d77
We could also use the container name instead if we wanted.
docker container restart uptimekuma-uptime-kuma-1
4. You can tell that your container has been successfully restarted as Docker will simply print out the name or ID you used to restart it back to the terminal.
Using the Terminal to Restart a Docker Compose Stack
If you are using Docker Compose stacks to manage your containers, there are a few other ways you can restart your containers. You can choose to restart the entire Docker Compose stack, or you can restart individual containers.
Navigating to your Compose File
1. The first thing you need to do before you do anything is to change to the same directory where your Docker Compose file has been written.
For this example, we will change the directory to the one where our Uptime Kuma Docker Compose stack is written.
cd /opt/stacks/uptime-kuma
Restarting the Whole Docker Compose Stack
2. Once you are in the right location, restarting all containers in your Docker Compose stack is as simple as using the following command within the terminal.
Docker will immediately stop, and then start all containers simultaneously
docker compose restart
When restarting containers in a Docker Compose stack you will be given a status of the process. Once a container has been restarted, you should see a tick appear next to its name and the status marked as “Started
“.
[+] Restarting 1/1
✔ Container uptimekuma-uptime-kuma-1 Started 4.6s
Only Restarting a Single Container in your Compose Stack
3. It is also possible to restart individual containers specified within your Compose stack.
If you have forgotten what service you have defined within your stack, you can list them out by using the following command
docker compose ps
Using this command, you will see a list similar to what we have shown below. The service name is the value you want for the container you want to restart.
For example, with the result below we can use “uptime-kuma
” to restart the container.
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
uptimekuma-uptime-kuma-1 louislam/uptime-kuma:1 "/usr/bin/dumb-init …" uptime-kuma 2 days ago Up 2 minutes (healthy) 3001/tcp
4. Once you know the name or service you want to restart in your Docker Compose stack all you need to do is use the following command within the terminal.
Just ensure that you replace “<CONTAINER>
” with the service name.
docker compose restart <CONTAINER>
For example, to restart the container we have defined in our Compose file, we could use the command below if we use the “service
” name.
docker compose restart uptime-kuma
Conclusion
By this point in this guide, you should hopefully have a good idea of how you can restart a Docker container.
The Docker command line tool makes it easy to restart containers running on your system.
Please feel free to comment below if you have had any issues following our guide.
If you liked this tutorial, we recommend taking some time to explore some of our many other Docker guides. These will show you how to run various pieces of software as well as how to use Docker in general.