如何在系统启动时运行docker-compose up -d? [英] How to run docker-compose up -d at system start up?
问题描述
为了让容器在启动时自动启动,我尝试添加命令:
To let the containers autostart at startup point, I tried to add the command:
cd directory_has_docker-compose.yml&在/etc/rc.local中将docker-compose up -d
。
,但是随后我重新启动机器后,容器无法工作。
but then after I reboot the machine, the containers not work.
如何运行 docker-compose up -d
在系统启动时?
How run docker-compose up -d
at system start up?
推荐答案
当我们使用 crontab
或已弃用的 /etc/rc.local
文件,我们需要延迟(例如 sleep 10
,具体取决于机器)以确保系统服务可用。通常, systemd
(或 upstart
)用于管理系统启动时启动的服务。您可以尝试使用类似的配置:
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
(or upstart
) is used to manage which services start when the system boots. You can try use the similar configuration for this:
# /etc/systemd/system/docker-compose-app.service
[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
[Service]
Type=oneshot
RemainAfterExit=yes
WorkingDirectory=/srv/docker
ExecStart=/usr/local/bin/docker-compose up -d
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
[Install]
WantedBy=multi-user.target
或者,如果要运行时不带 -d
标志:
Or, if you want run without the -d
flag:
# /etc/systemd/system/docker-compose-app.service
[Unit]
Description=Docker Compose Application Service
Requires=docker.service
After=docker.service
[Service]
WorkingDirectory=/srv/docker
ExecStart=/usr/local/bin/docker-compose up
ExecStop=/usr/local/bin/docker-compose down
TimeoutStartSec=0
Restart=on-failure
StartLimitIntervalSec=60
StartLimitBurst=3
[Install]
WantedBy=multi-user.target
更改 WorkingDirectory
参数和您的dockerized项目路径。并启用该服务以自动启动:
Change the WorkingDirectory
parameter with your dockerized project path. And enable the service to start automatically:
systemctl enable docker-compose-app
这篇关于如何在系统启动时运行docker-compose up -d?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!