Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignoring the restart policy for running on-off commands on the same docker image used for long-running services

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?

like image 575
Blankman Avatar asked Sep 24 '19 02:09

Blankman


People also ask

Which docker run flag should you use to configure the restart policy for a container?

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.

Which option has to be passed to docker run to restart a container on daemon startup?

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.


2 Answers

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.

like image 176
VonC Avatar answered Oct 11 '22 20:10

VonC


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.

like image 2
Exadra37 Avatar answered Oct 11 '22 22:10

Exadra37