๐Ÿ“ Docker

Dockerfile: Building Your Own Images

P
Author
PyLand Team
๐Ÿ“…
Published
08.05.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
339
๐ŸŒฟ
Level
Medium

Dockerfile โ€” a text file with instructions for building an image. Each instruction becomes a layer.

Minimal Python Dockerfile

FROM python:3.11-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Core Instructions

FROM โ€” base image. Always the first line.

FROM python:3.11-slim
FROM ubuntu:22.04
FROM scratch  # empty image

WORKDIR โ€” working directory inside the container. Created automatically.

WORKDIR /app

COPY โ€” copy files from the host into the image.

COPY app.py .          # copy a file
COPY . .               # copy everything
COPY src/ /app/src/    # copy a directory

RUN โ€” run a command at build time. The result is saved into the image.

RUN pip install flask
RUN apt-get update && apt-get install -y curl

CMD โ€” default command when the container starts.

CMD ["python", "app.py"]
CMD ["gunicorn", "app:app", "--bind", "0.0.0.0:8000"]

EXPOSE โ€” documents the port (does not actually publish it, just informational).

EXPOSE 8000

ENV โ€” environment variable.

ENV PYTHONDONTWRITEBYTECODE=1
ENV PYTHONUNBUFFERED=1

Building and Running

# Build an image with a tag
docker build -t myapp:v1 .

# Build with a different Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .

# Run
docker run -p 8000:8000 myapp:v1

.dockerignore

The .dockerignore file at the project root works like .gitignore for Docker. It excludes files from the build context.

.venv/
__pycache__/
*.pyc
.git/
.env
tests/
*.md

Without .dockerignore, Docker copies everything into the image including .git and node_modules โ€” builds will be slow and images will be bloated.

CMD vs ENTRYPOINT

# CMD โ€” can be overridden at runtime:
CMD ["python", "app.py"]
# docker run myapp python other.py  โ† replaces CMD

# ENTRYPOINT โ€” fixes the command; CMD becomes its arguments:
ENTRYPOINT ["python"]
CMD ["app.py"]
# docker run myapp other.py  โ† runs python other.py

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Docker Compose: Multi-Container Applications

Docker Compose is a tool for running multiple related containers as a single application. The...

๐Ÿ“… 08.05.2026 ๐Ÿ‘๏ธ 324
๐Ÿ“

What Is Docker and Why You Need It

Docker solves one of the most common problems in software development: "It works on my...

๐Ÿ“… 08.05.2026 ๐Ÿ‘๏ธ 321
๐Ÿ“

Multi-stage Builds: Shrinking Your Docker Image

A large image means slow downloads, more disk usage, and a bigger attack surface. Multi-stage...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 318
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum Docker: From Chaos to Containers Open course curriculum