【发布时间】:2020-11-28 17:37:20
【问题描述】:
每次我重新启动我的 ec2 服务器时,我都必须这样做:
sudo systemctl start docker 然后docker-compose up -d 启动我所有的容器。
有没有办法在实例开始时自动运行这两个命令?
我已阅读此answer,我认为理想情况下我想知道如何做到这一点:
创建一个 systemd 服务并启用它。所有启用的系统 服务将在通电时启动。
你知道如何创建这样的 systemd 服务吗?
[编辑 1]:根据 Chris William 的评论,我做了以下工作:
感谢 Chris,所以我创建了一个 docker_boot.service,其内容如下:
[Unit]
Description=docker boot
After=docker.service
[Service]
Type=simple
Restart=always
RestartSec=1
User=ec2-user
ExecStart=/usr/bin/docker-compose -f docker-compose.yml up
[Install]
WantedBy=multi-user.target
我在/etc/systemd/system 文件夹中创建了它
然后我做了:
sudo systemctl enable docker
sudo systemctl enable docker_boot
当我打开服务器时,唯一正在运行的 Docker 映像是 certbot/certbot 和 telethonkids/shinyproxy
请在下面找到我的docker-compose.yml 文件的内容。
您是否看到缺少什么以使所有映像都启动并运行?
version: "3.5"
services:
rstudio:
environment:
- USER=username
- PASSWORD=password
image: "rocker/tidyverse:latest"
build:
context: ./Docker_RStudio
dockerfile: Dockerfile
volumes:
- /home/ec2-user/R_and_Jupyter_scripts:/home/maxence/R_and_Jupyter_scripts
working_dir: /home/ec2-user/R_and_Jupyter_scripts
container_name: rstudio
ports:
- 8787:8787
jupyter:
image: 'jupyter/datascience-notebook:latest'
ports:
- 8888:8888
volumes:
- /home/ec2-user/R_and_Jupyter_scripts:/home/joyvan/R_and_Jupyter_scripts
working_dir: /home/joyvan/R_and_Jupyter_scripts
container_name: jupyter
shiny:
image: "rocker/shiny:latest"
build:
context: ./Docker_Shiny
dockerfile: Dockerfile
container_name: shiny
ports:
- 3838:3838
nginx:
image: nginx:alpine
container_name: nginx
restart: on-failure
networks:
- net
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
ports:
- 80:80
- 443:443
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
depends_on:
- shinyproxy
certbot:
image: certbot/certbot
container_name: certbot
restart: on-failure
volumes:
- ./data/certbot/conf:/etc/letsencrypt
- ./data/certbot/www:/var/www/certbot
entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"
shinyproxy:
image: telethonkids/shinyproxy
container_name: shinyproxy
restart: on-failure
networks:
- net
volumes:
- ./application.yml:/opt/shinyproxy/application.yml
- /var/run/docker.sock:/var/run/docker.sock
expose:
- 8080
cron:
build:
context: ./cron
dockerfile: Dockerfile
container_name: cron
volumes:
- ./Docker_Shiny/app:/home
networks:
- net
networks:
net:
name: net
【问题讨论】:
-
是 Amazon Linux 2 实例吗?
-
是 Amazon Linux 2 AMI
-
你检查过日志吗,
docker-compose -f docker-compose.yml logs?也许他们有一些关于为什么失败的信息?
标签: amazon-web-services docker amazon-ec2 docker-compose systemd