skip to content

Search

Syspirit
EN

Docker

Essential Docker commands, docker-compose & dockerfile!

DevOps
Published on

Docker is a platform that allows you to package and run applications in lightweight, portable containers regardless of the environment in which it’s installed.

Image Management

ActionCommand
List all imagesdocker images -a
Pull an imagedocker pull postgres:latest
Build an imagedocker build -t myapp .
Remove an imagedocker rmi <image_id>
Remove all imagesdocker rmi $(docker images -q)

Container Management

ActionCommand
Run a containerdocker run <image>
Run in backgrounddocker run -d <image>
Stop a containerdocker stop <container_id>
Restart a containerdocker restart <container_id>
Pausedocker pause <container_id>
Unpausedocker unpause <container_id>
Remove a containerdocker rm <container_id>
Remove all containersdocker rm $(docker ps -aq)
Stop all containersdocker stop $(docker ps -q)

Inspection and Monitoring

ActionCommand
List active containersdocker ps
List all containersdocker ps -a
View container logsdocker logs <container_id>
View resource usagedocker stats
Inspect a containerdocker inspect <container_id>
Connect to a containerdocker exec -it <container_id> bash (or sh)
List Docker networksdocker network ls
List Docker volumesdocker volume ls
Docker system infodocker info
Check installed versionsdocker version

Cleanup (Prune)

ActionCommand
Clean stopped containersdocker container prune
Clean unused imagesdocker image prune
Clean unused networksdocker network prune
Clean unused volumesdocker volume prune
Full cleanup (dangerous!)docker system prune -a
Remove untagged imagesdocker image prune -a

Troubleshooting and Debug

ActionCommand
View real-time logsdocker logs -f <container_id>
View container processesdocker top <container_id>
Container statisticsdocker stats <container_id>
Inspect file changesdocker diff <container_id>
Image historydocker history <image_id>
Test network connectivitydocker exec <container_id> ping google.com
Copy from container to hostdocker cp <container>:/path/file ./file
Copy from host to containerdocker cp ./file <container>:/path/file

Dockerfile

Dockerfile = recipe for creating a custom image

Simple Example - Nginx

FROM nginx:alpine
COPY index.html /usr/share/nginx/html/
EXPOSE 80

Complete Example - Node.js App

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

Key Instructions

  • FROM - Base image (ubuntu, nginx, node, postgres…)
  • WORKDIR - Working directory in the container
  • COPY - Copy files from host to container
  • RUN - Execute commands during build
  • EXPOSE - Port to expose
  • CMD - Default command at launch

Docker Compose

Docker Compose = orchestrate one or more containers with a YAML file

Simple Example - Site + Database

services:
  web:
    image: nginx
    ports:
      - "80:80"
    volumes:
      - ./site:/usr/share/nginx/html
 
  db:
    image: postgres:15
    environment:
      POSTGRES_PASSWORD: password
      POSTGRES_DB: myapp
    volumes:
      - postgres_data:/var/lib/postgresql/data
 
volumes:
  postgres_data:

Advanced Example - Complete App

version: '3.8'
services:
  app:
    build: .
    ports:
      - "3000:3000"
    environment:
      - DATABASE_URL=postgres://user:pass@db:5432/app
    depends_on:
      - db
 
  db:
    image: postgres:15
    environment:
      POSTGRES_USER: user
      POSTGRES_PASSWORD: pass
      POSTGRES_DB: app

Essential Commands

ActionCommand
Start in backgrounddocker-compose up -d
Stop and remove alldocker-compose down
View real-time logsdocker-compose logs -f
Services statusdocker-compose ps
Restart all servicesdocker-compose restart
Build imagesdocker-compose build
Stop without removingdocker-compose stop
View service logsdocker-compose logs <service>