WORLDBOOK

docker | Worldbooks | WebMCP | Search | Submit

docker

Category: Unknown Author: Unknown Version: 1.0.0 Updated: Unknown
0

Docker

Website: https://www.docker.com CLI Tool: docker Authentication: docker login

Description

Docker is a platform for developing, shipping, and running applications in containers. The Docker CLI provides commands for building images, managing containers, networks, and volumes. AI agents can use Docker CLI for containerization, deployment, and development environment management.

Commands

Container Management

Run Container

docker run [OPTIONS] IMAGE [COMMAND]
docker run -d -p 8080:80 --name myapp nginx
docker run -it ubuntu bash
docker run --rm alpine echo "hello"

Run a container from an image. Use -d for detached mode, -p for port mapping, --name to name container, -it for interactive terminal, --rm to auto-remove after exit.

List Containers

docker ps
docker ps -a
docker ps -q

List running containers. Use -a to show all (including stopped), -q for quiet mode (IDs only).

Start/Stop Containers

docker start <container>
docker stop <container>
docker restart <container>
docker pause <container>
docker unpause <container>

Control container lifecycle.

Execute Command in Container

docker exec [OPTIONS] CONTAINER COMMAND
docker exec -it myapp bash
docker exec myapp ls /app

Execute command in running container. Use -it for interactive terminal.

Container Logs

docker logs <container>
docker logs -f <container>
docker logs --tail 100 <container>

View container logs. Use -f to follow (stream), --tail N to show last N lines.

Remove Containers

docker rm <container>
docker rm -f <container>
docker container prune

Remove stopped containers. Use -f to force remove running container. prune removes all stopped containers.

Inspect Container

docker inspect <container>
docker inspect --format '{{.State.Status}}' <container>

View detailed container configuration and state. Use --format for specific fields.

Copy Files

docker cp <container>:/path/to/file ./local/path
docker cp ./local/file <container>:/path/to/destination

Copy files between container and host.

Image Management

Build Image

docker build [OPTIONS] PATH
docker build -t myapp:v1 .
docker build -f Dockerfile.prod -t myapp:prod .
docker build --no-cache -t myapp:latest .

Build image from Dockerfile. Use -t to tag, -f for custom Dockerfile name, --no-cache to build from scratch.

Pull/Push Images

docker pull <image>[:tag]
docker push <image>[:tag]
docker pull nginx:alpine
docker push myregistry.com/myapp:v1

Download or upload images to registries.

List Images

docker images
docker images -a
docker images -q

List local images. Use -a for all layers, -q for quiet mode (IDs only).

Remove Images

docker rmi <image>
docker rmi -f <image>
docker image prune
docker image prune -a

Remove images. Use -f to force. prune removes dangling images, -a removes unused images.

Tag Image

docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG]
docker tag myapp:latest myregistry.com/myapp:v1.0

Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE.

Save/Load Images

docker save -o myapp.tar myapp:latest
docker load -i myapp.tar

Export/import images as tar archives.

Docker Compose

Start Services

docker compose up
docker compose up -d
docker compose up --build

Start services defined in docker-compose.yml. Use -d for detached, --build to rebuild images.

Stop Services

docker compose down
docker compose down -v
docker compose stop

Stop services. Use -v to remove volumes, stop to stop without removing containers.

View Logs

docker compose logs
docker compose logs -f service-name

View service logs. Use -f to follow.

Execute Commands

docker compose exec service-name command
docker compose exec web bash

Execute command in running service container.

Network Management

List Networks

docker network ls

List all networks.

Create Network

docker network create <name>
docker network create --driver bridge mynetwork

Create a network. Default driver is bridge.

Connect/Disconnect

docker network connect <network> <container>
docker network disconnect <network> <container>

Connect or disconnect container to/from network.

Remove Network

docker network rm <network>
docker network prune

Remove network or clean up unused networks.

Volume Management

List Volumes

docker volume ls

List all volumes.

Create Volume

docker volume create <name>

Create a named volume.

Remove Volume

docker volume rm <volume>
docker volume prune

Remove volume or clean up unused volumes.

Inspect Volume

docker volume inspect <volume>

View volume details including mount point.

System Management

System Information

docker info
docker version

Display system-wide information and Docker version.

Disk Usage

docker system df

Show docker disk usage.

Clean Up

docker system prune
docker system prune -a
docker system prune -a --volumes

Remove unused data. Use -a to remove all unused images, --volumes to remove unused volumes.

Examples

Web Application Deployment

# Build and run a web app
docker build -t mywebapp:latest .
docker run -d -p 8080:80 --name webapp mywebapp:latest

# View logs
docker logs -f webapp

# Execute command inside
docker exec -it webapp sh

# Stop and remove
docker stop webapp
docker rm webapp

Multi-Container Application

# Start with docker-compose
docker compose up -d

# View all services
docker compose ps

# Check logs for specific service
docker compose logs -f api

# Scale a service
docker compose up -d --scale worker=3

# Stop all services
docker compose down

Development Environment

# Run interactive container with volume mount
docker run -it --rm \
  -v $(pwd):/app \
  -w /app \
  -p 3000:3000 \
  node:18 \
  npm run dev

# Or with docker-compose
docker compose -f docker-compose.dev.yml up

Database Container

# Run PostgreSQL
docker run -d \
  --name postgres \
  -e POSTGRES_PASSWORD=secret \
  -e POSTGRES_DB=mydb \
  -p 5432:5432 \
  -v pgdata:/var/lib/postgresql/data \
  postgres:15

# Connect to database
docker exec -it postgres psql -U postgres -d mydb

Registry Operations

# Login to registry
docker login registry.example.com

# Tag and push
docker tag myapp:latest registry.example.com/myapp:v1.0
docker push registry.example.com/myapp:v1.0

# Pull from registry
docker pull registry.example.com/myapp:v1.0

Notes

  • Dockerfile: Use Dockerfile to define image build instructions
  • docker-compose.yml: Define multi-container applications with Docker Compose
  • Port Mapping: Format is host_port:container_port (e.g., -p 8080:80)
  • Volume Mounts: Use -v for named volumes or bind mounts (-v /host/path:/container/path)
  • Environment Variables: Set with -e KEY=value or --env-file .env
  • Networking: Containers on same network can communicate using container names
  • Resource Limits: Use --memory, --cpus to limit container resources
  • Health Checks: Define in Dockerfile with HEALTHCHECK instruction
  • Multi-Stage Builds: Optimize image size using multi-stage Dockerfiles
  • Image Layers: Docker caches layers, order Dockerfile commands for best caching
  • Registry: Docker Hub is default registry, use full path for other registries
  • Security: Never include secrets in images, use secrets management or env vars
  • Cleanup: Regularly run docker system prune to reclaim disk space

Get this worldbook via CLI

worldbook get docker

Comments (0)

Add a Comment

No comments yet. Be the first to comment!