Docker

Docker

Docker is a platform that allows you to package applications and their dependencies into containers.
Containers run consistently across environments—your laptop, CI pipelines, and production servers.

Why Docker Matters

  • “Works on my machine” problem disappears
  • Lightweight compared to virtual machines
  • Faster deployments and scaling
  • Isolated, reproducible environments

Core Docker Concepts

ConceptDescription
ImageA blueprint (read-only) for your app
ContainerA running instance of an image
DockerfileInstructions to build an image
RegistryWhere images are stored (e.g., Docker Hub)

Installing Docker

  • macOS / Windows: Install Docker Desktop
  • Linux:
sudo apt install docker.io

Verify installation:

docker --version

Essential Docker Commands

Images

docker images              # List images
docker pull nginx          # Download image
docker rmi nginx           # Remove image

Containers

docker ps                  # Running containers
docker ps -a               # All containers
docker stop <id>           # Stop container
docker rm <id>             # Remove container

Run a container:

docker run nginx

Run in detached mode with port mapping:

docker run -d -p 8080:80 nginx<br>

Now open: http://localhost:8080

Interactive Containers

docker run -it ubuntu bash

You are now inside the container shell.

Writing a Dockerfile (Example)

Node.js App

# Use base image
FROM node:18

# Set working directory
WORKDIR /app

# Copy package files
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy app source
COPY . .

# Expose port
EXPOSE 3000

# Start app
CMD ["npm", "start"]

Build and run:

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

Docker Volumes (Persistent Data)

Without volumes, data disappears when containers stop.

docker run -v data:/app/data my-app

Bind mount (local folder):

docker run -v $(pwd):/app my-app

Docker Networks

docker network ls
docker network create my-network
docker run --network my-network nginx