Created
September 19, 2023 12:15
-
-
Save bbengfort/28a82540c4af38fe9d8aed56c913aebe to your computer and use it in GitHub Desktop.
Dockerfile examples for the blog post "How to Dockerize Python Data Science Processes" on rotational.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Ignore docker specific files | |
.dockerignore | |
docker-compose.yml | |
Dockerfile | |
# Ignore git directory and files | |
.gitignore | |
.git | |
# Ignore text files at the root of the project (optional) | |
LICENSE | |
README.md | |
DESCRIPTION.md | |
CONTRIBUTING.md | |
SECURITY.txt | |
# Ignore local environment and secrets | |
.python-version | |
.env | |
.env.template | |
.secret |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3" | |
services: | |
app: | |
build: | |
context: . | |
volumes: | |
- .:/home/myuser/app | |
command: > | |
sh -c "python3 main.py" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM python:3.11-slim | |
# Do not prompt during apt-get commands | |
ARG DEBIAN_FRONTEND=noninteractive | |
# Ensure output to stdout and stderr are written to the terminal without a buffer | |
ENV PYTHONUNBUFFERED 1 | |
# Add library dependencies for compiling data science python packages | |
RUN apt-get update && \\ | |
apt-get install --no-install-recommends -y gcc libc-dev build-essential python3-dev | |
# Create a user so that we don't run as root | |
RUN groupadd -r myuser && useradd -m -r -g myuser myuser | |
USER myuser | |
# Set the working directory where Python code will be stored | |
RUN mkdir /home/myuser/app | |
WORKDIR /home/myuser/app | |
# Install Python dependencies | |
COPY ./requirements.txt /requirements.txt | |
RUN pip3 install -r /requirements.txt | |
# Switch back to root to clean up after the build process | |
USER root | |
RUN apt-get clean autoclean && apt-get autoremove --yes && \\ | |
rm -rf /var/lib/apt/lists/* | |
# Switch back to non-root user and copy Python code | |
USER myuser | |
COPY ./ ./ | |
# Default command is a python3 interpreter | |
CMD ["python3"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment