Skip to content

Instantly share code, notes, and snippets.

@karma-git
Created January 1, 2022 13: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 karma-git/a56acc43e7a96360b75d1a5d9d84f934 to your computer and use it in GitHub Desktop.
Save karma-git/a56acc43e7a96360b75d1a5d9d84f934 to your computer and use it in GitHub Desktop.
Python-Poetry & Docker-Alpine

Overview

Our goal to manage python packages via poetry.

Poetry is a modern package manager inspired but tools such javascript npm. If you don't familiar with this tool check my another gist

Quickstart

Init project via poetry and install python-black

poetry new demo-po-alpine && cd demo-po-alpine
poetry add black

Inside the project folder we should create Dockerfile, docker-compose.yml. Also, it's good practise to specify all things that you don't need in runtime in .dockerignore.

So desired result should looks like this:

❯ tree
.
├── Dockerfile
├── README.rst
├── demo_po_alpine
│   └── __init__.py
├── docker-compose.yml
├── poetry.lock
├── pyproject.toml
└── tests
    ├── __init__.py
    └── test_demo_po_alpine.py

2 directories, 8 files

Build the image and check black version:

docker compose up        
[+] Running 1/1
 ⠿ Container demo-po-alpine  Started                                                                                                                                                                                                                                  0.6s
Attaching to demo-po-alpine
demo-po-alpine  | black, 21.12b0 (compiled: no)
demo-po-alpine exited with code 0
---
version: '3.8'
services:
demo-po-alpine:
container_name: demo-po-alpine
image: demo-po-alpine
build:
context: ./
args:
- POETRY_VERSION=1.1.12
command: black --version
volumes:
- ./:/tmp
# pull official base image
FROM python:3.10.1-alpine3.14
# set desired poetry version via build-arg
ARG POETRY_VERSION=1.1.12
# setup poetry
RUN apk add --no-cache --virtual .build-deps \
curl~=7.79 \
gcc~=10.3.1 \
libressl-dev~=3.3.3 \
libffi-dev~=3.3 \
musl-dev~=1.2.2 \
&& pip install --no-cache-dir poetry==${POETRY_VERSION} \
&& apk del .build-deps
# ignore everything what you dont't need in runtime in the file .dockerignore
COPY ./ ./
# our goal is install packages inside the container globally
RUN poetry config virtualenvs.create false \
&& poetry install
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment