Skip to content

Commit

Permalink
Refactor docker image for devcontainer (#6029)
Browse files Browse the repository at this point in the history
* Make base image configurable

* Refactor dockerfile

- Place extra packages into extra shell script
- Add pre-build stage where packages are installed, and then cache removed
- Run same script in dev target, but do not remove

* Run docker workflow whenever package requirements change

* Replace devcontainer dockerfile

* Install base packages in devcontainer

* --no-cache-dir
  • Loading branch information
SchrodingersGat committed Dec 4, 2023
1 parent 15f58b9 commit f96055a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 80 deletions.
52 changes: 0 additions & 52 deletions .devcontainer/Dockerfile

This file was deleted.

21 changes: 10 additions & 11 deletions .devcontainer/devcontainer.json
Expand Up @@ -3,16 +3,12 @@
{
"name": "InvenTree",
"build": {
"dockerfile": "Dockerfile",
"dockerfile": "../Dockerfile",
"context": "..",
"target": "devcontainer",
"args": {
// Update 'VARIANT' to pick a Python version: 3, 3.10, 3.9, 3.8, 3.7, 3.6
// Append -bullseye or -buster to pin to an OS version.
// Use -bullseye variants on local on arm64/Apple Silicon.
"VARIANT": "3.10-bullseye",
// Options
"NODE_VERSION": "lts/*",
"WORKSPACE": "${containerWorkspaceFolder}"
"base_image": "mcr.microsoft.com/vscode/devcontainers/base:alpine-3.18",
"workspace": "${containerWorkspaceFolder}"
}
},

Expand Down Expand Up @@ -48,8 +44,11 @@
},

// Use 'forwardPorts' to make a list of ports inside the container available locally.
"forwardPorts": [8000],
"forwardPorts": [5173, 8000],
"portsAttributes": {
"5173": {
"label": "Vite server"
},
"8000": {
"label": "InvenTree server"
}
Expand All @@ -60,9 +59,9 @@

// Comment out to connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
"remoteUser": "vscode",
"containerUser": "vscode",
"features": {
"git": "os-provided",
"github-cli": "latest"
"git": "os-provided"
},

"remoteEnv": {
Expand Down
6 changes: 3 additions & 3 deletions .devcontainer/postCreateCommand.sh
Expand Up @@ -9,12 +9,12 @@ mkdir -p $1/dev/{commandhistory,plugins}
cd $1

# create venv
python3 -m venv dev/venv
. dev/venv/bin/activate
python3 -m venv $1/dev/venv
. $1/dev/venv/bin/activate

# setup InvenTree server
pip install invoke
invoke update --no-frontend
invoke update
invoke setup-dev
invoke frontend-install

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/docker.yaml
Expand Up @@ -45,6 +45,7 @@ jobs:
- docker-compose.yml
- docker.dev.env
- Dockerfile
- requirements.txt
# Build the docker image
Expand Down
42 changes: 28 additions & 14 deletions Dockerfile
Expand Up @@ -9,7 +9,8 @@
# - Runs InvenTree web server under django development server
# - Monitors source files for any changes, and live-reloads server

FROM python:3.10-alpine3.18 as inventree_base
ARG base_image=python:3.10-alpine3.18
FROM ${base_image} as inventree_base

# Build arguments for this image
ARG commit_hash=""
Expand Down Expand Up @@ -53,6 +54,7 @@ LABEL org.label-schema.schema-version="1.0" \
org.label-schema.vcs-url="https://github.com/inventree/InvenTree.git" \
org.label-schema.vcs-ref=${commit_tag}

# Install required system level packages
RUN apk add --no-cache \
git gettext py-cryptography \
# Image format support
Expand All @@ -75,6 +77,8 @@ WORKDIR ${INVENTREE_HOME}

COPY ./docker/requirements.txt base_requirements.txt
COPY ./requirements.txt ./
COPY ./docker/install_build_packages.sh .
RUN chmod +x install_build_packages.sh

# For ARMv7 architecture, add the piwheels repo (for cryptography library)
# Otherwise, we have to build from source, which is difficult
Expand All @@ -83,23 +87,19 @@ RUN if [ `apk --print-arch` = "armv7" ]; then \
printf "[global]\nextra-index-url=https://www.piwheels.org/simple\n" > /etc/pip.conf ; \
fi

RUN apk add --no-cache --virtual .build-deps \
gcc g++ musl-dev openssl-dev libffi-dev cargo python3-dev openldap-dev \
# Image format dev libs
jpeg-dev openjpeg-dev libwebp-dev zlib-dev \
# DB specific dev libs
postgresql-dev sqlite-dev mariadb-dev && \
pip install -r base_requirements.txt -r requirements.txt --no-cache-dir && \
apk --purge del .build-deps

COPY tasks.py docker/gunicorn.conf.py docker/init.sh ./

RUN chmod +x init.sh

ENTRYPOINT ["/bin/sh", "./init.sh"]

FROM inventree_base as prebuild

RUN ./install_build_packages.sh --no-cache --virtual .build-deps && \
pip install -r base_requirements.txt -r requirements.txt --no-cache-dir && \
apk --purge del .build-deps

# Frontend builder image:
FROM inventree_base as frontend
FROM prebuild as frontend

RUN apk add --no-cache --update nodejs npm && npm install -g yarn
RUN yarn config set network-timeout 600000 -g
Expand All @@ -111,7 +111,7 @@ RUN cd ${INVENTREE_HOME}/InvenTree && inv frontend-compile
# InvenTree production image:
# - Copies required files from local directory
# - Starts a gunicorn webserver
FROM inventree_base as production
FROM prebuild as production

ENV INVENTREE_DEBUG=False

Expand All @@ -128,9 +128,16 @@ COPY --from=frontend ${INVENTREE_HOME}/InvenTree/web/static/web ./InvenTree/web/
# TODO: e.g. -b ${INVENTREE_WEB_ADDR}:${INVENTREE_WEB_PORT} fails here
CMD gunicorn -c ./gunicorn.conf.py InvenTree.wsgi -b 0.0.0.0:8000 --chdir ./InvenTree


FROM inventree_base as dev

# Vite server (for local frontend development)
EXPOSE 5173

# Install packages required for building python packages
RUN ./install_build_packages.sh

RUN pip install -r base_requirements.txt --no-cache-dir

# Install nodejs / npm / yarn

RUN apk add --no-cache --update nodejs npm && npm install -g yarn
Expand All @@ -152,3 +159,10 @@ ENTRYPOINT ["/bin/ash", "./docker/init.sh"]

# Launch the development server
CMD ["invoke", "server", "-a", "${INVENTREE_WEB_ADDR}:${INVENTREE_WEB_PORT}"]

# Image target for devcontainer
FROM dev as devcontainer

ARG workspace="/workspaces/InvenTree"

WORKDIR ${WORKSPACE}
8 changes: 8 additions & 0 deletions docker/install_build_packages.sh
@@ -0,0 +1,8 @@
#!/bin/ash

# Install system packages required for building InvenTree python libraries

apk add gcc g++ musl-dev openssl-dev libffi-dev cargo python3-dev openldap-dev \
jpeg-dev openjpeg-dev libwebp-dev zlib-dev \
postgresql-dev sqlite-dev mariadb-dev \
$@

0 comments on commit f96055a

Please sign in to comment.