Skip to content

Commit

Permalink
Merge pull request #1786 from wger-project/feature/improved-docker-image
Browse files Browse the repository at this point in the history
Improve docker image
  • Loading branch information
rolandgeider authored Oct 6, 2024
2 parents 4600c70 + a43c3af commit 52632b3
Show file tree
Hide file tree
Showing 14 changed files with 91 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ updates:
interval: "daily"

- package-ecosystem: "docker"
directory: "/extras/docker/development/"
directory: "/extras/docker/production/"
schedule:
interval: "daily"
5 changes: 1 addition & 4 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:

env:
REGISTRY_IMAGE: ${{ vars.REGISTRY_REPO }}/server
REGISTRY_IMAGE_ALT: ${{ vars.REGISTRY_REPO }}/devel

jobs:
build-prod:
Expand Down Expand Up @@ -42,7 +41,6 @@ jobs:
type=raw,value=2.3-dev
images: |
${{ env.REGISTRY_IMAGE }}
${{ env.REGISTRY_IMAGE_ALT }}
- name: Set up QEMU
uses: docker/[email protected]
Expand Down Expand Up @@ -72,7 +70,7 @@ jobs:
uses: docker/build-push-action@v6
with:
context: .
file: extras/docker/development/Dockerfile
file: extras/docker/production/Dockerfile
builder: ${{ steps.buildx.outputs.name }}
platforms: ${{ matrix.platform }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down Expand Up @@ -126,7 +124,6 @@ jobs:
type=raw,value=2.3-dev
images: |
${{ env.REGISTRY_IMAGE }}
${{ env.REGISTRY_IMAGE_ALT }}
- name: Login to Docker Hub
uses: docker/login-action@v3
Expand Down
19 changes: 9 additions & 10 deletions extras/docker/base/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,32 @@
# docker build --tag wger/base .
#

FROM ubuntu:22.04
FROM ubuntu:24.04

LABEL maintainer="Roland Geider <[email protected]>"

# Install dependencies
ENV DEBIAN_FRONTEND=noninteractive
RUN apt update \
&& apt install --no-install-recommends -y \
adduser \
locales \
nodejs \
npm \
python3-pip \
sqlite3 \
wget \
tzdata \
libpq5 \
&& npm install -g yarn sass\
&& locale-gen en_US.UTF-8

# Environmental variables
ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8
ENV LANG=en_US.UTF-8
ENV LANGUAGE=en_US:en
ENV LC_ALL=en_US.UTF-8

ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1


# Add wger user
RUN adduser wger --disabled-password --gecos ""
RUN deluser ubuntu
RUN adduser wger --uid 1000 --disabled-password --gecos ""
52 changes: 31 additions & 21 deletions extras/docker/demo/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
##########
# Builder
##########
FROM wger/base:latest as builder
FROM wger/base:latest AS builder
ARG DEBIAN_FRONTEND=noninteractive

RUN apt update \
Expand All @@ -32,20 +32,29 @@ RUN apt update \
libpq-dev \
rustc \
cargo \
&& pip install --upgrade pip
yarnpkg \
sassc

# Build the necessary python wheels
# Note that the --mount is a workaround for https://github.com/rust-lang/cargo/issues/8719
COPY requirements* ./
RUN --mount=type=tmpfs,target=/root/.cargo pip3 wheel --no-cache-dir --wheel-dir /wheels -r requirements_docker.txt
RUN --mount=type=tmpfs,target=/root/.cargo pip3 wheel --no-cache-dir --wheel-dir /wheels -r requirements_docker.txt \
&& pip3 install --break-system-packages --user --no-cache-dir /wheels/*

COPY . /home/wger/src
WORKDIR /home/wger/src
RUN ln -s /usr/bin/yarnpkg /usr/bin/yarn \
&& ln -s /usr/bin/sassc /usr/bin/sass \
&& yarn install \
&& yarn build:css:sass


########
# Final
########
FROM wger/base:latest
LABEL maintainer="Roland Geider <[email protected]>"

ENV TERM=xterm
ARG DOCKER_DIR=./extras/docker/demo
EXPOSE 80

Expand All @@ -70,45 +79,46 @@ RUN a2dissite 000-default.conf \
# Configure cron
COPY ${DOCKER_DIR}/crontab /etc/cron.d/wger
COPY ${DOCKER_DIR}/venvwrapper /home/wger/venvwrapper
COPY ${DOCKER_DIR}/entrypoint.sh /home/wger/entrypoint.sh

RUN chmod 0644 /etc/cron.d/wger \
&& chmod +x /home/wger/venvwrapper /home/wger/entrypoint.sh \
&& chmod +x /home/wger/venvwrapper \
&& touch /var/log/cron.log

COPY --from=builder /wheels /wheels
COPY --chown=wger:www-data . /home/wger/src
COPY --from=builder --chown=wger:wger /home/wger/src/wger/core/static/yarn /home/wger/src/wger/core/static/yarn

# Set up the application
RUN ln -s /home/wger/static/CACHE /var/www
USER wger

WORKDIR /home/wger/src
#RUN git clone https://github.com/wger-project/wger.git
RUN python3 -m venv /home/wger/venv

# Change permissions of some files and folders so the apache process
# can access them.
RUN mkdir -p ~/static/CACHE ~/media \
&& ln -s /home/wger/static/CACHE /home/wger/src/CACHE \
&& chmod g+w /home/wger/static/CACHE

RUN . /home/wger/venv/bin/activate \
&& pip install --upgrade pip \
&& pip install --no-cache /wheels/* \
&& pip install -e . \
&& wger create-settings --database-path /home/wger/db/database.sqlite \
&& wger bootstrap \
&& wger load-online-fixtures \
&& sed -i "/^MEDIA_ROOT/c\MEDIA_ROOT='\/home\/wger\/media'" settings.py \
&& echo STATIC_ROOT=\'/home/wger/static\' >> settings.py \
&& wger bootstrap --no-process-static \
&& python3 manage.py sync-exercises \
&& python3 manage.py download-exercise-images
&& wger load-online-fixtures \
&& python3 manage.py download-exercise-images \
&& python3 manage.py collectstatic --no-input


# Change permissions of some files and folders so the apache process
# can access them.
RUN mkdir -p ~/static/CACHE ~/media \
&& ln -s /home/wger/static/CACHE /home/wger/src/CACHE \
&& chmod g+w /home/wger/static/CACHE \
&& sed -i "/^MEDIA_ROOT/c\MEDIA_ROOT='\/home\/wger\/media'" settings.py \
&& echo STATIC_ROOT=\'/home/wger/static\' >> settings.py

USER root
RUN apt-get remove build-essential -y \
&& apt autoremove -y \
&& chown :www-data -R /home/wger/db \
RUN chown :www-data -R /home/wger/db \
&& chown www-data:www-data -R /home/wger/static \
&& chmod g+w /home/wger/db /home/wger/db/database.sqlite

ENTRYPOINT ["/home/wger/entrypoint.sh"]
CMD ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
14 changes: 0 additions & 14 deletions extras/docker/demo/entrypoint.sh

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#
# Docker image for wger development
# Docker image for wger
#
# Please consult the README for usage
#
# Note: you MUST build this image from the project's root!
# docker build -f extras/docker/development/Dockerfile --tag wger/server .
# docker build -f extras/docker/production/Dockerfile --tag wger/server .
#
# To run the container consult https://github.com/wger-project/docker

##########
# Builder
##########
FROM wger/base:latest as builder
FROM wger/base:latest AS builder
ARG DEBIAN_FRONTEND=noninteractive

RUN apt update \
Expand All @@ -25,23 +25,32 @@ RUN apt update \
libwebp-dev \
libpq-dev \
rustc \
cargo \
&& pip install --upgrade pip
yarnpkg \
sassc \
cargo

# Build the necessary python wheels
# Note that the --mount tmpfs is a workaround for https://github.com/rust-lang/cargo/issues/8719
COPY requirements* ./
RUN --mount=type=tmpfs,target=/root/.cargo \
pip3 wheel --no-cache-dir --wheel-dir /wheels -r requirements_docker.txt
pip3 wheel --no-cache-dir --wheel-dir /wheels -r requirements_docker.txt \
&& pip3 install --break-system-packages --user --no-cache-dir /wheels/*

# Download and copy js and css files
COPY . /home/wger/src
WORKDIR /home/wger/src
RUN ln -s /usr/bin/yarnpkg /usr/bin/yarn \
&& ln -s /usr/bin/sassc /usr/bin/sass \
&& yarn install \
&& yarn build:css:sass


########
# Final
########
FROM wger/base:latest
LABEL maintainer="Roland Geider <[email protected]>"
ARG DOCKER_DIR=./extras/docker/development
ARG DOCKER_DIR=./extras/docker/production
ENV PATH="/home/wger/.local/bin:$PATH"

EXPOSE 8000
Expand All @@ -50,25 +59,24 @@ EXPOSE 8000
# Set up the application
WORKDIR /home/wger/src
COPY --chown=wger:wger . /home/wger/src
COPY --from=builder /wheels /wheels
COPY --from=builder --chown=wger:wger /root/.local /home/wger/.local
COPY --from=builder --chown=wger:wger /home/wger/src/wger/core/static/yarn /home/wger/src/wger/core/static/yarn
COPY ${DOCKER_DIR}/settings.py /home/wger/src
COPY ${DOCKER_DIR}/settings.py /tmp/
COPY ${DOCKER_DIR}/entrypoint.sh /home/wger/entrypoint.sh
COPY ${DOCKER_DIR}/celery/beat/start /start-beat
COPY ${DOCKER_DIR}/celery/worker/start /start-worker
COPY ${DOCKER_DIR}/celery/flower/start /start-flower
COPY ${DOCKER_DIR}/celery/start-beat /start-beat
COPY ${DOCKER_DIR}/celery/start-worker /start-worker
COPY ${DOCKER_DIR}/celery/start-flower /start-flower
RUN chmod +x /home/wger/entrypoint.sh \
&& chmod +x /start-beat \
&& chmod +x /start-worker \
&& chmod +x /start-flower \
&& pip3 install --no-cache /wheels/* \
&& chown -R wger:wger .
&& chmod +x /start-flower

USER wger
RUN pip3 install -e . \
RUN pip3 install --break-system-packages --user -e . \
&& mkdir ~/media \
&& mkdir ~/static \
&& mkdir ~/beat \
&& mkdir ~/db/
&& mkdir ~/db

CMD ["/home/wger/entrypoint.sh"]
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,22 @@ A more comfortable development version is provided in the compose folder.

### 1 - Installing docker

Install docker, and the docker buildx tool (if they are separate packages on your system, e.g. on Arch Linux)
Install docker, and the docker buildx tool (if they are separate packages on your system, e.g. on
Arch Linux)

### 2 - Obtaining/building the docker image

We will run the `wger/server:latest` image in the next step.

You can either download it from [dockerhub](https://hub.docker.com/r/wger/server); docker will do this automatically if you have no such image with that tag yet.
You can also run `docker pull wger/server` to get the latest version. (you can use `docker images` to see if your image is old)
You can either download it from [dockerhub](https://hub.docker.com/r/wger/server); docker will do
this automatically if you have no such image with that tag yet.
You can also run `docker pull wger/server` to get the latest version. (you can use `docker images`
to see if your image is old)

Alternatively, you can build it yourself from your wger code checkout.
To do this, you **must** build from the project root!

```docker build -f extras/docker/development/Dockerfile --tag wger/server .```
```docker build -f extras/docker/production/Dockerfile --tag wger/server .```

### 3 - Start the container

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# Copy a settings file if nothing's found (e.g. when mounting a fresh checkout)
# This is a bit ugly, but it's needed since we use this image for development
# and "production".
# and production.
if [ ! -f /home/wger/src/settings.py ]; then
cp /tmp/settings.py /home/wger/src
fi
Expand All @@ -13,15 +13,22 @@ fi
# after a first checkout), repeat the process.
if [ ! -d "/home/wger/src/wger.egg-info" ];
then
pip3 install -e .
pip3 install --break-system-packages -e .
fi

# Bootstrap the application
# * Load the fixtures with exercises, ingredients, etc
# * Create an admin user
# * Download JS and CSS files
# * Compile custom bootstrap theme
wger bootstrap
# * (optionally) Download JS and CSS files
# * (optionally) Compile custom bootstrap theme

if [ "$YARN_PROCESS_STATIC" == "True" ]; then
yarn_static=""
else
yarn_static="--no-process-static"
fi

wger bootstrap $yarn_static

# Collect static files
if [ "$DJANGO_CLEAR_STATIC_FIRST" == "False" ]; then
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"yarn": "^1.22.22"
},
"scripts": {
"build:css:sass": "sass wger/core/static/scss/main.scss:wger/core/static/yarn/bootstrap-compiled.css"
"build:css:sass": "sass wger/core/static/scss/main.scss wger/core/static/yarn/bootstrap-compiled.css"
},
"engines": {
"yarn": ">= 1.0.0"
Expand Down
Loading

0 comments on commit 52632b3

Please sign in to comment.