티스토리 뷰

반응형

docker 잘 모름, 근데 묶어서 관리하고 실행하기 편할것 같아서 진행

 

 

1. docker desktop download

2. Dockerfile 생성 및 작성, requirements.txt에는 패키지

#requirements.txt
uvicorn
jinja2
fastapi
sqlalchemy
pymssql
pyodbc
redis
celery
eventlet
alembic
pytest
websockets

 

#docker-compose.yml
version: '3'

services:
  web:
    build: .
    ports:
      - "8000:8000"
    depends_on:
      - redis
  celery_worker:
    build: .
    command: celery -A celery_worker.celery worker --loglevel=info
    depends_on:
      - redis
  redis:
    image: redis:alpine
#Dockerfile
FROM python:3.11

WORKDIR /app

COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

 

3. 서버 경로에서 cmd -> build

docker build -t message-server .

4. docker destop의 이미지에서 실행 끝

 

이제 nginx랑 certbot 등 추가할지 말지 고민좀 해봐야겠다.

 

생각보다 세팅이 더 변경됨

nginx + certbot 추가 했음, 근데 nginx에서 ssl을 사용하려면 인증서를 먼저 발급을 받고 설정해줘야해서

nginx를 먼저 켜고 certbot으로 인증서 발급 후 nginx.conf에서 ssl server block을 다시 주석을 풀고 reload해주면 됨

docker-compose run --rm --entrypoint "" certbot certbot certonly --webroot -w /var/www/certbot -d domain.com --email youremail@gmail.com --agree-tos --no-eff-email

 

entrypoint ""을 준 이유는 내 docker-compose에서 certbot으로 재발급 command가 들어가 있어서 비워주지 않으면 발급 절차가 아닌 renew를 해버림

  nginx:

    image: nginx:alpine
    restart: always
    network_mode: "host"
    # ports:
    #   - '80:80'
    #   - '443:443' #SSL 인증서 발급 전 주석
    volumes:
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf:ro
      - ./nginx/conf.d:/etc/nginx/conf.d:ro
      - ./certbot/conf:/etc/letsencrypt
      - ./certbot/www:/var/www/certbot
    command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
    depends_on:
      - message_app
      - manage_app

  certbot:
    image: certbot/certbot
    volumes:
      - ./certbot/conf:/etc/letsencrypt
      - ./certbot/www:/var/www/certbot
    entrypoint: "/bin/sh -c 'trap exit TERM; while :; do certbot renew; sleep 12h & wait $${!}; done;'"

 

원래 windows에서 network mode를 host로 둘 수 없었는데 최근에 추가된 것 같아서 설정 해봤음

근데 설정을 해도 real ip가 제대로 나오지 않아서 유기함, 내가 host 설정을 알아본 이유가 real ip였기 때문

그래서 ubuntu로 옮겨서 진행중이고, ubuntu에서는 network_mode :host -> real ip 잘 나옴

- 근데 ubuntu에서 docker와 compose를 설치하고 docker-compose up을 치면 안되고 docker compose up을 하면 됨

 

이제 real ip를 제대로 확인할 수 있어서 nginx에서 allow랑 deny 설정으로 allow 사설망; deny all; 해줘서 내부에서만 일단 접속되도록 해놨음

 

network mode를 host로 두면 다른 fastapi app에서 redis 경로를 redis에서 localhost로 잡아주면 됨

celery는 host가 아니라 redis 그대로 둠

그리고 코드가 있고, container에서 실행될 때는 redis를 알아먹는데, 직접 코드를 디버깅할때는 redis가 pc에 설치되어 있어서 예외처리?를 해놨음

 

docker-compose.yml에 celery 서비스의 environment에 - REDIS_HOST=redis로 넣어줬고, 코드상에서는

redis_host = os.getenv("REDIS_HOST", "localhost") 로 해서 redis_host env가 있으면 가져오고 없으면 default로 localhost를 사용하도록

 

이제 각 fastapi app 이미지를 alpine으로 바꿔서 테스트 해보는정도 남은것 같음

댓글

티스토리 방명록

최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday