Docker CLI Helper
This skill explains how to use the Docker command line for common container workflows.
When to Use
Use this skill when:
- - The user wants to build or rebuild a Docker image.
- The user wants to run a container (one-off or long-running).
- The user wants to see which containers/images/volumes exist.
- The user wants to stop or remove containers/images.
- The user wants to see logs, exec into a container, or check resource usage.
Requirements
- - Docker is installed and running.
- INLINECODE0 or
docker info works in the user’s shell.
If unsure, suggest the user run:
CODEBLOCK0
to confirm Docker is available.
Safety Guidelines
- - Prefer read-only or non-destructive commands first:
-
docker ps,
docker ps -a
-
docker images
-
docker logs
-
docker inspect
- - Be cautious with destructive commands:
-
docker rm,
docker rmi
-
docker system prune
-
docker volume rm
- - Only recommend destructive cleanups when the user explicitly wants to free resources and understands what will be removed.
Common Workflows
1. List and inspect containers
List running containers:
CODEBLOCK1
List all containers (including stopped):
CODEBLOCK2
Inspect a container in detail:
CODEBLOCK3
2. List and inspect images
List local images:
CODEBLOCK4
Inspect an image:
CODEBLOCK5
3. Build images
Build an image from a Dockerfile in the current directory:
CODEBLOCK6
Example:
CODEBLOCK7
If the Dockerfile is in another directory:
CODEBLOCK8
4. Run containers
Run a container in the foreground:
CODEBLOCK9
Run in detached mode (background service):
CODEBLOCK10
Map ports from container to host:
CODEBLOCK11
Mount a host directory into the container:
CODEBLOCK12
5. Stop and remove containers
Stop a running container:
CODEBLOCK13
Remove a stopped container:
CODEBLOCK14
Stop and remove in one shot (two commands):
CODEBLOCK15
6. Remove images
Remove an image by ID or name:
CODEBLOCK16
Only suggest this when the user is sure the image is no longer needed.
7. Logs and exec
See logs for a container:
CODEBLOCK17
Stream logs (follow):
CODEBLOCK18
Execute a shell inside a running container (if it has /bin/bash):
CODEBLOCK19
or with /bin/sh:
CODEBLOCK20
8. Clean up resources
Only suggest these when the user explicitly wants cleanup:
- - Remove all stopped containers:
CODEBLOCK21
CODEBLOCK22
- - Remove everything unused (containers, networks, images, and optionally volumes):
CODEBLOCK23
For a more aggressive cleanup, but only if the user confirms:
CODEBLOCK24
Troubleshooting Tips
- - If images cannot be pulled, check:
- Network connectivity.
- Registry authentication (if using a private registry).
- - If ports are already in use, suggest:
- Changing the host port in
-p host:container.
- Or stopping the process that currently uses the port.
- - If a container keeps exiting immediately:
- Suggest checking
docker logs <container> for errors.
- Inspect entrypoint and command configuration.
技能名称:docker-cli
详细描述:
Docker CLI 助手
本技能介绍如何使用 Docker 命令行 执行常见的容器工作流程。
使用场景
在以下情况下使用本技能:
- - 用户想要构建或重新构建 Docker 镜像。
- 用户想要运行容器(一次性或长期运行)。
- 用户想要查看现有的容器/镜像/卷。
- 用户想要停止或删除容器/镜像。
- 用户想要查看日志、进入容器或检查资源使用情况。
前提条件
- - Docker 已安装并正在运行。
- 用户终端中可执行 docker version 或 docker info。
如果不确定,建议用户运行:
bash
docker version
以确认 Docker 可用。
安全指南
- docker ps、docker ps -a
- docker images
- docker logs
- docker inspect
- docker rm、docker rmi
- docker system prune
- docker volume rm
- - 仅在用户明确希望释放资源并了解删除内容时,才建议执行破坏性清理。
常见工作流程
1. 列出和检查容器
列出运行中的容器:
bash
docker ps
列出所有容器(包括已停止的):
bash
docker ps -a
详细检查容器:
bash
docker inspect <容器ID或名称>
2. 列出和检查镜像
列出本地镜像:
bash
docker images
检查镜像:
bash
docker inspect <镜像ID或名称>
3. 构建镜像
从当前目录的 Dockerfile 构建镜像:
bash
docker build -t <镜像名称>:<标签> .
示例:
bash
docker build -t my-app:latest .
如果 Dockerfile 位于其他目录:
bash
docker build -t my-app:latest path/to/context
4. 运行容器
在前台运行容器:
bash
docker run --rm -it <镜像名称>:<标签>
在分离模式下运行(后台服务):
bash
docker run -d --name <容器名称> <镜像名称>:<标签>
将容器端口映射到主机:
bash
docker run -d --name <容器名称> -p 8080:80 <镜像名称>:<标签>
将主机目录挂载到容器中:
bash
docker run -d --name <容器名称> -v /主机路径:/容器路径 <镜像名称>:<标签>
5. 停止和删除容器
停止运行中的容器:
bash
docker stop <容器ID或名称>
删除已停止的容器:
bash
docker rm <容器ID或名称>
一次性停止并删除(两个命令):
bash
docker stop <容器ID或名称>
docker rm <容器ID或名称>
6. 删除镜像
按 ID 或名称删除镜像:
bash
docker rmi <镜像ID或名称>
仅在用户确认不再需要该镜像时建议此操作。
7. 日志和进入容器
查看容器日志:
bash
docker logs <容器ID或名称>
实时流式查看日志:
bash
docker logs -f <容器ID或名称>
在运行中的容器内执行 shell(如果包含 /bin/bash):
bash
docker exec -it <容器ID或名称> /bin/bash
或使用 /bin/sh:
bash
docker exec -it <容器ID或名称> /bin/sh
8. 清理资源
仅在用户明确要求清理时建议以下操作:
bash
docker container prune
bash
docker image prune
- - 删除所有未使用的资源(容器、网络、镜像,以及可选的卷):
bash
docker system prune
如需更彻底的清理,但仅在用户确认后:
bash
docker system prune -a
故障排除提示
- 网络连接。
- 注册表身份验证(如果使用私有注册表)。
- 在 -p 主机端口:容器端口 中更改主机端口。
- 或停止当前占用端口的进程。
- 建议通过 docker logs <容器> 检查错误。
- 检查入口点和命令配置。