Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] merge prod and dev Dockerfile #1194

Merged
merged 8 commits into from
May 14, 2024

Conversation

mhsiungw
Copy link
Contributor

@mhsiungw mhsiungw commented May 5, 2024

Types of changes

  • 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:https://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

Dockerfile Show resolved Hide resolved
docker-compose.yml Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
Dockerfile Outdated Show resolved Hide resolved
Dockerfile Show resolved Hide resolved
Dockerfile Show resolved Hide resolved
Copy link

codecov bot commented May 7, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

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     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Dockerfile Outdated Show resolved Hide resolved
@mattwang44 mattwang44 self-requested a review May 8, 2024 10:11
Copy link
Member

@mattwang44 mattwang44 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@SivanYeh
Copy link
Collaborator

SivanYeh commented May 9, 2024

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
Copy link
Collaborator

SivanYeh commented May 10, 2024

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
Copy link
Member

@SivanYeh I believe we can merge this, right?

@SivanYeh
Copy link
Collaborator

@SivanYeh I believe we can merge this, right?

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

@SivanYeh SivanYeh merged commit 208b988 into master May 14, 2024
3 checks passed
@SivanYeh SivanYeh deleted the feature/merge-dockerfile-into-one branch May 14, 2024 03:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants