Skip to content

Commit

Permalink
Docker: improve caching & install numpy in container
Browse files Browse the repository at this point in the history
CI was building the image twice: once with dev dependencies and again
without. Separating the pipenv command into separate layers allows the
second build in CI to take advantage of the cache for the base
dependencies that it will share across both builds.

Install numpy along with the dev dependencies within the container.
Previously it was installed in CI only, but this meant extra work for
those running tests locally.

Install numpy to the correct site.
  • Loading branch information
MarkKoz committed Mar 8, 2021
1 parent 2240dde commit 0f604e5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/lint-test-build-push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ jobs:
push: false
load: true
target: venv
build-args: DEV=1
cache-from: |
type=local,src=/tmp/.buildx-cache
ghcr.io/python-discord/snekbox-base:latest
Expand All @@ -85,11 +86,10 @@ jobs:
export IMAGE_SUFFIX='-venv:${{ steps.sha_tag.outputs.tag }}'
docker-compose up --no-build -d
# One of the unit tests needs to import numpy.
- name: Install dependencies
run: >-
docker exec snekbox_dev /bin/bash -c
'pipenv install --system --deploy --dev && pip install numpy'
'pipenv install --system --deploy --dev'
# Required by pre-commit.
- name: Install git
Expand Down
17 changes: 14 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,24 @@ RUN chmod +x /usr/sbin/nsjail

# ------------------------------------------------------------------------------
FROM base as venv
ARG DEV

COPY Pipfile Pipfile.lock /snekbox/
WORKDIR /snekbox

# Install to the default user site since PIP_USER is set.
RUN pipenv install --deploy --system ${DEV:+--dev}
# Pipenv installs to the default user site since PIP_USER is set.
RUN pipenv install --deploy --system

# This must come after the first pipenv command! From the docs:
# All RUN instructions following an ARG instruction use the ARG variable
# implicitly (as an environment variable), thus can cause a cache miss.
ARG DEV

# Install numpy when in dev mode; one of the unit tests needs it.
RUN if [ -n "${DEV}" ]; \
then \
pipenv install --deploy --system --dev \
&& PYTHONUSERBASE=/snekbox/user_base pip install numpy~=1.19; \
fi

# At the end to avoid re-installing dependencies when only a config changes.
# It's in the venv image because the final image is not used during development.
Expand Down

0 comments on commit 0f604e5

Please sign in to comment.