docker 容器

时间:2022-07-22
本文章向大家介绍docker 容器,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

容器介绍

容器是独立运行的一个或一组应用,以及他们的运行环境,对应的,虚拟机可以理解为模拟运行的一整套操作系统(提供了运行态环境和其他系统环境)和跑在上面的应用。

启动容器

启动容器有两种方式,一种是基于镜像新建一个容器并启动,另外一个是将在终止状态的容器重新启动

例如:

1 2 3 4 5 6 7 8 9 10 11 12 13

[root@xs_test01 docker]# docker run ubuntu:14.04 /bin/echo "hello world" Unable to find image 'ubuntu:14.04' locally #这里在本地没有找到,随后去从远端拉取 14.04: Pulling from library/ubuntu 99ad4e3ced4d: Pull complete ec5a723f4e2a: Pull complete 2a175e11567c: Pull complete 8d26426e95e0: Pull complete 46e451596b7c: Pull complete Digest: sha256:ed49036f63459d6e5ed6c0f238f5e94c3a0c70d24727c793c48fded60f70aa96 Status: Downloaded newer image for ubuntu:14.04 hello world #拉取成功后便会继续执行此命令 [root@xs_test01 docker]# docker run ubuntu:14.04 /bin/echo "hello world" hello world

启动一个bash终端,允许用户进行交互

1 2 3 4 5

[root@xs_test01 docker]# docker run -it ubuntu:14.04 /bin/bash root@6e995cc631e8:/# ls bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var -t 分配一个伪终端,并绑定到容器的标准输入上 -i 让容器的标准输入保持打开

使用docker run创建容器时,docker在后台运行的标准操作包括:

  • 检查本地是否存在指定的镜像,不存在就从公有仓库下载
  • 利用镜像创建并启动一个容器
  • 分配一个文件系统,并在只读的镜像层外面挂载一层可读写层
  • 从宿主主机配置的网桥接口中桥接一个虚拟接口到容器中去
  • 从地址池配置一个 ip 地址给容器
  • 执行用户指定的应用程序
  • 执行完毕后容器被终止

后台运行

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

[root@xs_test01 docker]# docker run ubuntu:17.04 /bin/sh -c "while true;do echo hello world;sleep 1;done" hello world hello world hello world hello world 放在后台: [root@xs_test01 docker]# docker run -d ubuntu:17.04 /bin/sh -c "while true;do echo hello world;sleep 1;done" fb7f8612f469ddbc8ae11495f130e4c3058e31aa36fcc5480eddc0ad4bb89d9f 输出结果并不会打印到宿主机上面,输出结果可以用docker logs查看 [root@xs_test01 docker]# docker logs fb7f8612f469 hello world hello world hello world hello world fb7f8612f469 这个是CONTAINER ID

关闭容器

以上面为例:

1 2

[root@xs_test01 docker]# docker stop 9e3a4d40497c 9e3a4d40497c

进入容器

当我们使用-d参数时,容器启动后会进入后台,这个时候当我们需要进入容器里进行操作时,可以通过使用attachexec命令

1 2 3 4 5 6 7 8

[root@xs_test01 docker]# docker attach d7c0bf8ea467 #使用attach的时候,如果使用exit或者ctrl + c 时,容器就会停止运行 hello world hello world hello world 或者 [root@xs_test01 docker]# docker exec -it 1cd1fadb2 /bin/bash #使用exec,容器不会停止运行 root@1cd1fadb254b:/# root@1cd1fadb254b:/#

导入导入容器

使用exportimport命令

1 2 3 4 5 6 7 8 9 10

获取容器ID [root@xs_test01 docker]# docker container ls -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1cd1fadb254b ubuntu:17.04 "/bin/sh -c 'while t…" 14 minutes ago Up 14 minutes kind_wilson d7c0bf8ea467 ubuntu:17.04 "/bin/sh -c 'while t…" 16 minutes ago Exited (0) 16 minutes ago wonderful_benz 9e3a4d40497c ubuntu:17.04 "/bin/sh -c 'while t…" 21 minutes ago Exited (137) 21 minutes ago gracious_tesla 导出 docker export 1cd1fadb254b > ubuntu.tar 导入 cat ubuntu.tar | docker import - ubuntu.tar

删除容器

1 2

[root@xs_test01 docker]# docker container rm d7c0bf8ea467 #必须删除是处于终止状态的容器 d7c0bf8ea467

清理所有处于终止状态的容器

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25

[root@xs_test01 docker]# docker container prune #可以清理所有处于终止状态的容器 WARNING! This will remove all stopped containers. Are you sure you want to continue? [y/N] y Deleted Containers: 9e3a4d40497c6e13b004a66cfe7ab1c2285520df1095103a46ed4da85ed8fc66 fb7f8612f469ddbc8ae11495f130e4c3058e31aa36fcc5480eddc0ad4bb89d9f 5a1b005369a4913979ab2ff9f359b7216127c34efaaca45413970bbe8f9fe1d1 f808a2363282ba9ce6e9fb7fe640bf06115ae3a76e4bb85acdff358003004cd8 6e995cc631e89fa3947389c1257285af6b8142477f7bd60c06742eb5e8bcad81 0963f5b8f2e00ad8b277a626b73f8e6c81ebcde957480ca6e60ab91426ce2f2a ee85a15563f55ff35eecbb287308e17803f3db4efd2f0f9d0fc5a7766cb88a27 408befbf6498ed96331e2143ffbcc48579a807ae0a9d1b7f79ccd489c1349783 a228457cfdc5d132b3e7de0277c1dd486109df4d5ce63ef0262129f605ec2a03 4a0d536145278399b7fe0a77d29f99df253ee26b0feaa11780d2da46efa3cd2a 4f299d01a7fcf57e73172ac271feed1f794fb867966e991abedcd1f2f4450532 31df5dd13a7057e13821df72a58917f753b5d79e069d8861de8e0f3387980e64 6a64bbaeb601c2bc4decbed5cc9cc2983c21f1652cd992c70acbf283a04b02d6 48a9aefd5c864658dc2f9af86090a8b5053b2125d854f5277ee431057de48b09 b63d1421a9dbfcd807a7f6464ade7b597ab405739f47de465a4b8e8bcda4fb6f b045067ba7b23f0c2a6d6ae104e85a9398cabe4f16034d943589704836dca11b c4976d5f8b30b8310f01d82711a514e81f471186ac04816a4db20b20d0b77750 9728d46167bdced600001801ed92e97e8460366b66dc9ae89e8978db7e51ac9a ba88eb098c8da7ab089b23991c4da5cfa9705aa36dd3dc989904a75be2a10d70 Total reclaimed space: 3B