docker.cheat 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. % docker
  2. # 删除镜像 Remove an image
  3. docker image rm <image_id>
  4. # 从本地镜像存储中删除镜像 Delete an image from the local image store
  5. docker rmi <image_id>
  6. # 清理无标签/悬空的镜像 Clean none/dangling images
  7. docker rmi $(docker images --filter "dangling=true" -q --no-trunc)
  8. # 强制清理无标签/悬空的镜像 Force clean none/dangling images
  9. docker rmi $(docker images --filter "dangling=true" -q --no-trunc) -f
  10. # 列出本地存储的所有镜像 List all images that are locally stored with the Docker engine
  11. docker images
  12. # 从当前目录的 Dockerfile 构建镜像并打标签 Build an image from the Dockerfile in the current directory and tag the image
  13. docker build -t <image>:<version> .
  14. # 从镜像仓库拉取镜像 Pull an image from a registry
  15. docker pull <image>:<version>
  16. # 通过 SIGTERM 停止正在运行的容器 Stop a running container through SIGTERM
  17. docker stop <container_id>
  18. # 通过 SIGKILL 强制停止正在运行的容器 Stop a running container through SIGKILL
  19. docker kill <container_id>
  20. # 列出所有网络 List the networks
  21. docker network ls
  22. # 列出正在运行的容器 List the running containers
  23. docker ps
  24. # 删除所有正在运行和已停止的容器 Delete all running and stopped containers
  25. docker rm -f $(docker ps -aq)
  26. # 在容器内创建一个新的 bash 进程并连接到终端 Create a new bash process inside the container and connect it to the terminal
  27. docker exec -it <container_id> bash
  28. # 打印容器日志的最后 100 行 Print the last lines of a container's logs
  29. docker logs --tail 100 <container_id> | less
  30. # 打印容器日志的最后 100 行并持续跟踪日志 Print the last lines of a container's logs and following its logs
  31. docker logs --tail 100 <container_id> -f
  32. # 创建新网络 Create new network
  33. docker network create <network_name>
  34. # 获取镜像 ID (示例命令,需根据实际情况调整) Get image ID (example command, adjust as needed)
  35. $ image_id: docker images --- --headers 1 --column 3
  36. # 获取容器 ID (示例命令,需根据实际情况调整) Get container ID (example command, adjust as needed)
  37. $ container_id: docker ps --- --headers 1 --column 1
  38. % docker-compose
  39. # 构建、创建、启动并附加到所有服务的容器 Builds, (re)creates, starts, and attaches to containers for all services
  40. docker-compose up
  41. # 构建、创建、启动并分离所有服务的容器 Builds, (re)creates, starts, and detaches to containers for all services
  42. docker-compose up -d
  43. # 构建、创建、启动并附加到指定服务的容器 Builds, (re)creates, starts, and attaches to containers for a service
  44. docker-compose up -d <service_name>
  45. # 构建、创建、启动并分离指定服务的容器 Builds, (re)creates, starts, and detaches to containers for a service
  46. docker-compose up -d <service_name>
  47. # 打印指定服务日志的最后 100 行 Print the last lines of a service’s logs
  48. docker-compose logs --tail 100 <service_name> | less
  49. # 打印指定服务日志的最后 100 行并持续跟踪日志 Print the last lines of a service's logs and following its logs
  50. docker-compose logs -f --tail 100 <service_name>
  51. # 停止并删除由 `up` 创建的容器和网络 Stops containers and removes containers, networks created by up
  52. docker-compose down