7

I'm new to docker-compose. Before, when I started containers manually, after a host reboot I had to start the containers manually.

Today I found that -after a host reboot- I had 4 containers running. Those were previously started with docker-compose.

But docker-compose does not work well unless you are in the proper directory with the docker-compose.yml.

Question

How can I know what docker-compose.yml or (which path) was used to launch the docker containers that I find already started as soon as I login after a reboot?

I tried

docker inspect xxxxx

but I could not find any clue on what docker-compose.yml was used to launch.

Xavi Montero
  • 9,239
  • 7
  • 57
  • 79
  • Have you set [restart](https://docs.docker.com/compose/compose-file/#restart) in compose file or use `docker inspect $container` to see if any restart was set for these auto up containers? – atline Aug 15 '19 at 01:38

2 Answers2

10

docker-compose is not starting anything.

The Docker daemon is starting containers on which you have set a restart policy (possibly in one of your docker-compose.yaml files). You can simply remove these containers (docker container rm ...) if you don't need them anymore, or you can reset the restart policy using docker container update --restart=no <image_name_or_id>.

You can read more about restart policies here.

But docker-compose does not work well unless you are in the proper directory with the docker-compose.yml.

Since docker-compose isn't involve at this stage (it may have been responsible for creating the containers but it is not responsible for restarting them), this isn't a problem. Setting an appropriate restart policy on your containers via your docker-compose.yml is the correct way to enable containers to start at boot.


You can set a restart policy when you start a container using docker run by including the appropriate --restart=<policy> option on the command line.

larsks
  • 277,717
  • 41
  • 399
  • 399
  • Ahh I see, just to confirm: I could also manually start a container with `docker`, nothing to do with `docker-compose` and set a "restart policy" that will ensure the container is launched again upon reboot. Nothing to do with the `compose`, but with the `dockerd` itself. Is that true? – Xavi Montero Aug 15 '19 at 14:20
  • Correct. You can set a restart policy when you start a container using `docker run --restart= ...`. – larsks Aug 15 '19 at 14:31
  • Ah, seen and tested!! I'm gonna accept the answer as "the answer", and to improve it I'd suggest you edit it to include your last comment in the answer itself. – Xavi Montero Aug 15 '19 at 14:39
1

in compose file use restart: always to run after machine is rebooted

 services:   
   service1:
    image: serice1:latest
    restart: always
user2308728
  • 91
  • 1
  • 2