Unit 5.3 · Tutorial 1

Docker Basics & Images

Chapter 14 · System Design & Deployment
~2.5 hours Intermediate Docker · Images · Containers · Dockerfiles

Overview

Docker revolutionised application packaging and deployment. This tutorial covers the fundamentals: Docker images, containers, the Docker CLI, writing Dockerfiles, and using Docker registries. You'll learn to containerise your applications for consistent, reproducible deployments.

Why this matters: Docker solves the "works on my machine" problem. It ensures your application runs the same way in development, testing, and production.

1. Introduction to Docker

Docker is a platform for developing, shipping, and running applications in containers. Containers are lightweight, isolated environments that package an application and its dependencies.

  • Containers vs Virtual Machines: Containers share the host OS kernel, making them faster and more resource‑efficient than VMs.
  • Docker Engine: The runtime that runs containers.
  • Docker CLI: Command‑line tool for interacting with Docker.
# Install Docker (macOS/Windows/Linux) # Verify installation docker --version docker run hello-world

2. Docker Images

An image is a read‑only template with instructions for creating a container. Images are built from a Dockerfile.

  • Image layers: Each instruction in a Dockerfile creates a layer.
  • Layer caching: Docker caches layers to speed up builds.
  • Image naming: `repository:tag` (e.g., `node:18-alpine`).
# List images docker images # Pull an image docker pull node:18-alpine # Build an image docker build -t my-app:1.0 . # Tag an image docker tag my-app:1.0 username/my-app:latest # Push to a registry docker push username/my-app:latest

3. Running Containers

A container is a running instance of an image.

# Run a container docker run -d -p 3000:3000 --name my-app my-app:1.0 # List running containers docker ps # List all containers (including stopped) docker ps -a # Stop a container docker stop my-app # Start a stopped container docker start my-app # Remove a container docker rm my-app # Execute a command inside a running container docker exec -it my-app sh # View logs docker logs my-app
Flags:
  • -d: Detached mode (run in background).
  • -p: Port mapping (host:container).
  • --name: Name the container.
  • -it: Interactive terminal.
  • --rm: Remove container after it stops.

4. Writing Dockerfiles

A Dockerfile is a text file with instructions for building an image.

# Dockerfile for a Node.js app FROM node:18-alpine WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . EXPOSE 3000 CMD ["node", "index.js"]

Common instructions

  • FROM – Base image.
  • WORKDIR – Working directory.
  • COPY – Copy files from host to image.
  • RUN – Execute commands (during build).
  • EXPOSE – Document which ports the container listens on.
  • CMD – Default command to run.
  • ENTRYPOINT – Main command (overrides CMD).
  • ENV – Environment variables.
# Multi‑stage build (reduces image size) FROM node:18-alpine AS builder WORKDIR /app COPY . . RUN npm ci && npm run build FROM node:18-alpine WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules CMD ["node", "dist/index.js"]

5. Docker Registries

Registries store Docker images. Docker Hub is the default public registry.

  • Docker Hub: `docker push username/repo:tag`.
  • Private registries: AWS ECR, Google Container Registry, Azure Container Registry, or self‑hosted.
  • Authentication: `docker login`.
# Login to Docker Hub docker login # Tag and push docker tag my-app:1.0 myusername/my-app:latest docker push myusername/my-app:latest # Pull from a private registry docker pull myusername/my-app:latest

Quiz

Question 1

What is the difference between a Docker image and a container?

  • An image is a running instance; a container is a template.
  • An image is a template; a container is a running instance.
  • They are the same thing.
  • Images are for production, containers are for development.
Show answer
B. An image is a template; a container is a running instance.

Question 2

Which Dockerfile instruction copies files from the host to the image?

  • RUN
  • COPY
  • ADD
  • Both B and C (COPY and ADD)
Show answer
D. Both COPY and ADD (but COPY is preferred).

Question 3

What command runs a container in detached mode?

  • docker run -a
  • docker run -d
  • docker run -it
  • docker run --bg
Show answer
B. docker run -d.

Exercises

Exercise 1

Write a Dockerfile for a simple Python Flask app that listens on port 5000.

Sample answer
FROM python:3.10-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . EXPOSE 5000 CMD ["python", "app.py"]

Exercise 2

Build and run the container from the Dockerfile above. Map port 5000 on the host to port 5000 in the container.

Sample answer
docker build -t flask-app . docker run -d -p 5000:5000 --name flask-app flask-app

Then visit http://localhost:5000.

Homework

Homework 1

Containerise your capstone project's backend. Write a Dockerfile, build the image, and run the container. Document the steps and any issues encountered.

Sample outline
  • Dockerfile: Multi‑stage build for Node.js/TypeScript.
  • Build: `docker build -t capstone-api .`
  • Run: `docker run -d -p 3000:3000 capstone-api`
  • Issues: Environment variables, database connection, etc.

Mini‑Project

Containerise a Full‑Stack Application

Containerise a full‑stack application with a React frontend and a Node.js backend. Write separate Dockerfiles for each, and use a simple script to build and run them.

Sample outline
  • Frontend Dockerfile: Build React app, serve with nginx.
  • Backend Dockerfile: Node.js app with Express.
  • Run: `docker run -p 3000:3000 backend` and `docker run -p 80:80 frontend`.
  • Network: Use `--network` or `docker-compose` (covered in Tutorial 2).

Tutorial Summary

You learned the basics of Docker: images, containers, Dockerfiles, and registries. You can now containerise your applications and run them consistently across environments.

Key takeaway: Docker is the foundation of modern deployment. Mastering it is essential for any backend or full‑stack developer.