Skip to content

Instantly share code, notes, and snippets.

@Goldziher
Last active January 15, 2022 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Goldziher/942f4a027a7fa1e2cafaa35e0333b6dc to your computer and use it in GitHub Desktop.
Save Goldziher/942f4a027a7fa1e2cafaa35e0333b6dc to your computer and use it in GitHub Desktop.
Starlite+poetry example dockerfile
FROM python:3.10-slim AS install
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends curl \
&& apt-get autoremove -y
RUN pip install --upgrade pip
WORKDIR /app/
# install poetry and keep the get-poetry script so it can be reused later.
ENV POETRY_HOME="/opt/poetry"
RUN curl https://raw.githubusercontent.com/python-poetry/poetry/master/get-poetry.py > get-poetry.py
RUN python get-poetry.py
FROM install AS app-image
# allow controlling the poetry installation of dependencies via external args
ARG INSTALL_ARGS="--no-root --no-dev"
WORKDIR /app/
# let poetry know where its installed and add the poetry bin to the path
ENV POETRY_HOME="/opt/poetry"
ENV PATH="$POETRY_HOME/bin:$PATH"
COPY . .
# install without virtualenv, since we are inside a continer, follow by cleanup
RUN poetry config virtualenvs.create false \
&& poetry install $INSTALL_ARGS \
&& python get-poetry.py --uninstall \
&& rm get-poetry.py
# cleanup curl and apt cache
RUN apt-get purge -y curl \
&& apt-get clean -y \
&& rm -rf /root/.cache \
&& rm -rf /var/apt/lists/* \
&& rm -rf /var/cache/apt/*
# switch to a non-root user for security
RUN addgroup --system --gid 1001 "app-user"
RUN adduser --system --uid 1001 "app-user"
USER "app-user"
@Goldziher
Copy link
Author

Pass install args using docker or docker-compose.

You can for example pass only --no-root and keep the dev dependencies installed when running in a local development env.

You should also include a .dockerignore file in the folder that contains the app - and exclude all files and folders that should not be copied over into the container. This is also the most performant solution.

@Goldziher
Copy link
Author

Goldziher commented Jan 15, 2022

There is no CMD or ENTRY_POINT instruction in the Dockerfile itself, because you can specify these in your docker-compose file, or using the docker-cli / in K8s configuration.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment