pycon.tw
pycon.tw copied to clipboard
[FEAT] merge prod and dev Dockerfile
Types of changes
- [x] Refactoring
Description
Merge both dev and prod Dockerfile into one and divide it into multi stages.
Steps to Test This Pull Request
Test dev stage
- delete related containers and images
docker container rm ${USER}_pycontw_vm && docker image rm pycontw-pycontw
- run the following command
./enter_dev_env.sh
- start server
python manage.py runserver 0.0.0.0:8000
Test prod stage
- 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
- run the following command
docker-compose -f ./docker-compose-prod-demo.yml up --build
- visit http://localhost:8000/
More Information
- remove
ENV PATH /usr/local/bin:$PATH
due to duplicate entries/usr/local/bin
- 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. - I also remove
chown -R docker:nogroup "$BASE_DIR" "$APP_DIR"
and useCOPY --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 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.
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
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 I believe we can merge this, right?
@SivanYeh I believe we can merge this, right?
Yes! Sorry for the delay. I was quite busy yesterday