Docker Basics & Images
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.
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.
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`).
3. Running Containers
A container is a running instance of an image.
-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.
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.
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`.
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
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
Question 3
What command runs a container in detached mode?
- docker run -a
- docker run -d
- docker run -it
- docker run --bg
Show answer
Exercises
Exercise 1
Write a Dockerfile for a simple Python Flask app that listens on port 5000.
Sample answer
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
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.