gpt4 book ai didi

amazon-web-services - 启动 ec2 服务器时自动启动 docker

转载 作者:行者123 更新时间:2023-12-02 18:03:29 26 4
gpt4 key购买 nike

每次我重新启动我的 ec2 服务器时,我都必须这样做:sudo systemctl start docker然后 docker-compose up -d启动我所有的容器。
有没有办法在实例开始时自动运行这两个命令?
我读过这篇 answer我认为理想情况下我想知道如何做到这一点:

Create a systemd service and enable it. All the enabled systemsservices will be started on powering.


你知道如何创建这样的 systemd 服务吗?
[编辑 1] :在克里斯威廉的评论之后,这是我所做的:
谢谢 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/certbottelethonkids/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

最佳答案

使用 亚马逊 Linux 2 我试图复制这个问题。显然,我没有运行您确切的所有依赖项 docker-compose.yml ,因此我使用了 docker-compose.yml来自 here为了我的验证。文件设置 wordpressmysql .
我采取的步骤如下(在主文件夹中执行为 ec2-user):
1. 安装 docker

sudo yum update -y  
sudo yum install -y docker
sudo systemctl enable docker
sudo systemctl start docker
2.安装docker-compose
sudo curl -L https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m) -o /usr/bin/docker-compose

sudo chmod +x /usr/bin/docker-compose
3.创建 docker-compose.yml
mkdir myapp 
创建文件 ./myapp/docker-compose.yml :
version: '3.3'

services:
db:
image: mysql:5.7
volumes:
- db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress

wordpress:
depends_on:
- db
image: wordpress:latest
ports:
- "8000:80"
restart: always
environment:
WORDPRESS_DB_HOST: db:3306
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
WORDPRESS_DB_NAME: wordpress
volumes:
db_data: {}
4.创建 docker_boot.service该文件与您的不同,因为您的文件中几乎没有潜在问题:
  • 不使用绝对路径
  • ec2-user可能没有权限运行 docker

  • 创建文件 ./myapp/docker_boot.service :
    [Unit]
    Description=docker boot
    After=docker.service

    [Service]
    Type=oneshot
    RemainAfterExit=yes
    WorkingDirectory=/home/ec2-user/myapp
    ExecStart=/usr/bin/docker-compose -f /home/ec2-user/myapp/docker-compose.yml up -d --remove-orphans

    [Install]
    WantedBy=multi-user.target
    5.复制 docker_boot.service到 systemd
    sudo cp -v ./myapp/docker_boot.service /etc/systemd/system
    6.启用并启动 docker_boot.service
    sudo systemctl enable docker_boot.service
    sudo systemctl start docker_boot.service
    注:第一个 start可能需要一些时间,因为它会拉取所有需要的 docker 镜像。或者,首先手动启动 docker-compose 以避免这种情况。
    7.检查 docker_boot.service的状态
    sudo systemctl status docker_boot.service
    8.检查wordpress是否up
    curl -L localhost:8000
    9. 重启
    检查 docker_boot.service通过登录实例并使用 sudo systemctl status docker_boot.service 在实例重启后运行和/或 curl -L localhost:8000 .

    关于amazon-web-services - 启动 ec2 服务器时自动启动 docker,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63317771/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com