Skip to content

Commit

Permalink
add venv to docker and refactor to get rid of python build dependenci…
Browse files Browse the repository at this point in the history
…es that are not needed at runtime
  • Loading branch information
psych0d0g committed Oct 22, 2023
1 parent 584b8c5 commit 10f34a6
Show file tree
Hide file tree
Showing 6 changed files with 646 additions and 544 deletions.
61 changes: 49 additions & 12 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ RUN mkdir -p ${WEBSERVER_FOLDER}/assets/romm && \
ln -s /romm/library ${WEBSERVER_FOLDER}/assets/romm/library && \
ln -s /romm/resources ${WEBSERVER_FOLDER}/assets/romm/resources

# Setup backend
RUN apk update && \
apk add \
# install generall required packages
RUN apk add --upgrade \
bash \
curl \
gcc \
Expand All @@ -29,31 +28,69 @@ RUN apk update && \
python3-dev \
py3-pip

WORKDIR /back
COPY ./pyproject.toml ./poetry.lock ./
RUN pip install --no-cache --upgrade pip && \
# Install additional build dependencies
RUN apk add --upgrade \
gcc \
musl-dev \
mariadb-connector-c-dev \
python3-dev \
py3-pip

# Create python venv to not clash with OS python packages
RUN python3 -m venv /backend/

# move over project specific dependecy files
COPY ./pyproject.toml ./poetry.lock /

# Install poetry using pip
RUN . /backend/bin/activate && \
pip install --no-cache --upgrade pip && \
pip install --no-cache poetry && \
poetry config virtualenvs.create false && \
poetry install --no-interaction --no-ansi --only main && \
poetry cache clear --all .
COPY ./backend .
pip freeze | awk -F= '{print $1}' > /installed_pip_requirements.txt

# Install project dependencies using poetry
RUN . /backend/bin/activate && \
poetry config --no-cache virtualenvs.create false && \
poetry install --no-interaction --no-ansi --no-cache --only main && \
poetry export --without-hashes --only main --without-urls | awk -F= '{print $1}' > /installed_poetry_requirements.txt

# cleanup python dependencies that are not needed anymore
RUN . /backend/bin/activate && \
grep -v -x -f /installed_poetry_requirements.txt /installed_pip_requirements.txt > /build_requirements.txt && \
pip uninstall -y -r /build_requirements.txt

COPY ./backend /backend

# Setup init script and config files
COPY ./docker/init_scripts/* /
COPY ./docker/nginx/default.conf /etc/nginx/nginx.conf

# cleanup build deps
# cleanup additional build dependencies
RUN apk del \
gcc \
musl-dev \
mariadb-connector-c-dev \
python3-dev \
py3-pip

# cleanup leftover files that are not needed at runtime
RUN rm -r \
/pyproject.toml \
/poetry.lock \
/installed_pip_requirements.txt \
/installed_poetry_requirements.txt \
/build_requirements.txt \
/docker-entrypoint.sh \
/docker-entrypoint.d

# Move everything we prepared over to our final docker image
FROM scratch
LABEL org.opencontainers.image.title="zurdi15/romm" \
org.opencontainers.image.description="RomM (stands for Rom Manager) is a game library manager focused in retro gaming. Manage and organize all of your games from a web browser" \
org.opencontainers.image.licenses="GPL-3.0"
COPY --from=production-stage / /

# Expose ports and start
EXPOSE 8080
WORKDIR /romm
CMD ["/init"]
CMD ["/init"]
13 changes: 12 additions & 1 deletion docker/init_scripts/init
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
#!/bin/bash
#!/usr/bin/env bash

set -o errexit # treat errors as fatal
set -o nounset # treat unset variables as an error
set -o pipefail # treat errors in pipes as fatal
shopt -s inherit_errexit # inherit errexit

# use virtualenvs
source /backend/bin/activate

# switch to backend directory
cd /backend || { echo "/backend directory dosnt seem to exist"; exit 1; }

# Run migrations and start uvicorn
/init_back &
Expand Down
1 change: 0 additions & 1 deletion docker/init_scripts/init_back
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/bash

cd /back
alembic upgrade head && uvicorn main:app --proxy-headers --host 0.0.0.0 --port 5000 --uds /tmp/uvicorn.sock --workers 2
1 change: 0 additions & 1 deletion docker/init_scripts/init_worker
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/bash

cd /back
[[ ${ENABLE_EXPERIMENTAL_REDIS} == "true" ]] && rq worker high default low --logging_level WARN || sleep infinity
Loading

0 comments on commit 10f34a6

Please sign in to comment.