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 中一些常用命令和选项的详细介绍。你可以根据需求选择不同的选项来进行操作。

相关推荐
SelectDB13 小时前
Litefuse 开源并推出单进程轻量模式,25 秒就能跑起来的 Agent 可观测与评估平台
运维·后端·自动化运维
曲幽20 小时前
别再用网页翻译看源码了!你的私人翻译神器LibreTranslate,部署避坑指南来了
python·docker·web·pot·translate·libretranslate·arogstranslate
XIAOHEZIcode2 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
用户0328472220703 天前
如何搭建本地yum源(上)
运维
武子康3 天前
调查研究-183 Apple container:Mac 上用轻量 VM 跑 Linux 容器,Swift 会改写本地容器体验吗?
docker·容器·apple
大树886 天前
金刚石散热越强,管路越先见顶
大数据·运维·服务器·人工智能·ai
摇滚侠6 天前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
霸道流氓气质6 天前
领域驱动设计(DDD)在 Spring Boot 微服务中的实践指南
运维·spring boot·微服务
Inhand陈工6 天前
基于台达PLC与映翰通IG502的智慧水产养殖精准投喂与远程运维解决方案
运维·人工智能·物联网·阿里云·信息与通信
Alsn866 天前
等待学习-学习目录:Docker 容器安全攻防
学习·安全·docker