I am using the option to restart my docker instances in my docker-compose file like:
restart: always
The problem is that sometimes I run a single docker container for maintenance work like:
docker-compose run rails rake db:migrate
The issue with this is when I do a 'docker ps' I can see those on-off commands are still running and constantly being restarted:
"rake db:migrate" 2 days ago Restarting (7) 18 seconds ago
Is there a way to run a docker image that is for on-off purposes, but still have the restart policy on it but somehow ignore it for this single instance usage?
Using the --restart flag on Docker run you can specify a restart policy for how a container should or should not be restarted on exit.
Docker provides a restart policy for your containers by supplying the --restart command line option. Supplying --restart=always will always cause a container to be restarted after the Docker daemon is restarted.
Once the docker image is running (after docker-compose run), you could amend it, using docker update
:
docker update --restart=no <MY-CONTAINER-ID>
That would prevent said container to restart when you stop it.
See restart policies.
Just create another service in your docker-compose.yml
file that uses the same docker image, but sets the restart to restart: "no"
.
Dummy Example:
version: "2.1"
services:
rake_web:
image: rake-image-name
restart: always
networks:
- rake_network
rake_cli:
image: rake-image-name
restart: "no"
networks:
- rake_network
networks:
rake_network:
driver: "bridge"
Now instead of:
docker-compose run rake_web some-command
You use:
docker-compose run rake_cli some-command
Once rake_cli
is using the same docker image, but with the restart policy disabled your container for rake_cli
will not restart after you have run your command on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With