docker 命令

Docker 命令提供了许多选项,用于执行各种任务,如运行、停止、管理容器、镜像、网络、卷等操作。下面将详细讲解 Docker 各个命令的主要选项以及它们的作用。

1. docker 命令的通用选项

这些选项可以在大多数 Docker 命令中使用,它们用于配置 Docker 引擎或全局设置。

  • -D, --debug:启用调试模式,输出更多调试信息。

    bash 复制代码
    docker --debug run nginx
  • -H, --host:连接到 Docker 守护进程的地址,通常用于远程 Docker 主机的操作。

    bash 复制代码
    docker -H tcp://192.168.1.10:2376 run nginx
  • --config :指定 Docker CLI 配置文件的目录,默认是 ~/.docker

    bash 复制代码
    docker --config ~/my-docker-config/ run nginx
  • --context:指定 Docker 上下文(context),用于管理多个 Docker 主机环境。

    bash 复制代码
    docker --context myremote run nginx
  • --tls:启用与 Docker 守护进程的 TLS 安全连接。

    bash 复制代码
    docker --tls run nginx
  • --tlsverify:启用 TLS 连接并验证服务器的 TLS 证书。

    bash 复制代码
    docker --tlsverify run nginx

2. 镜像操作选项

2.1 docker pull 选项
  • --all-tags:下载指定镜像的所有标签(版本)。

    bash 复制代码
    docker pull --all-tags nginx
  • --platform :拉取适用于指定平台的镜像(如 linux/amd64, linux/arm64)。

    bash 复制代码
    docker pull --platform linux/arm64 nginx
2.2 docker build 选项
  • -t, --tag:为构建的镜像指定名称和标签(版本)。

    bash 复制代码
    docker build -t myapp:1.0 .
  • -f, --file :指定 Dockerfile 文件的路径(默认为 Dockerfile)。

    bash 复制代码
    docker build -f ./Dockerfile.custom -t myapp:custom .
  • --no-cache:不使用缓存,强制重新构建每一层。

    bash 复制代码
    docker build --no-cache -t myapp:1.0 .
  • --build-arg:设置构建时的环境变量。

    bash 复制代码
    docker build --build-arg VERSION=1.0 -t myapp:1.0 .
  • --platform:为指定平台构建镜像。

    bash 复制代码
    docker build --platform linux/amd64 -t myapp:1.0 .

3. 容器操作选项

3.1 docker run 选项
  • -d, --detach:以后台模式运行容器(守护进程模式)。

    bash 复制代码
    docker run -d nginx
  • -i, --interactive:保持标准输入(stdin)打开,即使未附加终端。

    bash 复制代码
    docker run -i busybox
  • -t, --tty :分配一个伪终端,通常与 -i 一起使用。

    bash 复制代码
    docker run -it ubuntu /bin/bash
  • -p, --publish :将主机端口映射到容器端口,格式为 主机端口:容器端口

    bash 复制代码
    docker run -p 8080:80 nginx
  • -P, --publish-all:随机将容器的所有公开端口映射到主机的高位端口。

    bash 复制代码
    docker run -P nginx
  • --name:为容器指定一个自定义名称。

    bash 复制代码
    docker run --name mynginx nginx
  • --rm:容器停止后自动删除容器。

    bash 复制代码
    docker run --rm nginx
  • -e, --env:设置环境变量,传递到容器内部。

    bash 复制代码
    docker run -e "MY_VAR=value" nginx
  • -v, --volume :挂载主机目录或数据卷到容器,格式为 主机路径:容器路径

    bash 复制代码
    docker run -v /host/data:/container/data nginx
  • --network:指定容器连接到的网络。

    bash 复制代码
    docker run --network mynetwork nginx
  • --restart :为容器设置重启策略(如 no, on-failure, always)。

    bash 复制代码
    docker run --restart always nginx
  • --cpus:限制容器可使用的 CPU 核数。

    bash 复制代码
    docker run --cpus="1.5" nginx
  • --memory:限制容器可使用的内存大小。

    bash 复制代码
    docker run --memory="500m" nginx
  • --gpus:为容器分配 GPU 资源(需要安装 NVIDIA Docker)。

    bash 复制代码
    docker run --gpus all tensorflow/tensorflow:latest-gpu

