To let the containers autostart at startup point, I tried to add the command:
cd directory_has_docker-compose.yml && docker-compose up -d
in /etc/rc.local.
but then after I reboot the machine, the containers not work.
How run docker-compose up -d
at system start up?
I tried
restart: always
, it works at some containers(like php-fpm), but i faced the problem that some containers(like nginx) is still not restarting after reboot.Solved the problem.
When we use
crontab
or the deprecated/etc/rc.local
file, we need a delay (e.g.sleep 10
, depending on the machine) to make sure that system services are available. Usually,systemd
(orupstart
) is used to manage which services start when the system boots. You can try use the similar configuration for this:Or, if you want run without the
-d
flag:Change the
WorkingDirectory
parameter with your dockerized project path. And enable the service to start automatically:You should be able to add:
to every service you want to restart in the docker-compose.yml file
Use restart: always in your docker compose file.
Docker-compose up -d
will launch container from images again. Usedocker-compose start
to start the stopped containers, it never launches new containers from images.Also you can write the code up in the docker file so that it gets created first, if it has the dependency of other containers.
If your
docker.service
enabled on system startupand your services in your
docker-compose.yml
hasall of the services run when you reboot your system if you run below command only once
As an addition to
user39544
's answer, one more type of syntax forcrontab -e
: