惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
aimingoo的专栏
aimingoo的专栏
Martin Fowler
Martin Fowler
C
Check Point Blog
G
Google Developers Blog
V
Visual Studio Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Google DeepMind News
Google DeepMind News
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
MongoDB | Blog
MongoDB | Blog
月光博客
月光博客
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
D
Docker
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog
博客园 - 三生石上(FineUI控件)
A
About on SuperTechFans
Recent Announcements
Recent Announcements
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
阮一峰的网络日志
阮一峰的网络日志
Stack Overflow Blog
Stack Overflow Blog
Vercel News
Vercel News

ashishb.net

A day in Luxembourg - the richest country in the world I was asked to install malware during a fake interview Book summary: Breakneck - China's quest to engineer the future by Dan Wang Book summary: How to Teach Your Baby to Read Book Summary: The Discontented Little Baby Book by Pamela Douglas Introducing Amazing Sandbox - run third-party tools and AI agents securely on your machine Why software outsourcing gets a bad reputation? Book summary: The Natural Baby Sleep Solution by Polly Moore A day in Antwerp, Belgium Journey of online influencers Two days in Brussels, Belgium Shortcuts - when we love them and when we don't A visit to Rakhigarhi Three days in overhyped Paris Empty Japan, crowded Tokyo The real lock-in in GitHub is not the code, but the stars 11-day Norwegian Breakaway East Caribbean cruise Sanskrit and Sri Lankan Air Force Use REST with Open API The Achilles heel of American capitalism Costa Rica in 4 days At a juice stall in Sri Lanka A short stay at Warsaw, Poland Best practices for using Python & uv inside Docker Two days in Vilnius, Lithuania How IntelliJ IDEs waste disk space Pregnancy Why there aren't many digital nomads from India Two days in Riga, Latvia To keep your machine secure, run third-party tools inside Docker
Best practices for using Python & Poetry inside Docker
Ashish Bhatia · 2024-01-14 · via ashishb.net
Featured in Pycoder’s WeeklyAwesome Python Weekly Newsletter

Poetry is a great build system. And in 2023, I believe, no one should use the pip for a private Python codebase. (In 2025, I now prefer uv over Poetry.)

Getting it right inside Docker is a different issue, however.

Consider a simple Flask-based web server as an example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Install poetry
$ pip3 install poetry==2.0.1
$ poetry --version
Poetry (version 2.0.1)

# Create a sample package
$ poetry init --python=~3.10 --name=src --description='Flask Hello world' --dependency=Flask@3.0.0 --author='Ashish' --license='Apache 2.0' --no-interaction
$ poetry install
$ touch README.md
$ mkdir src

# Create a file src/server.py in your favorite editor
$ cat src/server.py
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello_world():
  return "<p>Hello, World!</p>"

if __name__ == "__main__":
  app.run()

Let’s finish the build process Now, let’s add a simple Dockerfile titled Dockerfile1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
FROM python:3.10-slim as base

# Install Poetry
RUN pip3 install poetry==2.0.1
WORKDIR /app
COPY pyproject.toml poetry.lock /app
# Install dependencies
RUN poetry install
COPY src /app/src

ENTRYPOINT ["poetry", "run", "python", "src/server.py"]

And let’s build and check its size

1
2
$ docker build -f Dockerfile1 -t example1 . && docker image inspect example1 --format='{{.Size}}' | numfmt --to=iec-i
235Mi

We don’t need poetry in the final build, so, we can save space via multi-stage docker builds.

Consider following the multi-stage docker file Dockerfile2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
FROM python:3.10-slim as builder

# Install Poetry
RUN pip3 install poetry==2.0.1
WORKDIR /app
COPY pyproject.toml poetry.lock /app

# virtual env is created in "/app/.venv" directory
ENV POETRY_NO_INTERACTION=1 \
POETRY_VIRTUALENVS_IN_PROJECT=1 \
POETRY_VIRTUALENVS_CREATE=true \
POETRY_CACHE_DIR=/tmp/poetry_cache
# Install dependencies
RUN --mount=type=cache,target=/tmp/poetry_cache poetry install --only main --no-root
RUN poetry install

FROM python:3.10-slim as runner
COPY src /app/src
COPY --from=builder /app/.venv /app/.venv
ENV PATH="/app/.venv/bin:$PATH"

ENTRYPOINT ["/app/.venv/bin/python", "src/server.py"]

And the result

1
2
$ docker build -f Dockerfile2 -t example1 . && docker image inspect example1 --format='{{.Size}}' | numfmt --to=iec-i
158Mi

That’s an extra 77Mi (33%) of saving while reducing the attack surface of the docker image!