Web Development

Introduction to Docker Containers

The Problem Docker Solves

“It works on my machine” is a classic developer problem — differences in OS, installed versions, and configuration between environments cause bugs that are hard to reproduce. Docker packages an application with everything it needs to run, so it behaves identically everywhere.

Images vs. Containers

An image is a blueprint — a snapshot of an application and its dependencies. A container is a running instance of that image. You can start many containers from one image.

A Basic Dockerfile

FROM node:20-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD ["node", "server.js"]

Building and Running

docker build -t my-tutorial-app . docker run -p 3000:3000 my-tutorial-app

-p 3000:3000 maps port 3000 on your machine to port 3000 inside the container.

Useful Everyday Commands

docker ps # list running containers docker stop <id> # stop a container docker images # list downloaded images docker logs <id> # view container output

docker-compose for Multi-Container Apps

Real applications usually need more than one container — an app server plus a database, for example. docker-compose.yml defines them together:

version: "3.8" services: web: build: . ports: - "3000:3000" db: image: postgres:16 environment: POSTGRES_PASSWORD: example
docker compose up

Practice Exercise

Write a Dockerfile for a simple Node.js or Python app, build the image, and run it with a port mapping so you can access it in your browser.

Leave a Reply

Your email address will not be published. Required fields are marked *