
Photo by Rubaitul Azad on Unsplash
Docker has become the de‑facto standard for packaging and running applications, but many developers still feel daunted by the terminology and tooling that come with it. In this article we’ll walk through what a container is, why you should care, and how to get a simple web server up and running in minutes. By the end you’ll have a solid mental model of Docker’s core concepts and a handful of practical commands that will let you start experimenting on your own machine.
Why Containers Matter Right Now
Modern development workflows increasingly rely on reproducibility. A team member might develop an app on macOS, another runs it in CI on Linux, and a third deploys to a cloud platform. Without isolation, subtle differences in libraries or OS versions can turn “works for me” into a frustrating “it breaks everywhere else”. Containers solve this by bundling the application with its exact runtime environment—everything from the operating system kernel features to the specific version of a language runtime.
Beyond consistency, containers also enable rapid iteration. A Docker image is built once and then instantiated as many times as you like; each instance starts in seconds and consumes only the resources it needs. That makes them perfect for local development environments, testing pipelines, and even production deployments when combined with orchestration tools like Kubernetes.
1. Core Concepts: Images, Containers, and Layers
| Term | What It Is | Why It Matters |
|---|---|---|
| Image | A read‑only template that contains the application code, libraries, and OS files. | Think of it as a snapshot you can share or version. |
| Container | A running instance of an image, isolated from the host system. | Enables multiple versions or configurations to run side‑by‑side. |
| Layer | Each instruction in a Dockerfile creates a new immutable layer on top of the previous one. | Layers are cached; rebuilding only touches changed layers, speeding up builds. |
When you run docker build, Docker walks through each line of your Dockerfile, creating layers and caching them. Later runs that reuse the same base layers will skip re‑executing those steps, which is why incremental builds feel so fast.
2. Writing Your First Dockerfile
Below is a minimal example that turns a Python Flask app into a containerized service. The key ideas are:
- Base image – start from an official runtime to avoid reinventing the wheel.
- Copy only what you need – copy
requirements.txtfirst so Docker can cache dependencies. - Install dependencies – run the package manager inside the image.
- Add your code – copy the rest of the source after dependencies are installed.
- Expose ports and set entrypoint – tell Docker which port to listen on and what command to execute.
# 1️⃣ Pick a lightweight Python runtime
FROM python:3.12-slim
# 2️⃣ Set a working directory inside the container
WORKDIR /app
# 3️⃣ Copy dependency list first for better caching
COPY requirements.txt .
# 4️⃣ Install dependencies
RUN pip install --no-cache-dir -r requirements.txt
# 5️⃣ Copy the rest of your application code
COPY . .
# 6️⃣ Tell Docker which port to expose (Flask defaults to 5000)
EXPOSE 5000
# 7️⃣ Run the app
CMD ["python", "app.py"]
Tip: If you’re working on a language other than Python, replace python:3.12-slim with an appropriate base image (node:18-alpine, openjdk:21-jre-slim, etc.). The same principles apply.
3. Building and Running the Container
Once you have your Dockerfile, building and running is a two‑step process:
# Build the image (tag it so you can refer to it later)
docker build -t my-flask-app .
# Run a container from that image
docker run --rm -p 8080:5000 my-flask-app
--rmtells Docker to clean up the container when it stops.-p 8080:5000maps port 8080 on your host to port 5000 inside the container (the Flask default).
Now open http://localhost:8080/ and you should see your app’s output.
4. Inspecting, Managing, and Debugging Containers
A healthy Docker workflow involves more than just “run”. Knowing how to inspect and control containers lets you troubleshoot and fine‑tune performance.
| Command | What It Does |
|---|---|
docker ps |
List running containers. Add -a to see stopped ones. |
docker logs <container> |
Stream the container’s stdout/stderr output. |
docker exec -it <container> /bin/bash |
Open an interactive shell inside a running container (useful for debugging). |
docker stop <container> |
Gracefully terminate a container. |
docker rm <container> |
Remove a stopped container. |
Example: Suppose you need to check what environment variables are set inside your Flask app.
# First, find the container ID or name
docker ps
# Then exec into it
docker exec -it my-flask-app /bin/bash
# Inside the shell, run
env | grep FLASK
You can also use docker inspect <container> to get detailed JSON about networking, mounts, and more.
5. Docker Compose: Orchestrating Multiple Containers Locally
Real‑world applications rarely consist of a single process. A typical stack might include:
- Web server – your Flask app
- Database – PostgreSQL or MongoDB
- Cache – Redis
Docker Compose lets you define all of these in one file and start them with a single command.
# docker-compose.yml
version: "3.9"
services:
web:
build: .
ports:
- "8080:5000"
depends_on:
- db
db:
image: postgres:16-alpine
environment:
POSTGRES_PASSWORD: example
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
Run it with:
docker compose up --build
Compose automatically builds your web service, pulls the PostgreSQL image, and wires them together on an isolated network. Each container can refer to others by service name (web can reach db at db:5432).
6. Networking Inside Docker
Understanding how containers talk to each other is key when you start scaling.
- Bridge network (default) – Containers share a private IP range; they can reach each other via service names.
- Host network – The container shares the host’s networking namespace (rarely needed for beginners).
- Overlay network – Used by orchestration systems like Docker Swarm or Kubernetes to span multiple hosts.
When you expose a port with -p, Docker forwards traffic from the host into the container. If two containers need to communicate internally, no port mapping is required; they simply use each other’s service names and exposed ports.
7. Best Practices for Production‑Ready Containers
| Practice | Why It Helps |
|---|---|
Minimize image size – Use slim or alpine base images and delete build artifacts. |
Less attack surface and faster downloads. |
Pin versions – Specify exact tags (python:3.12-slim) instead of latest. |
Guarantees reproducibility over time. |
| Separate layers – Install dependencies before copying source to leverage cache. | Speeds up incremental builds during development. |
| Use multi‑stage builds – Compile in one stage, copy only binaries into a clean runtime image. | Keeps final images lean and secure. |
| Run as non‑root user – Create a dedicated user inside the container. | Reduces privilege escalation risk. |
Conclusion
Containers are not a silver bullet, but they do solve many of the pain points that plague modern development cycles: inconsistent environments, slow deployments, and hard‑to‑reproduce bugs. By mastering a few core commands—docker build, docker run, docker compose up—and understanding how images, layers, and networking work together, you can quickly spin up reliable services for both local testing and production.
Your first step? Write the Dockerfile above, build your Flask app, and watch it come alive on port 8080. From there, experiment with adding a database in Compose, tweak the image size, or try running the same stack on a different host. The world of containers is vast; this article just opened the door. Happy containerizing!
Frequently Asked Questions
What is the difference between an image and a container?
An image is a read‑only snapshot that contains your application code, dependencies, and OS files. A container is a running instance of that image, isolated from the host system.
Why should I copy requirements.txt before my source code in a Dockerfile?
Copying requirements.txt first allows Docker to cache the dependency installation layer. Subsequent builds will reuse this layer unless the dependencies change, speeding up rebuilds dramatically.
What does the --rm flag do when running a container?
The --rm flag tells Docker to automatically remove the container’s filesystem once it stops, keeping your system clean from stopped containers.
How do I expose and map ports correctly for my application?
Use EXPOSE in the Dockerfile to declare which port the container listens on (e.g., EXPOSE 5000). When running the container, map that internal port to a host port with -p, such as -p 8080:5000, so you can access the service via localhost:8080.


