Docker Preview

Docker packages applications with everything they need to run. No more "it works on my machine." Same container runs anywhere.

Why Containers?

Traditional approach:

  1. Install OS
  2. Install dependencies (exact versions!)
  3. Configure environment
  4. Hope nothing conflicts
  5. Repeat for every server

Container approach:

  1. Build container once
  2. Run anywhere

Install Docker

Terminal
$# Install Docker
$curl -fsSL https://get.docker.com | sh
Installing Docker...
$
$# Add your user to docker group
$sudo usermod -aG docker $USER
$
$# Log out and back in, then verify
$docker --version
Docker version 24.0.7, build afdd53b

Run Your First Container

Terminal
$docker run hello-world
Unable to find image 'hello-world:latest' locally latest: Pulling from library/hello-world ... Hello from Docker! This message shows that your installation appears to be working correctly.

That pulled an image and ran it as a container.

Core Concepts

TermMeaning
ImageBlueprint/template (like a class)
ContainerRunning instance (like an object)
DockerfileInstructions to build an image
RegistryImage storage (Docker Hub)

Run Useful Containers

Run nginx

Terminal
$docker run -d -p 8080:80 nginx
a1b2c3d4e5f6...
$curl localhost:8080
<!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title>
  • -d runs in background (detached)
  • -p 8080:80 maps host port 8080 to container port 80

List Containers

Terminal
$docker ps
CONTAINER ID IMAGE COMMAND STATUS PORTS a1b2c3d4e5f6 nginx "/docker-entrypoint.…" Up 2 minutes 0.0.0.0:8080->80/tcp

Stop and Remove

Terminal
$docker stop a1b2c3d4e5f6
$docker rm a1b2c3d4e5f6
$
$# Or stop and remove together
$docker rm -f a1b2c3d4e5f6

Build Your Own Image

Create a Dockerfile:

hljs dockerfile
FROM node:20-alpine

WORKDIR /app

COPY package*.json ./
RUN npm install

COPY . .

EXPOSE 3000
CMD ["node", "server.js"]

Build and run:

Terminal
$docker build -t myapp .
Building...
$docker run -d -p 3000:3000 myapp

Docker Compose

Manage multi-container apps with docker-compose.yml:

hljs yaml
version: '3.8'

services:
  web:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://db:5432/myapp
    depends_on:
      - db

  db:
    image: postgres:15
    environment:
      POSTGRES_DB: myapp
      POSTGRES_PASSWORD: secret
    volumes:
      - postgres_data:/var/lib/postgresql/data

volumes:
  postgres_data:
Terminal
$docker compose up -d
Creating network... Creating myapp_db_1... Creating myapp_web_1...
$docker compose logs -f web
(view logs)
$docker compose down
(stop everything)

Essential Commands

Terminal
$# List images
$docker images
REPOSITORY TAG IMAGE ID SIZE nginx latest a1b2c3d4e5f6 142MB node 20 b2c3d4e5f6g7 1.1GB
$
$# List all containers
$docker ps -a
$
$# View container logs
$docker logs container_id
$
$# Shell into running container
$docker exec -it container_id /bin/sh
$
$# Clean up unused images
$docker system prune -a

When to Use Docker

Good for:

  • Consistent development environments
  • Deploying applications
  • Running databases locally
  • CI/CD pipelines
  • Microservices

Overkill for:

  • Simple scripts
  • Learning Linux basics first
  • When native install is simpler

Learn Docker Properly

This is just a preview. Docker deserves its own course. Once you're comfortable with Linux basics, dive deeper into containers.

Knowledge Check

What's the difference between a Docker image and container?

Quick Reference

CommandPurpose
docker run -d -p 80:80 imgRun container
docker psList running containers
docker stop idStop container
docker rm idRemove container
docker imagesList images
docker build -t name .Build image
docker compose up -dStart compose services
docker compose downStop compose services
docker logs idView logs
docker exec -it id shShell into container

Key Takeaways

  • Images are templates, containers are running instances
  • -d for background, -p for port mapping
  • Dockerfile defines how to build an image
  • Docker Compose manages multi-container apps
  • Containers solve "works on my machine" problems
  • Learn Linux basics before diving deep into Docker

Next: security fundamentals.