70

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?

Harald Nordgren
  • 11,693
  • 6
  • 41
  • 65
  • 5
    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`. – Rickkwa Dec 09 '16 at 05:21
  • Indeed, each of the services have `restart: always`. I will change it and see what happens. – Harald Nordgren Dec 09 '16 at 09:34
  • 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! – Harald Nordgren Dec 09 '16 at 09:36
  • 1
    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. – Jack M. Feb 07 '17 at 00:00

6 Answers6

81

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>
davey
  • 1,666
  • 17
  • 24
  • 1
    This is also useful when you lost the `.yml`/or the project somehow :P – JPG Sep 18 '20 at 04:43
  • You can also get the current restart policy like so: `docker inspect --format='{{.HostConfig.RestartPolicy.Name}}' ` – yohosuff Jun 24 '21 at 17:21
20

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.

Flair
  • 2,609
  • 1
  • 29
  • 41
Grigory
  • 411
  • 3
  • 10
  • 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. – Akshay barahate May 14 '19 at 06:58
19

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.

vanduc1102
  • 5,769
  • 1
  • 46
  • 43
  • 10
    This has the unintended consequence of deleting things. The creation time from scratch is 20 mins for me, so this is not an option. – Harald Nordgren Oct 11 '17 at 09:49
  • 1
    This deletes the data. In many cases could be a database, so this may not be a good solution in many cases. – Daniel Benedykt Aug 22 '20 at 12:50
  • 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 ` – Sham Fiorin Oct 18 '21 at 14:25
10

Use the following command if you want to stop a specific container:

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

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

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

Thank you

Hudson Van-dal
  • 101
  • 1
  • 2
2

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
bmihelac
  • 6,233
  • 34
  • 45
0

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.

Jacu
  • 11
  • 2