How to Disable Autostarting Docker Containers After Reboot
Docker containers are incredibly useful for running applications in isolated environments. However, you might not always want your containers to restart automatically after a system reboot. In this guide, we will explore several methods to prevent Docker containers from autostarting after a reboot.
Method 1: Change the Restart Policy of Existing Containers
The restart policy of a Docker container determines whether it will automatically restart after a crash or reboot. To disable this behavior, you need to modify the restart policy.
-
List all running containers:
First, identify the containers you want to modify:
docker ps
-
Stop the container(s):
Stop the containers you want to modify:
docker stop <container_id>
-
Remove the container(s):
Remove the container(s) while keeping their data intact:
docker rm <container_id>
-
Recreate the container without the restart policy:
Recreate the container with the restart policy set to
no
:docker run --name <container_name> --restart=no <image_name>
Replace
<container_name>
and<image_name>
with the appropriate names.
Method 2: Update the Restart Policy for a Running Container
If you prefer not to stop and recreate the container, you can update the restart policy for a running container:
-
Update the restart policy:
Set the restart policy to
no
:docker update --restart=no <container_id>
Method 3: Disable Docker from Starting on Boot
If you want to prevent Docker itself from starting automatically on system boot, you can disable the Docker service. This will affect all containers managed by Docker.
-
Disable Docker service on systemd-based systems:
Disable the Docker service to prevent it from starting on boot:
sudo systemctl disable docker
-
Stop the Docker service:
Stop the Docker service immediately:
sudo systemctl stop docker
Method 4: Modify Docker Compose File
If you are using Docker Compose to manage your containers, ensure that the restart
policy in your docker-compose.yml
file is set to no
for all services.
Example docker-compose.yml
:
version: '3'
services:
my_service:
image: my_image
restart: no
After modifying the docker-compose.yml
file, apply the changes:
docker-compose down
docker-compose up -d
Conclusion
By following these methods, you can prevent your Docker containers from automatically starting after a system reboot. Whether you choose to modify individual container restart policies, update a running container, disable the Docker service, or adjust your Docker Compose configuration, you now have the tools to manage container behavior effectively.
In case you have found a mistake in the text, please send a message to the author by selecting the mistake and pressing Ctrl-Enter.