A small Dockerfile
FROM python:3.13-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "-m", "app"]
Copy dependency files and install them before copying the rest of the source. Docker caches that layer, so rebuilds after code edits are fast.
Images
| Command | What it does |
|---|---|
docker build -t myapp:1.0 . | Build and tag an image from the current directory |
docker images | List local images |
docker history myapp:1.0 | Show image layers and their sizes |
docker rmi myapp:1.0 | Remove an image |
Containers
docker run -d --name web -p 8000:8000 myapp:1.0 # detached, port mapped
docker run --rm -it myapp:1.0 bash # throwaway interactive shell
docker ps -a # all containers
docker logs -f web # follow logs
docker exec -it web sh # shell inside a running container
docker stop web && docker rm web
Volumes and networks
docker volume create pgdata
docker run -d -v pgdata:/var/lib/postgresql/data postgres:17
docker run --rm -v "$PWD":/work -w /work alpine ls # bind mount
docker network create appnet
docker run -d --network appnet --name db postgres:17
# other containers on appnet reach it by the hostname "db"
Compose
# compose.yaml
services:
web:
build: .
ports:
- "8000:8000"
environment:
DATABASE_URL: postgres://app:secret@db:5432/app
depends_on:
- db
db:
image: postgres:17
environment:
POSTGRES_USER: app
POSTGRES_PASSWORD: secret
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
docker compose up -d # start everything
docker compose logs -f web
docker compose down # stop and remove containers
Cleaning up
docker system df # what is using disk space
docker container prune # remove stopped containers
docker image prune # remove dangling images