Build your own image
Write a Dockerfile and build and tag an image.
A Dockerfile is a recipe: each instruction adds a layer to the image. This one packages a small Python app.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8000
CMD ["python", "app.py"]| Instruction | Meaning |
|---|---|
FROM | Base image to start from |
WORKDIR | Working directory inside the image |
COPY | Copy files from your folder into the image |
RUN | Run a command while building |
EXPOSE | Documents the port the app uses |
CMD | Default command when the container starts |
Build and run
docker build -t myapp:1.0 . # build from the Dockerfile in this folder
docker run -p 8000:8000 myapp:1.0Cache-friendly order
Docker caches layers. Copy the dependency file and install before copying the rest of the code, so changing your code does not reinstall everything.
.dockerignore
List files to leave out of the build, such as .git, node_modules and .env, to keep images small and avoid leaking secrets.
CarefulDo not put secrets in a Dockerfile or image. Anyone with the image can read every layer. Pass them at run time with environment variables or a secrets manager.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.