pycontw / pycon.tw

PyCon TW Proposal System & Backend Server
https://tw.pycon.org/prs
MIT License
94 stars 107 forks source link

[FEAT] merge prod and dev Dockerfile #1194

Closed mhsiungw closed 1 month ago

mhsiungw commented 2 months ago

Types of changes

Description

Merge both dev and prod Dockerfile into one and divide it into multi stages.

Screenshot 2024-05-09 at 13 25 48

Steps to Test This Pull Request

Test dev stage

  1. delete related containers and images

    docker container rm ${USER}_pycontw_vm && docker image rm pycontw-pycontw
  2. run the following command

    ./enter_dev_env.sh
  3. start server

    python manage.py runserver 0.0.0.0:8000

Test prod stage

  1. create docker-compose-prod-demo.yml file

    
    version: "3.5"
    services:
    db:
    container_name: db_dev
    image: postgres:11-alpine
    environment:
      - POSTGRES_DB=pycontw2016
      - POSTGRES_USER=postgres
      - POSTGRES_PASSWORD=secretpostgres
    ports:
      - ${COMPOSE_DB_PORT:-5432}:5432
    networks:
        - network-2024
    web:
    build:
      context: .
      target: prod
    container_name: pycontw-2024
    hostname: pycontw-2024
    entrypoint: ""
    ports:
      - 8000:8000
    command:
      - bash
      - -c
      - |
        set -o errexit -o nounset -o pipefail
        python3 manage.py compilemessages
        python3 manage.py migrate
        python3 manage.py collectstatic --no-input
    
        exec uwsgi --http-socket :8000 \
          --master \
          --hook-master-start "unix_signal:15 gracefully_kill_them_all" \
          --static-map /static=assets \
          --static-map /media=media \
          --mount /prs=pycontw2016/wsgi.py \
          --manage-script-name \
          --offload-threads 2
    restart: always
    environment:
      DJANGO_SETTINGS_MODULE: pycontw2016.settings.production.pycontw2024
      SCRIPT_NAME: /prs
      SECRET_KEY: ${SECRET_KEY}
      DATABASE_URL: ${DATABASE_URL}
      EMAIL_URL: ${EMAIL_URL}
      GTM_TRACK_ID: ${GTM_TRACK_ID}
      SLACK_WEBHOOK_URL: ${SLACK_WEBHOOK_URL}
    
    volumes:
      - ${MEDIA_ROOT}:/usr/local/app/src/media
    networks:
        - network-2024

networks: network-2024: name: network-2024

2. run the following command
```bash
docker-compose -f ./docker-compose-prod-demo.yml up --build
  1. visit http://localhost:8000/

More Information

  1. remove ENV PATH /usr/local/bin:$PATH due to duplicate entries /usr/local/bin
  2. I also remove ENV PATH /home/docker/.local/bin:$PATH to centralise where pip install packages and to align the PATH between production environment and development environment.
  3. I also remove chown -R docker:nogroup "$BASE_DIR" "$APP_DIR" and use COPY --chown instead to avoid unnecessary image size increasing. references: https://garbers.co.za/2017/11/15/reduce-docker-image-size-chmod-chown/ , https://stackoverflow.com/questions/30085621/why-does-chown-increase-size-of-docker-image
codecov[bot] commented 2 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 73.99%. Comparing base (53b0638) to head (0fa2454). Report is 31 commits behind head on master.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1194 +/- ## ========================================== + Coverage 71.19% 73.99% +2.79% ========================================== Files 84 81 -3 Lines 3451 3057 -394 ========================================== - Hits 2457 2262 -195 + Misses 994 795 -199 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

SivanYeh commented 2 months ago

LGTM @SivanYeh would you please try to deploy this branch on staging and check if everything works fine 🙏🏽

Count on me! I will do this tomorrow and tell you guys how it goes

SivanYeh commented 2 months ago

LGTM @SivanYeh would you please try to deploy this branch on staging and check if everything works fine 🙏🏽

Count on me! I will do this tomorrow and tell you guys how it goes

It works well! No error were found during docker build, and websites on staging are functioning. I attach docker logs here:docker_20240510.log

If no further issues are found, and staging remains stable throughout the weekend, I will merge to main branch and deploy to prod.

mattwang44 commented 1 month ago

@SivanYeh I believe we can merge this, right?

SivanYeh commented 1 month ago

@SivanYeh I believe we can merge this, right?

Yes! Sorry for the delay. I was quite busy yesterday