1、自助查看docker所有命令
docker 客户端非常简单,可以直接输入 docker 命令来查看到 Docker 客户端的所有命令选项。
angqiang@wangqiang:~$ docker
Usage: docker [OPTIONS] COMMAND
A self-sufficient runtime for containers
Common Commands:
run Create and run a new container from an image
exec Execute a command in a running container
ps List containers
build Build an image from a Dockerfile
pull Download an image from a registry
push Upload an image to a registry
images List images
login Log in to a registry
logout Log out from a registry
search Search Docker Hub for images
version Show the Docker version information
info Display system-wide information
Management Commands:
builder Manage builds
container Manage containers
context Manage contexts
image Manage images
manifest Manage Docker image manifests and manifest lists
network Manage networks
plugin Manage plugins
system Manage Docker
trust Manage trust on Docker images
volume Manage volumes
Swarm Commands:
swarm Manage Swarm
Commands:
attach Attach local standard input, output, and error streams to a running container
commit Create a new image from a container's changes
cp Copy files/folders between a container and the local filesystem
create Create a new container
diff Inspect changes to files or directories on a container's filesystem
events Get real time events from the server
export Export a container's filesystem as a tar archive
history Show the history of an image
import Import the contents from a tarball to create a filesystem image
inspect Return low-level information on Docker objects
kill Kill one or more running containers
load Load an image from a tar archive or STDIN
logs Fetch the logs of a container
pause Pause all processes within one or more containers
port List port mappings or a specific mapping for the container
rename Rename a container
restart Restart one or more containers
rm Remove one or more containers
rmi Remove one or more images
save Save one or more images to a tar archive (streamed to STDOUT by default)
start Start one or more stopped containers
stats Display a live stream of container(s) resource usage statistics
stop Stop one or more running containers
tag Create a tag TARGET_IMAGE that refers to SOURCE_IMAGE
top Display the running processes of a container
unpause Unpause all processes within one or more containers
update Update configuration of one or more containers
wait Block until one or more containers stop, then print their exit codes
Global Options:
--config string Location of client config files (default "/home/wangqiang/.docker")
-c, --context string Name of the context to use to connect to the daemon (overrides
DOCKER_HOST env var and default context set with "docker
context use")
-D, --debug Enable debug mode
-H, --host list Daemon socket to connect to
-l, --log-level string Set the logging level ("debug", "info", "warn", "error",
"fatal") (default "info")
--tls Use TLS; implied by --tlsverify
--tlscacert string Trust certs signed only by this CA (default
"/home/wangqiang/.docker/ca.pem")
--tlscert string Path to TLS certificate file (default
"/home/wangqiang/.docker/cert.pem")
--tlskey string Path to TLS key file (default "/home/wangqiang/.docker/key.pem")
--tlsverify Use TLS and verify the remote
-v, --version Print version information and quit
Run 'docker COMMAND --help' for more information on a command.
For more help on how to use Docker, head to https://docs.docker.com/go/guides/
2、查看命令使用说明
可以通过命令 docker command --help 更深入的了解指定的 Docker 命令使用方法。
wangqiang@wangqiang:~$ docker ps --help
Usage: docker ps [OPTIONS]
List containers
Aliases:
docker container ls, docker container list, docker container ps, docker ps
Options:
-a, --all Show all containers (default shows just running)
-f, --filter filter Filter output based on conditions provided
--format string Format output using a custom template:
'table': Print output in table format with column
headers (default)
'table TEMPLATE': Print output in table format using the given
Go template
'json': Print in JSON format
'TEMPLATE': Print output using the given Go template.
Refer to https://docs.docker.com/go/formatting/ for more
information about formatting output with templates
-n, --last int Show n last created containers (includes all states) (default -1)
-l, --latest Show the latest created container (includes all states)
--no-trunc Don't truncate output
-q, --quiet Only display container IDs
-s, --size Display total file sizes
3、Docker 容器信息
(base) wq@wqdeMacBook-Pro ~ % docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
b15bf547fcc8 nginx "/docker-entrypoint.…" 34 minutes ago Up 34 minutes 0.0.0.0:8080->80/tcp zen_clarke
CONTAINER ID
- 值:
b15bf547fcc8
- 解释:
- 唯一标识符:Docker 为每个容器分配的唯一 ID(64 位十六进制字符串,通常显示前 12 位)。
- 用途:用于管理容器(如查看日志、停止、删除等操作)。
- 示例命令:
- 查看容器日志:
docker logs b15bf547fcc8
- 停止容器:
docker stop b15bf547fcc8
- 删除容器:
docker rm b15bf547fcc8
- Docker 允许通过 容器 ID 的前几位 来唯一标识容器,只要这些前几位字符能够唯一匹配目标容器。如:
docker rm b15bf。
- 如果系统中存在多个容器的 ID 前缀相同(例如
b15
匹配多个容器),Docker 会报错:Error: More than one container matches the criteria
- 查看容器日志:
IMAGE
- 值:
nginx
- 解释:
- 镜像来源:基于
nginx
镜像创建容器(默认从 Docker Hub 拉取,若未指定标签则使用latest
)。 - 镜像内容:包含 Nginx Web 服务器及其依赖的完整环境。
- 相关命令:
- 查看本地镜像:
docker images
- 拉取最新版本镜像:
docker pull nginx:latest
- 查看本地镜像:
- 镜像来源:基于
COMMAND
- 值:
"/docker-entrypoint.…"
- 解释:
- 命令含义:容器启动时执行的命令(由镜像的
CMD
或ENTRYPOINT
指定)。 - 具体行为:
/docker-entrypoint.sh
是 Nginx 镜像的入口脚本,负责启动 Nginx 服务(可能被截断,完整命令可能是/docker-entrypoint.sh nginx -g 'daemon off;'
)。 - 覆盖默认命令:
- 如果需要覆盖默认命令,可在
docker run
中指定新命令,例如:
- 如果需要覆盖默认命令,可在
- 命令含义:容器启动时执行的命令(由镜像的
docker run --name my-nginx -d nginx echo "Hello from Nginx container"
CREATED
- 值:
6 minutes ago
- 解释:
- 创建时间:容器创建的时间(相对于当前时间)。
- 相关命令:
- 查看所有容器的创建时间:
docker ps -a
- 按时间排序:
docker ps -n=2
(查看最近创建的 2 个容器),docker ps -n 2
- 查看所有容器的创建时间:
STATUS
- 值:
Up 6 minutes
- 解释:
- 容器状态:
Up
表示容器正在运行;Exited
表示已停止。 - 时间含义:容器已运行了 6 分钟。
- 相关操作:
- 停止容器:
docker stop zen_clarke
- 重启容器:
docker restart zen_clarke
- 停止容器:
- 容器状态:
PORTS
- 值:
0.0.0.0:8080->80/tcp
- 解释:
- 端口映射:
- 宿主机的
8080
端口 → 容器的80
端口(HTTP 服务默认端口)。 0.0.0.0
代表主机的所有网络接口,这样任何能访问主机IP的设备都可以通过8080端口访问容器。
- 宿主机的
- 端口映射:
0.0.0.0:8080
:
表示宿主机(运行 Docker 的机器)的 8080 端口被监听。0.0.0.0
是一个特殊 IP 地址,表示监听 所有网络接口(包括本地回环接口127.0.0.1
、物理网卡接口、虚拟接口等)。
- 作用:允许任何能访问宿主机 IP 地址的设备(如局域网内的其他设备、公网用户)通过
8080
端口访问容器。
->80/tcp
:
表示将宿主机的8080
端口 映射到容器的 80 端口,且使用 TCP 协议。
- 作用:当外部请求到达宿主机的
8080
端口时,Docker 会将请求转发到容器的80
端口(通常用于 Web 服务)。- 每个容器都有一个独立的 网络命名空间,相当于一个独立的虚拟网络环境。因此,容器 A 的
80
端口和容器 B 的80
端口是完全隔离的。
0.0.0.0
只是表示 容器内部的服务会监听所有网络接口,但实际能否被外部访问,还取决于以下因素:(1) 宿主机的网络环境
局域网内:
如果宿主机位于局域网(如家庭路由器下),其他设备可以通过宿主机的局域网 IP(如192.168.2.22:8080
)访问容器服务。互联网访问:
如果希望 公网用户 通过互联网访问容器服务,需要满足以下条件:
- 宿主机的防火墙允许 8080 端口:
宿主机的防火墙(如 Linux 的iptables
、Windows 防火墙)需要开放8080
端口。- 路由器/云服务的安全组允许 8080 端口:
如果宿主机位于云服务器(如 AWS、阿里云)或家庭路由器后,需要配置云服务的安全组或路由器的端口转发规则,允许外部流量通过8080
端口进入宿主机。- 公网 IP 地址:
宿主机需要有一个公网 IP 地址(或通过域名解析到公网 IP),否则外部无法直接访问。- ubuntu系统查询公网IP地址:
# 直接返回当前宿主机的公网 IP 地址 curl ifconfig.me # ifconfig.me 是由第三方提供的公共 IP 查询服务(由 ifconfig.me 提供) curl http://checkip.amazonaws.com # checkip.amazonaws.com 是 Amazon AWS 提供的官方服务(AWS 的公共 DNS),它会返回请求方的公网 IP 地址(即你的宿主机所在网络的出口 IP)。如果宿主机本身没有公网 IP(如家庭宽带),则返回的是路由器的公网 IP。 # 查看本机网络接口的配置 # 显示当前系统所有网络接口的 IP 地址配置信息 ip addr show # 显示本机的 内网 IP 和路由信息(如 192.168.2.22 和 enp3s0 接口) ip route get 8.8.8.8
(2) 容器内部服务的配置
- 容器内的服务(如 Nginx、Web 应用)必须监听
0.0.0.0:80
,而不是127.0.0.1:80
。
# mac查询公网ip
curl ipinfo.io/ip
curl ifconfig.me
# mac查询内网ip
ipconfig getifaddr en0
ifconfig | grep "inet "
NAMES
- 值:
zen_clarke
- 解释:
- 容器名称:由 Docker 自动生成(若未指定则为随机名称,如
zen_clarke
)。 - 自定义名称:
- 创建时可通过
--name
参数指定名称:
- 创建时可通过
- 容器名称:由 Docker 自动生成(若未指定则为随机名称,如
docker run --name my-nginx -d -p 8080:80 nginx
- 用途:便于管理和识别容器(如
docker stop my-nginx
)。
CONTAINER ID和NAMES对比
特性 | CONTAINER ID | NAMES |
---|---|---|
唯一性 | 始终唯一,不可重复 | 可能重复(需 Docker 自动处理冲突) |
长度 | 通常为 12 位十六进制(完整为 64 位) | 用户自定义,长度限制为 63 个字符 |
稳定性 | 稳定,不会变化 | 可能变化(如容器重启后名称可能重用) |
用途 | 自动化脚本、调试、精准操作 | 人工管理、直观标识容器用途 |
示例 | 93cd2524e98b | ollama 、my-db 、redis-cache |