Running docker containers from a system service

  Kiến thức lập trình

I have a django web app that runs in as three docker images. One is a cronjob, one is the web app and one is the nginx service. I have separate build and deploy areas on the same Ubuntu system I deploy to /opt/dlnWebApplication. I can build the save tars files in the dev rea. I copy the tar file to /opt. Load the tar file and do a docker-compose up -d and everything run like a champ. In case of a system reboot or other oddity, I was trying to find a good way to make sure that the images were restarted. I have a dlnWebApplication.service file, but when I start the service it is currently trying to build the images. It cannot do that as the necessary files are in the build area. Here is my docker-compose.yml file:

services:
  web:
    image: dlnproject-web:latest
    container_name: ${WEB_CONTAINER_NAME}
    build:
      context: ./dlnWebApplication
    volumes:
      - ./db:/app/db  # Mount the new db directory containing db.sqlite3
    env_file:
      - .env
    environment:
      - DJANGO_SETTINGS_MODULE=dlnWebApplication.settings
      - DATABASE_PATH=/app/db/db.sqlite3  # Correct path for both services

  cron:
    image: dlnproject-cron:latest
    container_name: ${CRON_CONTAINER_NAME}
    build:
      context: ./dlnEquipmentSnooper
    volumes:
      - ./db:/app/db  # Mount the new db directory containing db.sqlite3
      - ./logs/dlnEquipmentSnooper:/var/log/dlnEquipmentSnooper  # Map log directory to host
    depends_on:
      - web
    env_file:
      - .env
    environment:
      - DJANGO_SETTINGS_MODULE=dlnWebApplication.settings
      - DATABASE_PATH=/app/db/db.sqlite3  # Correct path for both services

  nginx:
    image: dlnproject-nginx:latest
    container_name: ${NGINX_CONTAINER_NAME}
    build:
      context: ./nginx  # Use custom Dockerfile for nginx
    volumes:
      - ./dlnWebApplication/static:/app/static
      - ./dlnWebApplication/media:/app/media
      - ./logs:/var/log/nginx
    ports:
      - "80:80"
    depends_on:
      - web

volumes:
  sqlite_data:

And my service file:

[Unit]
Description=dlnWebApplication Service
After=network.target docker.service

[Service]
WorkingDirectory=/opt/dlnWebApplication
ExecStart=/usr/bin/docker-compose -f /opt/dlnWebApplication/docker-compose.yml up -d
ExecStop=/usr/bin/docker-compose -f /opt/dlnWebApplication/docker-compose.yml down
Restart=always
RestartSec=5
User=dlnwebapp
Group=dlnserverusers

[Install]
WantedBy=multi-user.target

Why won’t this work?
Anyone with ideas would be greatly appreciated.
Doug

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT