Containers vs VMs
Containers share the host kernel and are lightweight compared to VMs.
Images
docker build -t myapp:1 .
docker pull node:20-alpine
docker push user/myapp:1Usage
build creates an image from a Dockerfile. pull/push download/upload images to a registry.
Running Containers
docker run -d --name web -p 8080:80 nginx:alpine
docker ps -a
docker exec -it web shUsage
run starts a container; -p publishes ports; ps lists containers; exec opens an interactive shell.
Volumes & Networks
docker volume create data
docker network create app-net
docker run -d --name db --network app-net -v data:/var/lib/mysql mysql:8Usage
volume persists data; network isolates communication; mounting a volume keeps DB state across restarts.
Dockerfile: Node App
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]Docker Compose
services:
web:
build: .
ports: ["3000:3000"]
depends_on: [db]
db:
image: mongo:6
volumes: ["dbdata:/data/db"]
volumes:
dbdata: {}Project: Node + Mongo with Compose
Build a Node.js API coupled with MongoDB using the provided Compose file. Implement CRUD endpoints and persist data via the volume.