pycon.tw icon indicating copy to clipboard operation
pycon.tw copied to clipboard

[FEAT] merge prod and dev Dockerfile

Open mhsiungw opened this issue 9 months ago • 3 comments

Types of changes

  • [x] Refactoring

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
  1. run the following command
./enter_dev_env.sh
  1. 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

  1. run the following command
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

mhsiungw avatar May 05 '24 06:05 mhsiungw

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
@@            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.

codecov[bot] avatar May 07 '24 05:05 codecov[bot]

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 avatar May 09 '24 09:05 SivanYeh

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.

SivanYeh avatar May 10 '24 05:05 SivanYeh

@SivanYeh I believe we can merge this, right?

mattwang44 avatar May 14 '24 02:05 mattwang44

@SivanYeh I believe we can merge this, right?

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

SivanYeh avatar May 14 '24 03:05 SivanYeh