Docker 101
You've met the shop's API in Software Development 101 and its database in SQL 101. Today you'll package both, so they run the same on any machine with one command.
| Your laptop | The server | |
|---|---|---|
| OS | Windows | Linux |
| Node.js | 22 | 18 |
| Postgres | 16 | 14 |
| Setup | you did it once, by hand | someone else did it differently |
shop.cshop.exeDockerfileMinutes to boot. Gigabytes each.
Seconds to start. Megabytes each.
On Windows and macOS, Docker Desktop runs one small Linux VM, and your containers live inside it.
docker ps -a still lists it, stopped. -a means all, not just running.docker run -d -p 8080:80 --name web nginxdocker runstop and start keep the container and its files.rm deletes it, and refuses while it's running. -f forces it.-p 8081:80.
# The recipe for the shop API's image.
FROM node:22-slim
WORKDIR /app
COPY package*.json ./
RUN npm ci --omit=dev
COPY . .
EXPOSE 3000
USER node
CMD ["node", "server.js"]
/app inside the image.COPY . . runs again..dockerignore keeps node_modules and .env out of the image.ECONNREFUSED 127.0.0.1:5432.We need a second container, and a way for the two to find each other. That's Compose.
services:
api:
build: ./api
ports:
- "3000:3000"
environment:
DATABASE_URL: postgresql://shop:${DB_PASSWORD}@db:5432/shop
depends_on:
db:
condition: service_healthy
db:
image: postgres:16
environment:
POSTGRES_USER: shop
POSTGRES_PASSWORD: ${DB_PASSWORD}
volumes:
- pgdata:/var/lib/postgresql/data
- ./db/init:/docker-entrypoint-initdb.d
healthcheck:
test: ["CMD-SHELL", "pg_isready -U shop"]
interval: 2s
retries: 15
volumes:
pgdata:
api and db.docker build and -p..env file that stays out of Git.up -d --build builds the API, creates the network and volume, and starts both.logs shows every service's output in one place.down deletes them, and that's fine.up.down -v deletes volumes too: the data is gone. Great for a fresh start, terrible in production.
# .env.example: committed, no real values
DB_PASSWORD=change-me
# .env: your copy, never committed
DB_PASSWORD=a-long-random-password
.env next to docker-compose.yml..env in .gitignore, and commit .env.example so others know what to set.| docker run -d -p 8080:80 --name web nginx | start a container in the background |
|---|---|
| docker ps docker ps -a | running containers, or all of them |
| docker logs web | what it printed |
| docker stop web docker rm web | stop it, then delete it |
| docker build -t shop-api ./api | build an image from a Dockerfile |
| docker images | images on this machine |
| docker compose up -d --build | build and start everything |
| docker compose ps logs exec | check on it, read logs, run a command inside |
| docker compose down down -v | remove containers, or containers and data |
docker push sends it to a registry such as Docker Hub or GitHub's, so a server can pull it.docker compose up -d, nginx in front: covered in Software Development 101.Containerize something of your own this week: one Dockerfile, one compose file.
docker compose up -d questions