root@ruopu:~# docker run ubuntu:14.04 /bin/echo 'Hello World' Unable to find image 'ubuntu:14.04' locally 14.04: Pulling from library/ubuntu aa1a66b8583a: Pull complete aaccc2e362b2: Pull complete a53116a2808f: Pull complete b3a7298e318c: Pull complete Digest: sha256:f961d3d101e66017fc6f0a63ecc0ff15d3e7b53b6a0ac500cd1619ded4771bd6 Status: Downloaded newer image for ubuntu:14.04 Hello World # 新建并启动一个容器,因为本地没有ubuntu14.04的镜像,所以会先下载镜像并启动容器,并输出一个"Hello World"。运行完命令后这个容器就会终止。 root@ruopu:~# docker run ubuntu:14.04 /bin/echo 'Hello World' Hello World # 再次执行相同命令时就不会再下载了
root@ruopu:~# docker run -t -i ubuntu:14.04 /bin/bash root@eb5dd3071f75:/# # -t选项让Docker分配一个伪终端(pseudo-tty)并绑定到容器的标准输入上,-i则让容器的标准输入保持打开。 root@f55cb4d1089e:/# pwd / root@f55cb4d1089e:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var # 在交互模式下,用户可以通过所创建的终端来输入命令
root@ruopu:~# docker container stop sharp_booth sharp_booth # 可以使用stop命令终止容器,容器可以用NAMES或ID指明 root@ruopu:~# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 299d22cbda4f ubuntu:14.04 "/bin/sh -c 'while t…" 7 minutes ago Exited (137) 11 seconds ago sharp_booth e06be3875296 ubuntu:14.04 "/bin/sh -c 'while t…" 9 minutes ago Exited (0) 9 minutes ago recursing_vaughan 2d025ef6aa4c ubuntu:14.04 "/bin/sh -c 'while t…" 9 minutes ago Exited (0) 9 minutes ago jolly_merkle f2e9d5847f7f ubuntu:14.04 "/bin/sh -c 'while t…" 9 minutes ago Exited (2) 9 minutes ago keen_johnson f55cb4d1089e ubuntu:14.04 "/bin/bash" 6 hours ago Up 6 hours competent_feistel eb5dd3071f75 ubuntu:14.04 "/bin/bash" 7 hours ago Exited (0) 6 hours ago flamboyant_edison c43e0c0c2836 ubuntu:14.04 "/bin/echo 'Hello Wo…" 7 hours ago Exited (0) 7 hours ago mystifying_nobel 9a4c0b5b979b ubuntu:14.04 "/bin/echo 'Hello Wo…" 7 hours ago Exited (0) 7 hours ago focused_stallman 54b6e4618182 nginx:v2 "nginx -g 'daemon of…" 27 hours ago Up 27 hours 0.0.0.0:81->80/tcp web2 4fc2b542c0de nginx "nginx -g 'daemon of…" 28 hours ago Up 28 hours 0.0.0.0:80->80/tcp webserver c17f952574b5 hello-world "/hello" 28 hours ago Exited (0) 28 hours ago clever_bell # 可以使用此命令查看到所有的容器,包括已停止的容器,在信息中会被标记为"Exited"。仍在运行的容器被标记为"Up" # 如果是只启动了一个终端的容器,用户可以在容器中使用exit命令或Ctrl+d来退出终端,退出时创建的容器立刻终止。 root@ruopu:~# docker container restart f55c f55c # 使用restart命令可以重启容器,容器可以使用ID或NAMES指定
进入容器
attach命令(不建议使用)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
root@ruopu:~# docker run -dit ubuntu:14.04 64dda1b25fac61f165a0c7ab7c7807f9adf7ac173cdbdc907b0815d796ae0473 # 在后台启动一个容器 root@ruopu:~# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 64dda1b25fac ubuntu:14.04 "/bin/bash" About a minute ago Up About a minute gifted_swartz # 查看已启动的容器 root@ruopu:~# docker attach 64dd root@64dda1b25fac:/# # 使用attach进入容器。此时如果使用exit退出容器,那么会导致容器的停止。 root@64dda1b25fac:/# exit exit root@ruopu:~# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 64dda1b25fac ubuntu:14.04 "/bin/bash" 5 minutes ago Exited (0) 13 seconds ago gifted_swartz # 查看显示容器已停止
root@ruopu:~# docker run -dit ubuntu:14.04 c063d378b1191193d70d44c8fcb7a81e3e5bf0ab8a2f2cb1db4d526b6b510005 root@ruopu:~# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c063d378b119 ubuntu:14.04 "/bin/bash" 15 seconds ago Up 15 seconds agitated_hopper root@ruopu:~# docker exec -i c063 bash
ls bin boot dev ... # 只用-i参数时,由于没有分配伪终端,界面没有我们熟悉的Linux命令提示符,但命令执行结果仍然可以返回。直接输入命令就会返回结果。 root@ruopu:~# docker exec -it c063 bash root@c063d378b119:/# # 当-i、-t参数一起使用时,则可以看到我们熟悉的 Linux 命令提示符。 root@c063d378b119:/# exit exit root@ruopu:~# docker container ls CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c063d378b119 ubuntu:14.04 "/bin/bash" 4 minutes ago Up 4 minutes agitated_hopper # 如果使用exit退出容器,不会导致容器的停止。
导出、导入容器
导出容器
1 2 3 4 5
root@ruopu:~# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 9a4c0b5b979b ubuntu:14.04 "/bin/echo 'Hello Wo…" 7 hours ago Exited (0) 7 hours ago focused_stallman root@ruopu:~# docker export 9a4c0b5b979b > ubuntu.tar # 这样就将容器快照导出到本地文件
导入容器快照
1 2 3 4 5 6 7 8 9 10
root@ruopu:~# cat ubuntu.tar | docker import - test/ubuntu:v1.0 sha256:34a66c78f8af873bd07ab5b53073b4b2fbfd0a53cece350b54be9ef9910925ad root@ruopu:~# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE test/ubuntu v1.0 34a66c78f8af 11 seconds ago 175MB # 导入容器,容器的REPOSITORY是test/ubuntu,TAG是v1.0
root@ruopu:~# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c063d378b119 ubuntu:14.04 "/bin/bash" 14 minutes ago Up 14 minutes agitated_hopper 64dda1b25fac ubuntu:14.04 "/bin/bash" 20 minutes ago Exited (0) 15 minutes ago gifted_swartz 299d22cbda4f ubuntu:14.04 "/bin/sh -c 'while tb" 34 minutes ago Exited (137) 27 minutes ago root@ruopu:~# docker container rm gifted_swartz gifted_swartz root@ruopu:~# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c063d378b119 ubuntu:14.04 "/bin/bash" 14 minutes ago Up 14 minutes agitated_hopper 299d22cbda4f ubuntu:14.04 "/bin/sh -c 'while tb" 34 minutes ago Exited (137) 27 minutes ago
删除运行中的容器
1 2 3 4 5 6 7 8
root@ruopu:~# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c063d378b119 ubuntu:14.04 "/bin/bash" 14 minutes ago Up 14 minutes agitated_hopper root@ruopu:~# docker container rm -f f55c f55c # 使用-f选项强制删除运行中的容器,Docker 会发送SIGKILL信号给容器。通过容器ID删除容器 root@ruopu:~# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
清理所有处于终止状态的容器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
root@ruopu:~# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c063d378b119 ubuntu:14.04 "/bin/bash" 21 minutes ago Up 21 minutes ........ root@ruopu:~# docker container prune WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y Deleted Containers: 299d22cbda4f6d648095732350572999aa6937cad2d0d20db5ab37f09340c19e e06be3875296e3f0beaea61f8f48517c4e2ac39ee8852eef24bb2d6c871b0e12 ........ Total reclaimed space: 5B root@ruopu:~# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c063d378b119 ubuntu:14.04 "/bin/bash" 21 minutes ago Up 21 minutes agitated_hopper 54b6e4618182 nginx:v2 "nginx -g 'daemon ofb ........