Blankman
Blankman

Reputation: 266978

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?

Upvotes: 5

Views: 234

Answers (4)

champion-runner
champion-runner

Reputation: 1647

Docker provides restart policies to control whether your containers start automatically when they exit, or when Docker restarts. Restart policies ensure that linked containers are started in the correct order. Docker recommends that you use restart policies, and avoid using process managers to start containers.

Restart policies are different from the --live-restore flag of the dockerd command. Using --live-restore allows you to keep your containers running during a Docker upgrade, though networking and user input are interrupted.

Source

Upvotes: 0

Exadra37
Exadra37

Reputation: 13054

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.

Upvotes: 2

Jay
Jay

Reputation: 2200

You can use container_name directive in your compose to specify a name, say,

container_name: mycontainer

and then, run,

docker-compose run rails rake db:migrate && docker update --restart=no mycontainer

If you have more than one container in the compose file, you can do this for those which you don't need to restart.

docker-compose run rails rake db:migrate && docker update --restart=no mycontainer1 && docker update --restart=no mycontainer2

You can have this in a shell script. There are other ways, such as using sed to create a temporary compose file without the restart directive. But I think, the above option is the cleanest.

Upvotes: 0

VonC
VonC

Reputation: 1323783

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.

Upvotes: 4

Related Questions