Skip to content
Adarsh Verma edited this page Aug 23, 2025 · 1 revision

Docker

What is Docker?

  • Docker is a containerization platform.
  • A container is like a lightweight virtual machine.
  • Instead of installing everything on your system, you package your app and all its dependencies inside a container so it runs the same everywhere.

Key Terms

  1. Image → Blueprint of your app (like a template).
    Example: node:18-alpine is a Node.js image.

  2. Container → Running instance of an image.
    Example: When you run docker run node:18-alpine, you create a container from that image.

  3. Dockerfile → A text file with instructions to build your own image.

  4. Docker Hub → A public repo where images are stored (like GitHub for Docker).

Basic Commands

# List all running containers
docker ps

# List all containers (including stopped)
docker ps -a

# List images
docker images

# Run a container
docker run -it ubuntu bash

# Stop a container
docker stop <container_id>

# Remove a container
docker rm <container_id>

# Remove an image
docker rmi <image_id>

Image & layer: create a Dockerfile file

FROM node:18-alpine   # Layer 1
WORKDIR /app          # Layer 2
COPY package*.json ./ # Layer 3
RUN npm install       # Layer 4
COPY . .              # Layer 5

Containers: build & Run

# Build image
docker build -t my-node-app .

# Run container
docker run -p 3000:3000 my-node-app

Volumes (Persistent Storage)

docker run -d -p 3306:3306 \
  -e MYSQL_ROOT_PASSWORD=secret \
  -v mysql-data:/var/lib/mysql \
  mysql:latest

Networking

By default, containers on the same bridge network can communicate using container names.

# Create network
docker network create mynet

# Run MongoDB in network
docker run -d --name mongo --network mynet mongo

# Run Node.js in same network
docker run -it --network mynet node bash
# Inside node container -> "ping mongo" works

Docker Compose

lets you define multiple containers (services) in one docker-compose.yml file & Run docker-compose up -d.

version: '3.8'

services:
  server:
    build: .
    ports:
      - '3000:3000'
    depends_on:
      - mongo
    volumes:
      - .:/app

  mongo:
    image: mongo
    ports:
      - '27017:27017'
    volumes:
      - mongo-data:/data/db

volumes:
  mongo-data:

Clone this wiki locally