0

Everytime I restart my EC2 server I have to do: sudo systemctl start docker and then docker-compose up -d to launch all my containers.
I would like to create a systemd to do that as suggested in this answer to automatically run these two commands at the start of the instance.

So far I have created a docker_boot.service in /etc/systemd/system/ with the following content:

[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

I don't know if the content of my docker_boot.service file is correct. Ideally I would like to do docker-compose down as well when turning off the instance.

I then did:

sudo systemctl enable docker
sudo systemctl enable docker_boot

But my docker images are not running when I restart the EC2 instance, how can I debug this?
Please find below the content of my docker-compose.yml file:

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
ML_Enthousiast
  • 1,147
  • 1
  • 15
  • 39

2 Answers2

1

To enable your docker daemon at startup simply run the following command:

sudo systemctl enable docker

To run the docker-compose up at boot-time you can add this line in your cont-tab. The absolute path to your compose file must be used.

@reboot /usr/bin/docker-compose -f /absolute-path-to-your/docker-compose.yml up
Neo Anderson
  • 5,957
  • 2
  • 12
  • 29
  • Thanks for your answer, so it is weird but following your instructions, it then only runs the container `certbot` and the container `shinyproxy`, would you know why looking at the `docker-compose.yml` ? – ML_Enthousiast Aug 19 '20 at 22:13
  • restart policy: on-failure – Neo Anderson Aug 19 '20 at 22:24
  • Thx. You can see `restart: on-failure` is already there for nginx image but still does not launch it. I added it to the other images but did not change anything. – ML_Enthousiast Aug 19 '20 at 22:42
  • Hello @ML_Enthousiast, sorry for the expedite answer earlier. I am on a road trip and I have no access to a computer for a few days. Try to debug step by step. Check if the cron job is really executed. Check the logs of the containers and try to find some clues. Be consistent with your restart policy(you could try to add `restart: always` on all containers, then I guess you'd no longer need the cron job) – Neo Anderson Aug 20 '20 at 05:31
1
  1. Run this command on the EC2 machine to enable your docker daemon at startup with:
sudo systemctl enable docker
  1. Add restart: always to your docker-compose.yaml file like this:
version: "3.1"
services:
 my-server:
   image: .../server:0.0.1
   restart: always
   ...
gshock
  • 584
  • 7
  • 18