“It works on my machine” is the problem Docker solves. A Docker image packages your app with everything it needs, and a Dockerfile is the recipe for building that image.
Image vs container
An image is a read-only template. A container is a running instance of an image. You can start many containers from one image, just as you can run several copies of one program.
A simple Dockerfile for a 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 ["gunicorn", "app:app", "--bind", "0.0.0.0:8000"]
FROMchooses the base image.WORKDIRsets the folder inside the image.COPYbrings files in.RUNexecutes a command during the build.EXPOSEdocuments the port (it does not publish it).CMDis the command that starts the app.
Layers and caching
Each instruction creates a layer, and Docker reuses unchanged layers. That is why the requirements file is copied and installed before the rest of the code: changing a line of code does not reinstall every package.
Build and run
docker build -t course-app:1.0 .docker run -p 8000:8000 course-app:1.0
The -p 8000:8000 option maps your computer’s port to the container’s port.
Good habits
- Use small base images such as
-slimor Alpine. - Add a
.dockerignorefile so virtual environments and secrets are not copied in. - Never put passwords in the Dockerfile; pass them at runtime as environment variables.
- Run the app as a non-root user where possible.
Next step
Once single containers make sense, combine several with Docker Compose, and learn how the images are deployed to Kubernetes.
Related: Docker basics and Docker volumes.
Hands-on labs like this are part of our AWS and DevOps course in Bangalore.
Book a free demo class at our BTM Layout centre, and ask a counsellor for batch details.
