如何在系统启动时运行 docker-compose up -d? [英] How to run docker-compose up -d at system start up?

查看:44
本文介绍了如何在系统启动时运行 docker-compose up -d?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了让容器在启动点自动启动,我尝试添加以下命令:

To let the containers autostart at startup point, I tried to add the command:

cd directory_has_docker-compose.yml &&docker-compose up -d 在/etc/rc.local 中.

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.

如何在系统启动时运行 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

使用您的 dockerized 项目路径更改 WorkingDirectory 参数.并启用服务自动启动:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