#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
은 위와 같이 작성했다.
github action 배포 설정에 대한 yml이다.
작업 순서는 다음과 같다.
docker-compose.yml
파일 복사 (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