[Docker] Docker compose로 Django, Nginx 띄우기 - 3 (docker-compose.yml, deploy.yml)

JinUk Lee·2024년 10월 16일
0

Docker

목록 보기
8/10
post-custom-banner

docker-compose.yml

#docker-compose.yml

services:
  backend: #컨테이너 이름
    build:
      context: ./djangobase2 # Dockerfile 위치
      dockerfile: Dockerfile
    image: mechauk418/backend:latest #이미지 이름
    ports:
      - "8000:8000"

    command: gunicorn config.wsgi:application --bind 0.0.0.0:8000
  
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    image: mechauk418/nginx:latest
    ports:
      - "80:80"
    depends_on:
      - backend

    command: [nginx-debug, '-g', 'daemon off;']

docker-compose.yml은 위와 같이 작성했다.

deploy.yml

github action 배포 설정에 대한 yml이다.

작업 순서는 다음과 같다.

  1. 도커 허브 로그인
  2. .env 파일 생성
  3. docker-compose.yml 파일 복사 (SSH 접속)
  4. 도커 실행 (SSH 접속)

name: Docekr CI

on:
  push:
    branches: [master]

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: checkout
        uses: actions/checkout@v4
        
      - name: Login Dockerhub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_PASSWORD }}

      - name: Generate Environment Variables File for Production
        run: |
          touch ./djangobase2/.env
          echo AWS_ACCESS_KEY_ID=${{secrets.AWS_ACCESS_KEY_ID}} >> ./djangobase2/.env
          echo AWS_SECRET_ACCESS_KEY=${{secrets.AWS_SECRET_ACCESS_KEY}} >> ./djangobase2/.env
          echo AWS_STORAGE_BUCKET_NAME=${{secrets.AWS_STORAGE_BUCKET_NAME}} >> ./djangobase2/.env
          echo DEBUG=${{secrets.DEBUG}} >> ./djangobase2/.env
          echo DATABASE_HOST=${{secrets.DATABASE_HOST}} >> ./djangobase2/.env
          echo DATABASE_PASSWORD=${{secrets.DATABASE_PASSWORD}} >> ./djangobase2/.env
          echo DATABASE_NAME=${{secrets.DATABASE_NAME}} >> ./djangobase2/.env
          echo REST_API_KEY=${{secrets.REST_API_KEY}} >> ./djangobase2/.env
          echo GOOGLE_CLIENT=${{secrets.GOOGLE_CLIENT}} >> ./djangobase2/.env
          echo GOOGLE_PASSWORD=${{secrets.GOOGLE_PASSWORD}} >> ./djangobase2/.env
          echo STATE=${{secrets.STATE}} >> ./djangobase2/.env
          echo SECRET_KEY=${{secrets.SECRET_KEY}} >> ./djangobase2/.env
          cat ./djangobase2/.env

      - name: Build the Docker images
        run: docker compose build --no-cache
      - name: Push the Docker images
        run: docker compose push
      - name: copy file via ssh password
        uses: appleboy/scp-action@master
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_KEY }}
          port: ${{ secrets.SSH_PORT }}
          source: "docker-compose.yml"
          target: "/home/ubuntu"

      - name: executing remote ssh commands using password
        uses: appleboy/ssh-action@v1.0.3
        with:
          host: ${{ secrets.SSH_HOST }}
          username: ${{ secrets.SSH_USERNAME }}
          key: ${{ secrets.SSH_KEY }}
          port: ${{ secrets.SSH_PORT }}
          script: |
            sudo docker stop $(sudo docker ps -a -q)
            sudo docker rm $(sudo docker ps -a -q)
            sudo docker compose pull
            sudo docker compose up -d
            sudo docker image prune -f
profile
개발자 지망생
post-custom-banner

0개의 댓글