【问题标题】:Docker-compose auto-start after lauching AWS EC2 server启动 AWS EC2 服务器后 Docker-compose 自动启动
【发布时间】:2020-08-19 08:39:18
【问题描述】:

每次我重新启动我的 EC2 服务器时,我都必须这样做: sudo systemctl start docker 然后docker-compose up -d 启动我所有的容器。
我想按照answer 中的建议创建一个 systemd 来在实例开始时自动运行这两个命令。

到目前为止,我已经在/etc/systemd/system/ 中创建了一个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

我不知道我的docker_boot.service 文件的内容是否正确。理想情况下,我想在关闭实例时也这样做docker-compose down

然后我做了:

sudo systemctl enable docker
sudo systemctl enable docker_boot

但是当我重新启动 EC2 实例时,我的 docker 映像没有运行,我该如何调试呢?
请在下面找到我的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-web-services docker amazon-ec2 docker-compose system


【解决方案1】:

要在启动时启用您的 docker 守护程序,只需运行以下命令:

sudo systemctl enable docker

要在启动时运行docker-compose up,您可以在继续选项卡中添加这一行。必须使用撰写文件的绝对路径。

@reboot /usr/bin/docker-compose -f /absolute-path-to-your/docker-compose.yml up

【讨论】:

  • 感谢您的回答,这很奇怪,但是按照您的指示,它只运行容器certbot 和容器shinyproxy,您知道为什么要查看docker-compose.yml 吗?
  • 重启策略:on-failure
  • 谢谢。您可以看到 restart: on-failure 已经存在 nginx 映像,但仍然没有启动它。我将它添加到其他图像中,但没有改变任何东西。
  • 您好@ML_Enthousiast,很抱歉之前的快速答复。我正在公路旅行中,我有几天无法使用电脑。尝试一步一步调试。检查 cron 作业是否真的执行。检查容器的日志并尝试找到一些线索。与您的重启策略保持一致(您可以尝试在所有容器上添加 restart: always,然后我猜您将不再需要 cron 作业)