3.2 docker ps 选项
  • -a, --all:列出所有容器,包括未运行的容器。

    bash 复制代码
    docker ps -a
  • -q, --quiet:仅显示容器的 ID。

    bash 复制代码
    docker ps -q
  • --filter:根据条件过滤容器,如根据状态或名称。

    bash 复制代码
    docker ps --filter "status=exited"
  • --format :自定义输出格式,类似于 Go 的模板语法。

    bash 复制代码
    docker ps --format "{{.ID}}: {{.Names}}"
3.3 docker stop 选项
  • -t, --time :等待容器停止的超时时间(默认 10 秒)。

    bash 复制代码
    docker stop -t 5 mycontainer

4. 网络操作选项

4.1 docker network create 选项
  • --driver :指定网络驱动(如 bridge, overlay, host)。

    bash 复制代码
    docker network create --driver bridge mynetwork
  • --subnet:指定自定义子网 CIDR。

    bash 复制代码
    docker network create --subnet=192.168.1.0/24 mynetwork
  • --gateway:指定自定义网关 IP 地址。

    bash 复制代码
    docker network create --gateway=192.168.1.1 mynetwork
  • --attachable :允许非 swarm 模式容器连接到 overlay 网络(仅用于 overlay 网络)。

    bash 复制代码
    docker network create --driver overlay --attachable myoverlay

5. 卷操作选项

5.1 docker volume create 选项
  • --driver :指定卷驱动程序(如 local、云存储驱动等)。

    bash 复制代码
    docker volume create --driver local myvolume
  • --opt:为卷驱动程序指定自定义选项。

    bash 复制代码
    docker volume create --opt type=tmpfs --opt device=tmpfs myvolume
  • --label:为卷设置标签。

    bash 复制代码
    docker volume create --label project=myapp myvolume
5.2 docker volume rm 选项
  • -f, --force :强制删除卷,即使卷已被使用。

    bash 复制代码
    docker volume rm -f myvolume

6. 其他操作选项

6.1 docker exec 选项
  • -i, --interactive:保持标准输入打开。

    bash 复制代码
    docker exec -i mycontainer /bin/bash
  • -t, --tty:分配一个伪终端。

    bash 复制代码
    docker exec -it mycontainer /bin/bash
6.2 docker logs 选项
  • **`

-f, --follow`**:实时输出容器日志。

bash 复制代码
docker logs -f mycontainer
  • --tail :仅显示日志的最后几行。

    bash 复制代码
    docker logs --tail 10 mycontainer
6.3 docker inspect 选项
  • --format :自定义输出格式,类似于 Go 的模板语法。

    bash 复制代码
    docker inspect --format '{{.State.Status}}' mycontainer
6.4 docker prune 选项
  • -a, --all:删除所有未使用的镜像,包括中间镜像。

    bash 复制代码
    docker system prune -a
  • -f, --force:强制删除而不提示确认。

    bash 复制代码
    docker system prune -f

这是 Docker 中一些常用命令和选项的详细介绍。你可以根据需求选择不同的选项来进行操作。

相关推荐
叱咤少帅(少帅)22 分钟前
IDC机房机柜部署
运维
Jelly-小丑鱼24 分钟前
Linux搭建syslog日志服务器
linux·服务器·docker·日志服务器·syslog服务器
没有bug.的程序员41 分钟前
高频IO服务优化实战指南
java·jvm·spring·容器
阿巴阿巴boer1 小时前
用wsl搭建远程linux服务器
linux·运维·服务器·ssh
lisanmengmeng2 小时前
docker 方式安装部署禅道zentao(五)
运维·docker·容器
程序员老赵2 小时前
AdguardHome Docker 容器化部署指南
docker·dns
wanhengidc3 小时前
云手机的硬件依赖性如何?
运维·服务器·智能手机·云计算
piaoroumi3 小时前
UVC调试
linux·运维·前端
VekiSon3 小时前
Linux系统编程——标准IO
linux·运维·服务器
Evan芙3 小时前
DNS服务器类型,解析答案,正反解析域,资源记录定义总结
运维·服务器