Disable autostart of docker-compose project
Asked Answered
E

6

80

I have a docker-compose project using Docker for Mac that autostarts when I boot the computer.

I usually start the project with docker-compose up -d, but even running docker-compose stop before shutting down autostarts it again on boot.

I am not aware of specifically enabling this. How can I disable it?

Exodontist answered 8/12, 2016 at 9:46 Comment(4)
Can we see docker-compose file? But I'm guessing there is a restart: always in it. If there is, then try changing it to restart: unless-stopped.Packsaddle
Indeed, each of the services have restart: always. I will change it and see what happens.Exodontist
That did not work. Setting restart: unless-stopped causes docker to time out on requests (ps, stop, kill). And rebooting still starts all the containers!Exodontist
Not certain about docker-compose stop, but for me docker-compose down works like a charm. I tend to pair it with --rmi local, but be careful with that.Unfamiliar
S
86

Today I had the same issue that all containers are started when I boot my dev laptop, as restart: always was set in the .yml files.

As I don't want to touch the .yml files, I just found out (thx Bobby) how to alter this setting by:

docker update --restart=no <MY-CONTAINER-ID>
Substructure answered 5/4, 2017 at 10:59 Comment(2)
This is also useful when you lost the .yml/or the project somehow :PStarrstarred
You can also get the current restart policy like so: docker inspect --format='{{.HostConfig.RestartPolicy.Name}}' <MY-CONTAINER-ID>Deaf
F
23

restart: no is default mode. There is line inside your docker-compose file with restart: no or restart: unless-stopped. It also means that when you boot your system, it (re)starts container(s) again as long as docker daemon. Details
You need to change restart to no or on-failure, example:

version: '2.1'
services:
    backend:
        restart: on-failure
        build:
            args:
                USER_ID: ${USER_ID}
            context: codebase/namp-backend
            dockerfile: Dockerfile.dev
        ports:
          - "5001:5001"
          - "5851:5851"
        volumes:
          - ./codebase/namp-backend:/codebase
        environment:

Also docker-compose down for most cases, gives you the same result - do not start containers while (docker) system startup, except: containers will be deleted after this, not stopped.

Fatally answered 12/4, 2018 at 21:12 Comment(1)
no is the default restart policy, and it does not restart a container under any circumstance. When always is specified, the container always restarts. The on-failure policy restarts a container if the exit code indicates an on-failure error.Ulent
D
18

Try with docker-compose down instead of docker-compose stop

down

Stops containers and removes containers, networks, volumes, and images created by up. Networks and volumes defined as external are never removed.

stop

Stops running containers without removing them. They can be started again with docker-compose start.

Dandiprat answered 11/10, 2017 at 3:47 Comment(3)
This has the unintended consequence of deleting things. The creation time from scratch is 20 mins for me, so this is not an option.Exodontist
This deletes the data. In many cases could be a database, so this may not be a good solution in many cases.Basil
The needs was just disable auto-start (restart), so if u make a docker-compose down will destroy all data, The best way is to as #davey said docker update --restart=no <MY-CONTAINER-ID>Ternary
R
16

Use the following command if you want to disable autostart after stopping a specific container:

docker update --restart=no <MY-CONTAINER-ID>

If you want to apply this setting to all registered containers, this is the best option:

docker update --restart=no $(docker container ls -a -q)

Thank you

Reckford answered 6/5, 2021 at 12:30 Comment(1)
Other useful commands: docker container ls -a to check containers names & ids and docker inspect --format='{{.HostConfig.RestartPolicy.Name}}' <nameOrId> to check their current settingCompatriot
T
3

Beside setting restart: unless-stopped, remove existing containers and recreate them.

docker-compose down
docker-compose up -d

Now, it would work as expected:

docker-compose stop
sudo service docker restart
docker-compose ps
# should NOT HAVE containers running

docker-compose up -d
sudo service docker restart
docker-compose ps
# should HAVE containers running
Thoroughwort answered 14/5, 2019 at 6:44 Comment(0)
D
1

Just an addition to @Hudson Van-dal comment if you are really using compose. When you are in .yml compose file directory you can use:

docker update --restart=no $(docker compose ps -a -q)

It will filter out only containers running under this particular compose instead of all available containers.

Dynamometry answered 4/5, 2023 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.