Skip to content

Commit

Permalink
feat(images): refactored to use single Dockerfile
Browse files Browse the repository at this point in the history
  • Loading branch information
thejoeejoee committed Nov 11, 2023
1 parent c11e686 commit 7290153
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 77 deletions.
74 changes: 38 additions & 36 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
#
# webpack image
#
FROM node:18.15.0-slim as webpack-base

RUN apt-get update && apt-get install -y python python3 gcc g++ make build-essential && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

COPY ./webpack/package.json ./webpack/yarn.lock ./
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn npm install yarn && yarn install

COPY ./webpack/ /usr/src/app/
COPY ./fiesta/ /usr/src/fiesta/

CMD ["yarn", "run"]

# stable image
FROM webpack-base as webpack-stable

ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}

ARG BUILD_DIR=/usr/src/build
ENV BUILD_DIR=${BUILD_DIR}

ARG PUBLIC_PATH=/static/
ENV PUBLIC_PATH=${PUBLIC_PATH}

ARG TAILWIND_CONTENT_PATH="/usr/src/fiesta/**/templates/**/*.html:/usr/src/fiesta/**/*.py"
ENV TAILWIND_CONTENT_PATH=${TAILWIND_CONTENT_PATH}

RUN ["yarn", "build"]

#
# django web app image
#
Expand Down Expand Up @@ -77,43 +111,11 @@ ENV DJANGO_BUILD_DIR=${DJANGO_BUILD_DIR}
RUN bash -c "DJANGO_SECRET_KEY=\$RANDOM DJANGO_CONFIGURATION=LocalProduction python manage.py collectstatic --no-input"

# given by webpack compiled results
COPY ./webpack-stats.json $DJANGO_BUILD_DIR
COPY --from=webpack-stable /usr/src/build/webpack-stats.json ${DJANGO_BUILD_DIR}

# TODO: check opts https://www.uvicorn.org/#command-line-options
CMD ["python -m gunicorn fiesta.wsgi:application"]

#
# webpack image
#
FROM node:18.15.0-slim as webpack-base

RUN apt-get update && apt-get install -y python python3 gcc g++ make build-essential && rm -rf /var/lib/apt/lists/*

WORKDIR /usr/src/app

COPY ./webpack/package.json ./webpack/yarn.lock ./
RUN --mount=type=cache,target=/root/.yarn YARN_CACHE_FOLDER=/root/.yarn npm install yarn && yarn install

COPY ./webpack/ /usr/src/app/
COPY ./fiesta/ /usr/src/fiesta/

CMD ["yarn", "run"]

# stable image
FROM webpack-base as webpack-stable

ARG BUILD_DIR=/usr/src/build
ENV BUILD_DIR=${BUILD_DIR}

ARG PUBLIC_PATH=/static/
ENV PUBLIC_PATH=${PUBLIC_PATH}

ARG TAILWIND_CONTENT_PATH="/usr/src/fiesta/**/templates/**/*.html:/usr/src/fiesta/**/*.py"
ENV TAILWIND_CONTENT_PATH=${TAILWIND_CONTENT_PATH}

RUN ["yarn", "build"]


#
# proxy image
#
Expand All @@ -126,9 +128,9 @@ COPY ./nginx/nginx.conf.template /etc/nginx/templates/

FROM proxy-base as proxy-stable

# propared by webpack and web builds during CD
COPY webpack-build/ /var/build/
COPY web-static/ /var/static/
# prepared by webpack and web builds during CD
COPY --from=webpack-stable /usr/src/build/ /var/build/
COPY --from=web-stable /usr/src/static/ /var/static/

ARG STATIC_LOCATION_PATTERN="^/static/(.*)$$"
ENV STATIC_LOCATION_PATTERN=${STATIC_LOCATION_PATTERN}
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ up: DC_CMD = up
up: dc ## Runs all needed docker containers

produp: ## Runs fiesta in (local)production mode.
$(DC) -f docker-compose.yml -f docker-compose.prod.yml --profile prod up --build
$(DC) -f docker-compose.yml -f docker-compose.prod.yml --profile prod up

psql: DC_CMD = run --entrypoint bash db -c "PGPASSWORD=fiesta psql --host db --user fiesta --dbname fiesta"
psql: dc ## Runs psql shell in database
Expand Down
9 changes: 9 additions & 0 deletions docker-compose.override.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,29 @@ services:
volumes:
- ./fiesta/:/usr/src/app/
- ./conf/certs:/usr/share/certs
- web_static_files:/usr/src/static
- web_media_files:/usr/src/media
- webpack_build:/usr/src/build
build:
args:
- POETRY_EXPORT_ARGS=--dev

