Files
Documentation/Doc/Containerisation/Docker/Commands.md
T
Shaz 42b8971744
Build and deploy Docusaurus / build-and-deploy (push) Successful in 1m1s
Add network & containerisation
2026-03-10 20:31:37 +01:00

184 lines
3.2 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Docker Commands
Practical Docker and Docker Compose command reference. Each block groups related commands with inline comments.
For the concepts behind these commands, see:
- [Docker / Basics](./Basics.md)
---
## 1. Images
```bash
# List local images
docker images
# Pull an image from a registry
docker pull nginx:stable
# Build an image from a Dockerfile in the current directory
docker build -t myapp:1.0 .
# Build with a custom Dockerfile
docker build -f Dockerfile.prod -t myapp:prod .
# Tag an existing image
docker tag myapp:1.0 myapp:latest
# Remove an image
docker rmi myapp:1.0
# Remove all unused images (dangling and unreferenced)
docker image prune -a
```
---
## 2. Container Lifecycle
```bash
# Run a container in detached mode
docker run -d --name myapp -p 8080:80 nginx:stable
# Run with a bind mount, environment variable, and restart policy
docker run -d \
--name myapp \
-v /host/data:/app/data \
-e APP_ENV=production \
--restart unless-stopped \
myapp:1.0
# Start / stop / restart a container
docker start myapp
docker stop myapp
docker restart myapp
# List running containers
docker ps
# List all containers (including stopped)
docker ps -a
# Remove a stopped container
docker rm myapp
# Stop and remove in one go
docker stop myapp && docker rm myapp
```
---
## 3. Logs and Inspection
```bash
# View container logs
docker logs myapp
# Follow logs in real time
docker logs -f myapp
# Show last 50 lines
docker logs --tail 50 myapp
# Execute a command inside a running container
docker exec -it myapp bash # interactive shell
docker exec myapp ls /app # one-off command
# Inspect container configuration and networking
docker inspect myapp
# Show live resource usage for all running containers
docker stats
```
---
## 4. Volumes
```bash
# List volumes
docker volume ls
# Create a named volume
docker volume create mydata
# Inspect a volume (shows mount path on host)
docker volume inspect mydata
# Remove a volume
docker volume rm mydata
# Remove all unused volumes
docker volume prune
```
---
## 5. Networks
```bash
# List networks
docker network ls
# Create a user-defined bridge network
docker network create mynet
# Connect a running container to a network
docker network connect mynet myapp
# Inspect a network (shows connected containers)
docker network inspect mynet
# Remove a network
docker network rm mynet
```
---
## 6. Docker Compose
```bash
# Start all services in detached mode
docker compose up -d
# Stop and remove containers (volumes are kept)
docker compose down
# Stop and remove containers AND named volumes
docker compose down -v
# Rebuild images before starting
docker compose up -d --build
# Restart a single service
docker compose restart app
# View logs for all services
docker compose logs -f
# View logs for one service
docker compose logs -f app
# List containers for the current project
docker compose ps
# Execute a command inside a service container
docker compose exec app bash
```
---
## 7. System Cleanup
```bash
# Show disk usage by Docker objects (images, containers, volumes, cache)
docker system df
# Remove all stopped containers, unused networks, dangling images, and build cache
docker system prune
# Same but also remove unused non-dangling images
docker system prune -a
```