跳轉到

指令速查表

以下整理講義中出現的常用指令,依主題分類,方便快速查找。

基本資訊與說明

docker version
docker info
docker --help
docker <command> --help

映像檔管理

docker pull nginx
docker images
docker inspect nginx
docker tag nginx my-registry.com/nginx:1.0
docker rmi nginx
docker history my-image
docker save -o nginx.tar nginx:latest
docker load -i nginx.tar

容器生命週期

docker run nginx
docker run -d --name web -p 8080:80 nginx
docker run -it ubuntu bash
docker run --rm ubuntu echo "Hello, Docker!"
docker ps
docker ps -a
docker start web
docker stop web
docker restart web
docker rm web
docker container prune

常用執行參數

docker run -d nginx
docker run -it ubuntu bash
docker run --name web-server nginx
docker run --rm busybox sh
docker run -p 8080:80 nginx
docker run -p 127.0.0.1:8080:80 nginx
docker run -P nginx
docker run --network host nginx
docker run --network none busybox sleep 3600
docker run --entrypoint bash my-image

互動與除錯

docker exec -it my-container bash
docker exec my-container env
docker attach my-container
docker logs my-container
docker logs -f --tail 100 -t my-container
docker stats
docker stats --no-stream my-container
docker top my-container
docker inspect --format='{{.State.Status}}' my-container
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' my-container

檔案交換與備份

docker cp my-container:/app/config.json ./config.json
docker cp ./config.json my-container:/app/config.json
docker export my-container > container-backup.tar
docker import container-backup.tar my-image:backup

Volume 與掛載

docker volume create my_volume
docker volume ls
docker volume inspect my_volume
docker volume rm my_volume
docker volume prune

docker run -v mydata:/var/lib/postgresql postgres:18
docker run -v /host/path:/container/path image
docker run --mount type=bind,source=/host/path,target=/container/path image
docker run --tmpfs /container/path image

網路

docker network ls
docker network create my-network
docker network inspect my-network
docker network connect my-network existing-container
docker network disconnect my-network existing-container
docker network rm my-network
docker port my-container

Dockerfile 與建置

docker build -t myapp:1.0.0 .
docker build -f Dockerfile.multi-stage -t myapp:1.0.0-slim .
docker build --build-arg KEY=value -t myapp .
docker build --no-cache -t myapp .
docker images myapp

映像檔發布

docker login
docker push myusername/my-app:1.0.0
docker pull myusername/my-app:1.0.0

Docker Compose

docker compose version
docker compose up
docker compose up -d
docker compose up -d --build
docker compose up -d --force-recreate
docker compose up -d web db
docker compose down
docker compose down -v
docker compose down --rmi all
docker compose ps
docker compose ps -a
docker compose logs
docker compose logs -f
docker compose logs --tail=100 -t
docker compose exec web bash
docker compose exec --user root web apt-get update
docker compose build
docker compose build --no-cache
docker compose build --parallel
docker compose -f compose.yaml -f compose.prod.yaml up -d

資源限制與重啟策略

docker run --memory 512m nginx:alpine
docker run --cpus 0.5 nginx:alpine
docker run --restart on-failure:3 nginx:alpine
docker run --restart always nginx:alpine
docker run --log-opt max-size=10m --log-opt max-file=3 nginx:alpine

清理與空間檢查

docker system df -v
docker container prune
docker image prune
docker image prune -a
docker volume prune
docker network prune
docker system prune
docker system prune --volumes
docker system prune -a
docker system prune -f

安全性與弱點掃描

docker scout quickview nginx:1.25-alpine
docker scout cves nginx:1.25-alpine