proxy:
volumes:
- ./nginx/nginx.conf.template:/etc/nginx/templates/nginx.conf.template
- web_static_files:/var/static
- webpack_build:/var/build

webpack:
environment:
PUBLIC_PATH: //webpack.${ROOT_DOMAIN}/static/
NODE_ENV: development
volumes:
- ./webpack/:/usr/src/app
# since tailwind need to access templates and all the stuff to dynamically strip classes
# see more in webpack/tailwind.config.js
- ./fiesta/:/usr/src/fiesta
- webpack_build:/usr/src/build

dockerproxy:
environment:
Expand Down
12 changes: 7 additions & 5 deletions docker-compose.prod.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
version: '3.3'
services:
web:
command: [ sh, "-c", "python manage.py collectstatic && python manage.py runserver 0.0.0.0:8000" ]
build:
# collectstatic done be dockerfile
target: web-stable
environment:
DJANGO_CONFIGURATION: LocalProduction

proxy:
build:
target: proxy-stable
command: [ nginx, '-g', 'daemon off;' ]
environment:
STATIC_LOCATION_PATTERN: '^/static/(.*)$$'

webpack:
command: yarn build
environment:
NODE_ENV: production
PUBLIC_PATH: https://${ROOT_DOMAIN}/static/
profiles:
- do-not-start

dockerproxy:
ports:
Expand Down
60 changes: 25 additions & 35 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@ version: '3.3'
services:
web:
command: python manage.py runserver 0.0.0.0:8000
volumes:
- web_static_files:/usr/src/static
- web_media_files:/usr/src/media
- webpack_build:/usr/src/build
expose:
- 8000
env_file:
Expand All @@ -21,20 +17,6 @@ services:
VIRTUAL_HOST: web.${ROOT_DOMAIN}
VIRTUAL_PORT: 8000

db:
image: postgres:15-alpine3.17
volumes:
- postgres15_data:/var/lib/postgresql/data/
ports:
- "127.0.0.1:15432:5432"
expose:
- 5432
environment:
POSTGRES_USER: fiesta
POSTGRES_PASSWORD: fiesta
POSTGRES_DB: fiesta
TZ: Europe/Prague

proxy:
build:
context: .
Expand All @@ -46,9 +28,7 @@ services:
depends_on:
- web
volumes:
- web_static_files:/var/static
- web_media_files:/var/media
- webpack_build:/var/build
- wiki_static_files:/var/wiki
- /var/run/docker.sock:/var/run/docker.sock
environment:
Expand All @@ -57,18 +37,19 @@ services:
VIRTUAL_HOST: "*.${ROOT_DOMAIN},${ROOT_DOMAIN}"
VIRTUAL_PORT: 80

dockerproxy:
build:
context: ./nginx
dockerfile: dockerproxy.Dockerfile
logging:
driver: none
ports:
- "80:80"
- "443:443"
- "3306:3306"
db:
image: postgres:15-alpine3.17
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
- postgres15_data:/var/lib/postgresql/data/
ports:
- "127.0.0.1:15432:5432"
expose:
- 5432
environment:
POSTGRES_USER: fiesta
POSTGRES_PASSWORD: fiesta
POSTGRES_DB: fiesta
TZ: Europe/Prague

webpack:
build:
Expand All @@ -78,17 +59,26 @@ services:
command: yarn dev
expose:
- 8003
volumes:
- webpack_build:/usr/src/build
env_file:
- ./webpack/.env.base
environment:
VIRTUAL_HOST: webpack.${ROOT_DOMAIN}
VIRTUAL_PORT: 8003
BUILD_DIR: /usr/src/build
NODE_ENV: development
ROOT_DOMAIN: ${ROOT_DOMAIN}
PUBLIC_PATH: //webpack.${ROOT_DOMAIN}/static/

dockerproxy:
build:
context: ./nginx
dockerfile: dockerproxy.Dockerfile
logging:
driver: none
ports:
- "80:80"
- "443:443"
- "3306:3306"
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro

legacydb:
image: mariadb:10.4
Expand Down
2 changes: 2 additions & 0 deletions fiesta/fiesta/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class LocalProduction(Base):

ROOT_DOMAIN = "fiesta.test"

USE_WEBPACK_INTEGRITY = False


class Production(S3ConfigMixin, Base):
DEBUG = False
Expand Down

0 comments on commit 7290153

Please sign in to comment.