-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1127 from open-dynaMIX/docker-setup
use multi stage docker build for api and push to registry
- Loading branch information
Showing
9 changed files
with
145 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
name: Container image | ||
|
||
on: | ||
release: | ||
types: [created] | ||
|
||
# Run build for any PRs - we won't push in those however | ||
pull_request: | ||
branches: | ||
- main | ||
|
||
# Publish `main` as Docker `dev` image. | ||
push: | ||
branches: | ||
- main | ||
|
||
concurrency: | ||
group: build-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
env: | ||
IMAGE_NAME: ${{ github.repository }} | ||
REGISTRY: ghcr.io | ||
|
||
jobs: | ||
# Push image to GitHub Packages. | ||
# See also https://docs.docker.com/build/ci/github-actions/ | ||
container-registry: | ||
runs-on: ubuntu-latest | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
include: | ||
- image: ${{ github.repository }} | ||
path: api | ||
- image: ${{ github.repository }}-caluma | ||
path: caluma | ||
permissions: | ||
packages: write | ||
contents: read | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v3 | ||
|
||
- name: Log in to the Container registry | ||
uses: docker/login-action@v3 | ||
if: github.event_name != 'pull_request' | ||
with: | ||
registry: ${{ env.REGISTRY }} | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Extract metadata (tags, labels) for Docker | ||
id: meta | ||
uses: docker/metadata-action@v5 | ||
with: | ||
images: ${{ matrix.image }} | ||
tags: | | ||
type=raw,value=dev,enable={{is_default_branch}} | ||
type=semver,pattern={{version}} | ||
type=semver,pattern={{major}}.{{minor}} | ||
type=semver,pattern={{major}} | ||
- name: Build and push | ||
uses: docker/build-push-action@v6 | ||
with: | ||
context: "{{defaultContext}}:${{ matrix.path }}" | ||
file: ./Dockerfile | ||
push: ${{ github.event_name != 'pull_request' }} | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} | ||
cache-from: type=gha | ||
cache-to: type=gha,mode=max |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,66 @@ | ||
FROM python:3.9 | ||
FROM python:3.12-alpine AS base | ||
|
||
# needs to be set for users with manually set UID | ||
ENV HOME=/home/mysagw | ||
RUN apk update --no-cache && \ | ||
apk upgrade --no-cache && \ | ||
apk add wait4ports shadow libpq-dev --no-cache && \ | ||
useradd -m -r -u 1001 mysagw && \ | ||
apk del shadow && \ | ||
rm -rf /var/cache/apk/* | ||
|
||
ENV PYTHONUNBUFFERED=1 | ||
ENV DJANGO_SETTINGS_MODULE mysagw.settings | ||
ENV APP_HOME=/app | ||
ENV DJANGO_SETTINGS_MODULE=mysagw.settings \ | ||
PYTHONFAULTHANDLER=1 \ | ||
PYTHONHASHSEED=random \ | ||
PYTHONDONTWRITEBYTECODE=1 \ | ||
PIP_NO_CACHE_DIR=off \ | ||
PIP_DISABLE_PIP_VERSION_CHECK=on \ | ||
PIP_DEFAULT_TIMEOUT=100 | ||
|
||
RUN mkdir -p /app \ | ||
&& useradd -u 901 -r mysagw --create-home \ | ||
# all project specific folders need to be accessible by newly created user but also for unknown users (when UID is set manually). Such users are in group root. | ||
&& chown -R mysagw:root /home/mysagw \ | ||
&& chmod -R 770 /home/mysagw \ | ||
&& apt-get update && apt-get install -y --no-install-recommends \ | ||
wait-for-it \ | ||
# needed for psycopg2 | ||
libpq-dev \ | ||
&& pip install -U poetry | ||
EXPOSE 8000 | ||
|
||
USER mysagw | ||
FROM base AS build | ||
|
||
WORKDIR $APP_HOME | ||
WORKDIR /app | ||
|
||
ARG INSTALL_DEV_DEPENDENCIES=false | ||
COPY pyproject.toml poetry.lock $APP_HOME/ | ||
RUN if [ "$INSTALL_DEV_DEPENDENCIES" = "true" ]; then poetry install --with dev; else poetry install; fi | ||
COPY . ./ | ||
|
||
COPY . $APP_HOME | ||
ENV POETRY_NO_INTERACTION=1 \ | ||
POETRY_VIRTUALENVS_CREATE=false | ||
|
||
EXPOSE 8000 | ||
RUN pip install -U poetry | ||
|
||
FROM build AS wheel | ||
|
||
WORKDIR /app | ||
|
||
RUN poetry build -f wheel && mv ./dist/*.whl /tmp/ && pip uninstall -y poetry | ||
|
||
FROM build AS dev | ||
|
||
WORKDIR /app | ||
|
||
RUN poetry install --no-root | ||
|
||
USER 1001 | ||
|
||
CMD [\ | ||
"/bin/sh", "-c", \ | ||
"wait4ports -s 15 tcp://${DATABASE_HOST:-db}:${DATABASE_PORT:-5432} && \ | ||
./manage.py migrate --no-input && \ | ||
./manage.py runserver 0.0.0.0:8000 -v 3" \ | ||
] | ||
|
||
FROM base AS prod | ||
|
||
COPY manage.py /usr/local/bin | ||
COPY --from=wheel /tmp/*.whl /tmp/ | ||
|
||
RUN pip install /tmp/*.whl && rm /tmp/*.whl | ||
|
||
USER 1001 | ||
|
||
CMD [\ | ||
"/bin/sh", "-c", \ | ||
"wait-for-it $DATABASE_HOST:${DATABASE_PORT:-5432} -- \ | ||
poetry run ./manage.py migrate && \ | ||
exec poetry run gunicorn --workers 10 --access-logfile - --limit-request-line 16384 --bind 0.0.0.0:8000 mysagw.wsgi" \ | ||
"wait4ports -s 15 tcp://${DATABASE_HOST:-db}:${DATABASE_PORT:-5432} && \ | ||
manage.py migrate --no-input && \ | ||
gunicorn --workers 10 --access-logfile - --limit-request-line 16384 --bind :8000 mysagw.wsgi" \ | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,3 @@ | ||
version: "3" | ||
|
||
services: | ||
|
||
db: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters