Skip to content

Commit

Permalink
Merge pull request #129 from bcgov/70-backend-docker
Browse files Browse the repository at this point in the history
feat: Dockerfile for backend
  • Loading branch information
pbastia authored Feb 7, 2024
2 parents 5e21267 + 8f56554 commit ca3a062
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 8 deletions.
15 changes: 11 additions & 4 deletions .github/workflows/build-and-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@ on:
workflow_dispatch:

jobs:
frontend-docker-build:
docker-build:
strategy:
matrix:
include:
- image: cas-reporting-backend
context: server
- image: cas-reporting-frontend
context: client
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -21,7 +28,7 @@ jobs:
id: meta
uses: docker/metadata-action@v5
with:
images: ghcr.io/bcgov/cas-reporting-frontend
images: ghcr.io/bcgov/${{ matrix.image }}
tags: |
type=sha,format=long,prefix=
latest
Expand All @@ -35,10 +42,10 @@ jobs:
- name: Build image
uses: docker/build-push-action@v5
with:
context: client
context: ${{ matrix.context }}
builder: ${{ steps.buildx.outputs.name }}
push: true
file: client/Dockerfile
file: ${{ matrix.context }}/Dockerfile
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
Expand Down
7 changes: 7 additions & 0 deletions server/.dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.venv
.git
.gitignore
.vscode
**/__pycache__
**/*.pyc
python_cache
2 changes: 1 addition & 1 deletion server/.tool-versions
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
python 3.9.18
poetry 1.6.1
poetry 1.7.1
postgres 14.0
52 changes: 52 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Use an official Python runtime as a parent image
FROM python:3.9.18 AS main

# Install system dependencies and clean up
RUN apt-get update && \
apt-get upgrade -y && \
apt-get install -y git gnupg curl build-essential && \
apt-get clean

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
USER_ID=1001 \
HOME=/root

WORKDIR ${HOME}
# Make everything in the home group-writable to support OpenShift's restricted SCC
# Needs to be done as root to chown
RUN useradd -ms /bin/bash -d ${HOME} --uid ${USER_ID} -g root server

COPY ./ ${HOME}/

# Install asdf
RUN git clone https://github.com/asdf-vm/asdf.git ${HOME}/asdf --depth 1 --branch v0.14.0

ENV BASH_ENV="${HOME}/asdf/asdf.sh"
# Because asdf is loaded via BASH_ENV, all commands using adsf need to be executed using /usr/bin/env bash -c
SHELL ["/usr/bin/env", "bash", "-c"]

RUN sed -i -nr '/python|poetry/p' ${HOME}/.tool-versions && \
cat ${HOME}/.tool-versions | cut -f 1 -d ' ' | xargs -n 1 asdf plugin-add && \
asdf plugin-update --all && \
asdf install && \
asdf reshim

# Add Poetry's bin directory to PATH
ENV PATH="${HOME}/.asdf/installs/poetry/1.7.1/bin:${PATH}"

# Configuring poetry to behave properly with asdf
RUN poetry config virtualenvs.prefer-active-python true

# Change ownership and permissions
RUN chown -R server:0 ${HOME} && \
chmod -R g+rwX ${HOME}

# Expose the port your Django application will run on (change as needed)
EXPOSE 8000

# Install project dependencies using Poetry
RUN poetry install --without dev
USER ${USER_ID}
CMD ["/usr/bin/env", "bash", "-c", "poetry run python manage.py collectstatic --noinput && poetry run python manage.py migrate && poetry run gunicorn --access-logfile - server.wsgi:application --timeout 200 --workers 8 --bind '0.0.0.0:8000'"]
38 changes: 36 additions & 2 deletions server/poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions server/pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ django-ninja = "^0.22.2"
python-dotenv = "^1.0.0"
psycopg = "^3.1.10"
psycopg-binary = "^3.1.10"
whitenoise = "^6.6.0"
gunicorn = "^21.2.0"

[tool.poetry.group.dev.dependencies]
black = "^24.1.1"
Expand Down
9 changes: 8 additions & 1 deletion server/server/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
Expand Down Expand Up @@ -123,9 +124,15 @@
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.2/howto/static-files/

STATIC_URL = 'static/'
STATIC_URL = os.environ.get("STATIC_URL", "/static/")
STATIC_ROOT = os.path.join(BASE_DIR, "staticfiles")

# Default primary key field type
# https://docs.djangoproject.com/en/4.2/ref/settings/#default-auto-field

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'


STORAGES = {
"staticfiles": {"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage"},
}

0 comments on commit ca3a062

Please sign in to comment.