Skills / DevOps and CI/CD / Containers with Docker

DevOps and CI/CD, lesson 3 of 8

Containers with Docker

"It works on my machine" is one of the oldest problems in software. An app depends on a particular language version, libraries and settings, and a server that differs slightly can break it. Containers solve this by packaging the app together with everything it needs.

Docker is the most widely used container tool.

Key ideas:

  • An image is a packaged, read-only template: your app, its runtime (such as Python or Node), its libraries and its settings
  • A container is a running instance of an image. You can run many containers from one image
  • A Dockerfile is the recipe that builds an image, step by step

A simple Dockerfile for a Python web app reads like a list of instructions:

  1. FROM python:3.12-slim starts from an official Python image
  2. WORKDIR /app sets the working folder
  3. COPY requirements.txt . then RUN pip install -r requirements.txt installs the libraries
  4. COPY . . adds your code
  5. CMD ["python", "app.py"] says what to run when the container starts

Then docker build -t myapp . builds the image and docker run -p 8000:8000 myapp runs it, connecting port 8000 on your machine to port 8000 in the container.

Why teams love containers:

  • The same image runs on a laptop, a test server and production
  • Containers start in seconds and use fewer resources than full virtual machines, because they share the host's operating system kernel
  • Each service can be packaged and updated on its own

Docker Compose runs several containers together, such as an app plus its database, from one file.

Security note: use official, minimal base images, keep them updated, and never bake passwords or keys into an image.

Try it: Install Docker Desktop (free for personal use) and run the official hello-world image with docker run hello-world. Then read the output and explain each step it describes.

Check yourself

1. What is a Docker image?

2. What file contains the recipe for building an image?

3. Why are containers lighter than virtual machines?