attach
用于连接到正在运行中的容器。
要attach上去的容器必须正在运行,同一个container是允许同时被连接,但attach链接的容器,输入的操作在其他attach同一个容器的终端都会同步,很容易互相影响,不推荐这种方式进入容器进行操作。退出attach使用CTRL-C
,退出的时候需要注意,会给进程发送信号,可能会导致进程退出。
用法
1 | docker attach [OPTIONS] CONTAINER |
选项
选项 | 描述 |
---|---|
--detach-keys string |
Override the key sequence for detaching a container |
--no-stdin |
过滤输入操作 |
--sig-proxy |
退出的时候是否给进程发送信号,默认是true |
例子
1 | # 连接hello-world容易查看日志信息 |
build
用于使用 Dockerfile 创建镜像
用法
1 | docker build [OPTIONS] PATH | URL | - |
选项
选项 | 描述 |
---|---|
--add-host list |
Add a custom host-to-IP mapping (host:ip) |
--build-arg list |
设置镜像创建时的变量 |
--cache-from strings |
Images to consider as cache sources |
--cgroup-parent string |
Optional parent cgroup for the container |
--compress |
Compress the build context using gzip |
--cpu-period int |
限制 CPU CFS周期 |
--cpu-quota int |
限制 CPU CFS配额 |
-c, --cpu-shares int |
设置 cpu 使用权重 |
--cpuset-cpus string |
指定使用的CPU id (0-3, 0,1) |
--cpuset-mems string |
指定使用的内存 id (0-3, 0,1) |
--disable-content-trust |
忽略校验,默认开启 (default true) |
-f, --file string |
指定要使用的Dockerfile路径 (Default is ‘PATH/Dockerfile’) |
--force-rm |
设置镜像过程中删除中间容器 |
--iidfile string |
Write the image ID to the file |
--isolation string |
使用容器隔离技术 |
--label list |
设置镜像使用的元数据 |
-m, --memory bytes |
设置内存最大值 |
--memory-swap bytes |
设置Swap的最大值为内存+swap,”-1”表示不限swap |
--network string |
默认 default。在构建期间设置RUN指令的网络模式 (default “default”) |
--no-cache |
创建镜像的过程不使用缓存 |
--pull |
尝试去更新镜像的新版本 |
-q, --quiet |
安静模式,成功后只输出镜像 ID |
--rm |
设置镜像成功后删除中间容器 (default true) |
--security-opt strings |
Security options |
--shm-size bytes |
设置/dev/shm的大小,默认值是64M |
-t, --tag list |
镜像的名字及标签,通常 name:tag 或者 name 格式;可以在一次构建中为一个镜像设置多个标签 |
--target string |
Set the target build stage to build. |
--ulimit ulimit |
Ulimit配置 (default []) |
例子
1 | # 使用当前目录的 Dockerfile 创建镜像,标签为 hello-world:v1 |
commit
用于从容器创建一个新的镜像。
用法
1 | docker commit [OPTIONS] CONTAINER [REPOSITORY[:TAG]] |
选项
选项 | 描述 |
---|---|
-a, --author string |
提交的镜像作者名称 |
-c, --change list |
使用Dockerfile指令来创建镜像 |
-m, --message string |
提交时的说明文字 |
-p, --pause |
在commit时,将容器暂停(默认是 true) |
例子
1 | # 将容器e2ca0ed2f18c保存为新的镜像,并添加提交人信息和说明信息,且打上标签v1 |
container
用于管理docker容器
用法
1 | docker container COMMAND |
选项
选项 | 描述 |
---|---|
例子
1 | # 查看容器tomcat1的网络信息 |
cp
用于容器与主机之间的数据拷贝
用法
1 | docker cp [OPTIONS] CONTAINER:SRC_PATH DEST_PATH|- |
选项
选项 | 描述 |
---|---|
-a, --archive |
数据模式(copy all uid/gid information) |
-L, --follow-link |
保持源目标中的链接 |
例子
1 | # 将主机/home/shll目录拷贝到容器e2ca0ed2f18c的/home目录下,重名为shll2。 |
create
用于创建一个新的容器但不启动它
用法
1 | docker create [OPTIONS] IMAGE [COMMAND] [ARG...] |
选项
选项 | 描述 |
---|---|
--add-host list |
Add a custom host-to-IP mapping (host:ip) |
--attach list |
Attach to STDIN, STDOUT or STDERR |
--blkio-weight uint16 |
Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) |
--blkio-weight-device list |
Block IO weight (relative device weight) (default []) |
--cap-add list |
Add Linux capabilities |
--cap-drop list |
Drop Linux capabilities |
--cgroup-parent string |
Optional parent cgroup for the container |
--cidfile string |
Write the container ID to the file |
--cpu-period int |
Limit CPU CFS (Completely Fair Scheduler) period |
--cpu-quota int |
Limit CPU CFS (Completely Fair Scheduler) quota |
--cpu-rt-period int |
Limit CPU real-time period in microseconds |
--cpu-rt-runtime int |
Limit CPU real-time runtime in microseconds |
--cpu-shares int |
CPU shares (relative weight) |
--cpus decimal |
Number of CPUs |
--cpuset-cpus string |
CPUs in which to allow execution (0-3, 0,1) |
--cpuset-mems string |
MEMs in which to allow execution (0-3, 0,1) |
--device list |
Add a host device to the container |
--device-cgroup-rule list |
Add a rule to the cgroup allowed devices list |
--device-read-bps list |
Limit read rate (bytes per second) from a device (default []) |
--device-read-iops list |
Limit read rate (IO per second) from a device (default []) |
--device-write-bps list |
Limit write rate (bytes per second) to a device (default []) |
--device-write-iops list |
Limit write rate (IO per second) to a device (default []) |
--disable-content-trust |
Skip image verification (default true) |
--dns list |
Set custom DNS servers |
--dns-option list |
Set DNS options |
--dns-search list |
Set custom DNS search domains |
--entrypoint string |
Overwrite the default ENTRYPOINT of the image |
--env list |
Set environment variables |
--env-file list |
Read in a file of environment variables |
--expose list |
Expose a port or a range of ports |
--group-add list |
Add additional groups to join |
--health-cmd string |
Command to run to check health |
--health-interval duration |
Time between running the check (ms|s|m|h) (default 0s) |
--health-retries int |
Consecutive failures needed to report unhealthy |
--health-start-period duration |
Start period for the container to initialize before starting health-retries countdown (ms|s|m|h) (default 0s) |
--health-timeout duration |
Maximum time to allow one check to run (ms|s|m|h) (default 0s) |
--help |
Print usage |
--hostname string |
Container host name |
--init |
Run an init inside the container that forwards signals and reaps processes |
--interactive |
Keep STDIN open even if not attached |
--ip string |
IPv4 address (e.g., 172.30.100.104) |
--ip6 string |
IPv6 address (e.g., 2001:db8::33) |
--ipc string |
IPC mode to use |
--isolation string |
Container isolation technology |
--kernel-memory bytes |
Kernel memory limit |
--label list |
Set meta data on a container |
--label-file list |
Read in a line delimited file of labels |
--link list |
Add link to another container |
--link-local-ip list |
Container IPv4/IPv6 link-local addresses |
--log-driver string |
Logging driver for the container |
--log-opt list |
Log driver options |
--mac-address string |
Container MAC address (e.g., 92:d0:c6:0a:29:33) |
--memory bytes |
Memory limit |
--memory-reservation bytes |
Memory soft limit |
--memory-swap bytes |
Swap limit equal to memory plus swap: ‘-1’ to enable unlimited swap |
--memory-swappiness int |
Tune container memory swappiness (0 to 100) (default -1) |
--mount mount |
Attach a filesystem mount to the container |
--name string |
给容器实例命名 |
--network string |
Connect a container to a network (default “default”) |
--network-alias list |
Add network-scoped alias for the container |
--no-healthcheck |
Disable any container-specified HEALTHCHECK |
--oom-kill-disable |
Disable OOM Killer |
--oom-score-adj int |
Tune host’s OOM preferences (-1000 to 1000) |
--pid string |
PID namespace to use |
--pids-limit int |
Tune container pids limit (set -1 for unlimited) |
--privileged |
Give extended privileges to this container |
--publish list |
Publish a container’s port(s) to the host |
--publish-all |
Publish all exposed ports to random ports |
--read-only |
Mount the container’s root filesystem as read only |
--restart string |
Restart policy to apply when a container exits (default “no”) |
--rm |
Automatically remove the container when it exits |
--runtime string |
Runtime to use for this container |
--security-opt list |
Security Options |
--shm-size bytes |
Size of /dev/shm |
--stop-signal string |
Signal to stop a container (default “SIGTERM”) |
--stop-timeout int |
Timeout (in seconds) to stop a container |
--storage-opt list |
Storage driver options for the container |
--sysctl map |
Sysctl options (default map[]) |
--tmpfs list |
Mount a tmpfs directory |
--tty |
Allocate a pseudo-TTY |
--ulimit ulimit |
Ulimit options (default []) |
--user string |
Username or UID (format: <name|uid>[:<group|gid>]) |
--userns string |
User namespace to use |
--uts string |
UTS namespace to use |
--volume list |
Bind mount a volume |
--volume-driver string |
Optional volume driver for the container |
--volumes-from list |
Mount volumes from the specified container(s) |
--workdir string |
Working directory inside the container |
例子
1 | # 创建一个hello-world容器的实例,命名为hhello |
diff
用于检查容器里文件结构的更改
用法
1 | docker diff CONTAINER |
例子
1 | # 查看容器实例e2ca0ed2f18c的文件结构变更 |
event
用于从服务器获取实时事件。
用法
1 | docker events [OPTIONS] |
选项
选项 | 描述 |
---|---|
-f |
根据条件过滤事件; |
--since |
从指定的时间戳后显示所有事件; |
--until |
流水时间显示到指定的时间为止; |
例子
1 | # 显示docker 2019年4月1日后的所有事件。 |
exec
用于在运行的容器中执行命令
用法
1 | docker exec [OPTIONS] CONTAINER COMMAND [ARG...] |
选项
选项 | 描述 |
---|---|
-d, --detach |
分离模式: 在后台运行命令 |
--detach-keys string |
Override the key sequence for detaching a container |
-e, --env list |
Set environment variables |
-i, --interactive |
即使没有附加也保持标准输入STDIN的打开 |
--privileged |
Give extended privileges to the command |
-t, --tty |
分配一个伪终端 |
-u, --user string |
Username or UID (format: <name|uid>[:<group|gid>]) |
-w, --workdir string |
Working directory inside the container |
例子
1 | # 以交互模式执行容器实例hhello的/home/restart.sh脚本 |
export
用于导出打包容器的文件系统成tar,作为容器的容器包使用
用法
1 | docker export [OPTIONS] CONTAINER |
选项
选项 | 描述 |
---|---|
-o或--output -string |
将输入内容写到文件。 |
例子
1 | # 打包hello-world的实例容器e2ca0ed2f18c |
history
用于查看指定镜像的创建历史
用法
1 | docker history [OPTIONS] IMAGE |
选项
选项 | 描述 |
---|---|
--format string |
以go模板格式打印日志 |
-H, --human |
以可读的格式打印镜像大小和日期 (default true) |
--no-trunc |
显示完整的提交记录 |
-q, --quiet |
仅列出提交记录ID |
例子
1 | # 查看本地镜像hello-world的创建历史。 |
images
用于列出本地镜像
用法
1 | docker images [OPTIONS] [REPOSITORY[:TAG]] |
选项
选项 | 描述 |
---|---|
-a, --all |
列出本地所有的镜像,含中间映像层(默认情况下,过滤掉中间映像层) |
--digests |
显示镜像的摘要信息 |
-f, --filter filter |
显示满足条件的镜像 |
--format string |
使用go目标显示打印 |
--no-trunc |
显示完整的镜像信息 |
-q, --quiet |
只显示镜像ID |
例子
1 | # 显示本地所有镜像 |
import
用于从export导出的容器包中创建镜像
用法
1 | docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]] |
选项
选项 | 描述 |
---|---|
-c, --change list |
使用Dockerfile 命令去创建镜像 |
-m, --message string |
导入镜像时设置提交的信息 |
例子
1 | # 从容器包hello.tar创建镜像,命名为hello:v3 |
info
用于显示 Docker 系统信息,包括镜像和容器数
用法
1 | docker info [OPTIONS] |
选项
选项 | 描述 |
---|---|
-f, --format string |
使用go模板格式打印信息 |
例子
1 | # 查看docker系统信息 |
inspect
用于获取容器/镜像的元数据
用法
1 | docker inspect [OPTIONS] NAME|ID [NAME|ID...] |
选项
选项 | 描述 |
---|---|
-f或--format |
指定返回值的模板文件。 |
-s或--size |
显示总的文件大小。 |
--type |
为指定类型返回JSON。 |
例子
1 | # 获取镜像hello-world的元信息 |
kill
用于杀掉一个运行中的容器。
用法
1 | docker kill [OPTIONS] CONTAINER [CONTAINER...] |
选项
选项 | 描述 |
---|---|
-s |
向容器发送一个信号(默认是KILL) |
例子
1 | # 杀掉运行中的容器实例hello-world,并发送KILL信号 |
load
用于导入使用 docker save 命令导出的镜像
用法
1 | docker load [OPTIONS] |
选项
选项 | 描述 |
---|---|
-i, --input string |
指定要加载的的文件 |
-q, --quiet |
精简输出加载的时候的信息 |
例子
1 | # 加载hello-world.tar镜像归档 |
login
用于登陆到一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub
用法
1 | docker login [OPTIONS] [SERVER] |
选项
选项 | 描述 |
---|---|
-p, --password string |
登陆的密码 |
--password-stdin |
标准输入输入密码 |
-u, --username string |
登陆的用户名 |
例子
1 | # 登录hello用户 |
logout
登出一个Docker镜像仓库,如果未指定镜像仓库地址,默认为官方仓库 Docker Hub
用法
1 | docker logout [SERVER] |
例子
1 | # 登出当前用户 |
logs
用于获取容器的日志
用法
1 | docker logs [OPTIONS] CONTAINER |
选项
选项 | 描述 |
---|---|
-f |
跟踪日志输出 |
--since |
显示某个开始时间的所有日志 |
-t |
显示时间戳 |
--tail |
仅列出最新N条容器日志 |
例子
1 | # 跟踪容器hello-world的日志输出 |
pause
用于暂停容器中所有的进程
用法
1 | docker pause CONTAINER [CONTAINER...] |
例子
1 | # 暂停容器实例hello-world的服务 |
port
用于列出指定的容器的端口映射。
用法
1 | docker port CONTAINER [PRIVATE_PORT[/PROTO]] |
选项
选项 | 描述 |
---|---|
-o或--output -string |
将输入内容写到文件。 |
例子
1 | # 列出hello-world的实例容器e2ca0ed2f18c的端口信息 |
ps
用于列出容器
用法
1 | docker ps [OPTIONS] |
选项
选项 | 描述 |
---|---|
-a |
显示所有的容器,包括未运行的。 |
-f |
根据条件过滤显示的内容。 |
--format |
指定返回值的模板文件。 |
-l |
显示最近创建的容器。 |
-n |
列出最近创建的n个容器。 |
--no-trunc |
不截断输出。 |
-q |
静默模式,只显示容器编号。 |
-s |
显示总的文件大小。 |
例子
1 | # 列出所有在运行的容器信息 |
network
用于创建和管理docker的网络
用法
1 | docker network COMMAND |
选项
选项 | 描述 |
---|---|
connect |
将容器连接到网络 |
create |
创建一个网络 |
disconnect |
将容器的从网络断开 |
inspect |
查看网络的详情 |
ls |
把docker的网络所有网络列出来 |
prune |
移除所有不使用的网络 |
rm |
移除一个或多个网络 |
例子
1 | # |
pull
用于从镜像仓库中拉取或者更新指定镜像
用法
1 | docker pull [OPTIONS] NAME[:TAG|@DIGEST] |
选项
选项 | 描述 |
---|---|
-a, --all-tags |
拉取所有标签的镜像 |
--disable-content-trust |
忽略镜像的校验,(默认true true) |
例子
1 | # 从Docker Hub下载java最新版镜像。 |
push
用于将本地的镜像上传到镜像仓库,要先登陆到镜像仓库
用法
1 | docker push [OPTIONS] NAME[:TAG] |
选项
选项 | 描述 |
---|---|
--disable-content-trust |
忽略镜像的校验,(默认true true) |
例子
1 | # 上传本地镜像hello-world:v1到镜像仓库中 |
rename
用于给容器起新的名字
用法
1 | docker rename CONTAINER NEW_NAME |
例子
1 | # 将容器实例e2ca0ed2f18c的名字改成hellonew |
restart
用于重新启动容器
用法
1 | docker restart CONTAINER [CONTAINER...] |
例子
1 | # 重启动已经被停止的hello-world容器 |
rm
用于删除一个或多少容器
用法
1 | docker rm [OPTIONS] CONTAINER [CONTAINER...] |
选项
选项 | 描述 |
---|---|
-f或--force |
通过SIGKILL信号强制删除一个运行中的容器 |
-l或--link |
移除容器间的网络连接,而非容器本身 |
-v或--volumes |
删除与容器关联的卷 |
例子
1 | # 强制删除容器实例hello-world,并删除容器挂载的数据卷 |
rmi
用于删除本地一个或多少镜像
用法
1 | docker rmi [OPTIONS] IMAGE [IMAGE...] |
选项
选项 | 描述 |
---|---|
-f, --force |
强制删除 |
--no-prune |
不移除没有标签的镜像 |
例子
1 | # 强制删除本地镜像hello:v2 |
run
用于创建一个新的容器并运行一个命令
用法
1 | docker run [OPTIONS] IMAGE [COMMAND] [ARG...] |
选项
选项 | 描述 |
---|---|
-a stdin |
指定标准输入输出内容类型,可选 STDIN/STDOUT/STDERR 三项; |
-d |
后台运行容器,并返回容器ID; |
-i |
以交互模式运行容器,通常与 -t 同时使用; |
-p |
端口映射,格式为:主机(宿主)端口:容器端口 |
-t |
为容器重新分配一个伪输入终端,通常与 -i 同时使用; |
--name="nginx-lb" |
为容器指定一个名称; |
--dns 8.8.8.8 |
指定容器使用的DNS服务器,默认和宿主一致; |
--dns-search example.com |
指定容器DNS搜索域名,默认和宿主一致; |
-h "mars" |
指定容器的hostname; |
-e username="ritchie" |
设置环境变量; |
--env-file=[] |
从指定文件读入环境变量; |
--cpuset="0-2" or --cpuset="0,1,2" |
绑定容器到指定CPU运行; |
-m |
设置容器使用内存最大值; |
--net="bridge" |
指定容器的网络连接类型,支持 bridge/host/none/container: 四种类型; |
--link=[] |
添加链接到另一个容器; |
--expose=[] |
开放一个端口或一组端口; |
例子
1 | # 以后台模式启动一个nginx:latest容器,并将容器命名为mynginx。 |
save
用于将指定镜像保存成tar归档文件
用法
1 | docker save [OPTIONS] IMAGE [IMAGE...] |
选项
选项 | 描述 |
---|---|
-o, --output string |
输出到的文件名 |
例子
1 | # 将镜像hello-world生成hello-world.tar |
search
用于从Docker Hub查找镜像
用法
1 | docker search [OPTIONS] TERM |
选项
选项 | 描述 |
---|---|
-f, --filter filter |
根据提供的条件筛选搜索条件 |
--format string |
使用go目标打印搜索输出 |
--limit int |
最大搜索结果显示数量(默认 25) |
--no-trunc |
显示完整的镜像描述 |
例子
1 | # 搜索官网的hello镜像 |
start
用于启动一个或多个已经被停止的容器
用法
1 | docker start [OPTIONS] CONTAINER [CONTAINER...] |
选项
选项 | 描述 |
---|---|
-a, --attach |
Attach STDOUT/STDERR and forward signals |
--detach-keys string |
Override the key sequence for detaching a container |
-i, --interactive |
Attach container’s STDIN |
例子
1 | # 启动已经被停止的hello-world容器 |
stats
stop
用于停止一个运行中的容器
用法
1 | docker stop [OPTIONS] CONTAINER [CONTAINER...] |
选项
选项 | 描述 |
---|---|
-t, --time int |
需要等待多少秒才关闭容器(默认值 10秒) |
例子
1 | # 关闭容器实例hello-world |
tag
标记本地镜像,并将其加入到仓库。主要仓库名不一样就成了新的仓库了而不是版本。
用法
1 | docker tag SOURCE_IMAGE[:TAG] TARGET_IMAGE[:TAG] |
例子
1 | # 将镜像hello-world标记为hello:v2镜像。 |
top
用于查看容器中运行的进程信息,支持 ps 命令参数。
用法
1 | docker top CONTAINER [ps OPTIONS] |
例子
1 | # 查看容器hello-world的进程信息 |
unpause
用于恢复容器中所有的进程的运行
用法
1 | docker unpause CONTAINER [CONTAINER...] |
例子
1 | # 恢复容器实例hello-world的服务 |
update
用于更新一个或多个容器的配置
用法
1 | docker update [OPTIONS] CONTAINER [CONTAINER...] |
选项
选项 | 描述 |
---|---|
--blkio-weight uint16 |
Block IO (relative weight), between 10 and 1000, or 0 to disable (default 0) |
--cpu-period int |
Limit CPU CFS (Completely Fair Scheduler) period |
--cpu-quota int |
Limit CPU CFS (Completely Fair Scheduler) quota |
--cpu-rt-period int |
Limit the CPU real-time period in microseconds |
--cpu-rt-runtime int |
Limit the CPU real-time runtime in microseconds |
-c, --cpu-shares int |
CPU shares (relative weight) |
--cpus decimal |
Number of CPUs |
--cpuset-cpus string |
CPUs in which to allow execution (0-3, 0,1) |
--cpuset-mems string |
MEMs in which to allow execution (0-3, 0,1) |
--kernel-memory bytes |
Kernel memory limit |
-m, --memory bytes |
Memory limit |
--memory-reservation bytes |
Memory soft limit |
--memory-swap bytes |
Swap limit equal to memory plus swap: ‘-1’ to enable unlimited swap |
--restart string |
Restart policy to apply when a container exits |
例子
1 | # ?? |
version
用于显示 Docker 版本信息
用法
1 | docker version [OPTIONS] |
选项
选项 | 描述 |
---|---|
-f, --format string |
使用go模板格式化显示信息 |
--kubeconfig string |
Kubernetes config file |
例子
1 | # 显示 Docker 版本信息。 |
volume
用于显示 Docker 版本信息
用法
1 | docker volume COMMAND |
选项
选项 | 描述 |
---|---|
create |
创建一个新的volume |
inspect |
查看volume的信息 |
ls |
将docker的volume都显示处理 |
prune |
删除所有的不使用的volume |
rm |
删除一个或多个volume |
例子
1 | # 新建一个名为v1的volume |
wait
用于阻塞运行直到容器停止,然后打印出它的退出代码。
用法
1 | docker wait CONTAINER [CONTAINER...] |
例子
1 | # 阻塞等待hello-world容器的退出,然后打印退出码 |
export和save比较
参考