用 docker pull 拉取镜像
root@lishichao-virtual-machine:~# docker pull hello-worldUsing default tag: latestlatest: Pulling from library/hello-worldd1725b59e92d: Pull complete Digest: sha256:1a6fd470b9ce10849be79e99529a88371dff60c60aab424c077007f6979b4812Status: Downloaded newer image for hello-world:latest
用 docker images
命令查看镜像的信息。
root@lishichao-virtual-machine:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEhttpd latest c5a621af54e4 6 hours ago 178MBhello-world latest 4ab4c602aa5e 5 weeks ago 1.84kB
通过 docker run
运行。 docker run --help 查看帮助
root@lishichao-virtual-machine:~# docker run hello-worldHello from Docker!This message shows that your installation appears to be working correctly.To generate this message, Docker took the following steps: 1. The Docker client contacted the Docker daemon. 2. The Docker daemon pulled the "hello-world" image from the Docker Hub. (amd64) 3. The Docker daemon created a new container from that image which runs the executable that produces the output you are currently reading. 4. The Docker daemon streamed that output to the Docker client, which sent it to your terminal.To try something more ambitious, you can run an Ubuntu container with: $ docker run -it ubuntu bashShare images, automate workflows, and more with a free Docker ID: https://hub.docker.com/For more examples and ideas, visit: https://docs.docker.com/get-started/root@lishichao-virtual-machine:~#
-it
参数的作用是以交互模式进入容器,并打开终端。5d1eb9e31f58 是容器的内部 ID。
root@lishichao-virtual-machine:~# docker run -it centos[root@5d1eb9e31f58 /]#
docker ps 查看运行中的容器
root@lishichao-virtual-machine:~# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4d85f20b58c6 centos "/bin/bash" 50 seconds ago Up 49 seconds brave_jennings
镜像命令操作
docker search 到镜像仓库搜索镜像
docker search centos
docker images 查看本地已有的镜像
[root@dns-server ~]# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos-vim-dockerfile latest f39351a5d35c 30 hours ago 355MBmysql 5.7 1b30b36ae96a 3 days ago 372MBmysql latest ee1e8adfcefb 3 days ago 484MBnginx latest dbfc48660aeb 3 days ago 109MB
docker save 导出镜像
[root@dns-server ~]# docker save -o nginx.tar nginx[root@dns-server ~]# lsanaconda-ks.cfg docker_login.sh nginx.tar[root@dns-server ~]#
docker load --input 导入镜像
[root@dns-server ~]# docker load --input nginx.tar Loaded image: nginx:latest
docker rmi 删除镜像
[root@dns-server ~]# docker rmi f39351a5d35cUntagged: centos-vim-dockerfile:latestDeleted: sha256:f39351a5d35ca9e00dc101c43464c3f55e9e08a240daaafc4eded43692f745fbDeleted: sha256:f1b1df6c5b8314f3a0b3a1d5a6a96d7f20501f770b042ac1ad18b6f84164cabc
docker commit
docker commit 命令是创建新镜像最直观的方法,其过程包含三个步骤:
- 运行容器
- 修改容器
- 将容器保存为新的镜像
举个例子:在 Centos base 镜像中安装 vim 并保存为新镜像。
第一步, 运行容器
root@lishichao-virtual-machine:~# docker run -it centos[root@4d85f20b58c6 /]#
-it 参数的作用是以交互模式进入容器,并打开终端。4d85f20b58c6 是容器的内部 ID。
第二步,安装 vim
确认vim没有安装 [root@4d85f20b58c6 /]# vimbash: vim: command not found 安装vim[root@4d85f20b58c6 /]# yum install vim
第三步,保存为新镜像
a.在新窗口中查看当前运行的容器。
root@lishichao-virtual-machine:~# docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES4d85f20b58c6 centos "/bin/bash" 50 seconds ago Up 49 seconds brave_jennings
brave_jennings 是 Docker 为我们的容器随机分配的名字。
b. 执行 docker commit 命令将容器保存为镜像。
root@lishichao-virtual-machine:~# docker commit brave_jennings centos-with-vimsha256:a55c595ffacac70fdd2995d898bab31dd932f6ddeeed59fdfcd52a0f695a0c82
新镜像命名为 ubuntu-with-vi。
c. 查看新镜像的属性。
root@lishichao-virtual-machine:~# docker imagesREPOSITORY TAG IMAGE ID CREATED SIZEcentos-with-vim latest a55c595ffaca 4 seconds ago 355MBhttpd latest c5a621af54e4 6 hours ago 178MBcentos latest 75835a67d134 6 days ago 200MBhello-world latest 4ab4c602aa5e 5 weeks ago 1.84kBubuntu latest cd6d8154f1e1 5 weeks ago 84.1MB
从 size 上看到镜像因为安装了软件而变大了。
d. 从新镜像启动容器,验证 vi 已经可以使用。
root@lishichao-virtual-machine:~# docker -it centos-with-vim[root@c2b086c59d36 /]# which vim/usr/bin/vim
以上演示了如何用 docker commit 创建新镜像。然而,Docker 并不建议用户通过这种方式构建镜像。原因如下:
1、这是一种手工创建镜像的方式,容易出错,效率低且可重复性弱。比如要在 debian base 镜像中也加入 vi,还得重复前面的所有步骤。
2、更重要的:使用者并不知道镜像是如何创建出来的,里面是否有恶意程序。也就是说无法对镜像进行审计,存在安全隐患。
既然 docker commit 不是推荐的方法,我们干嘛还要花时间学习呢?
原因是:即便是用 Dockerfile(推荐方法)构建镜像,底层也 docker commit 一层一层构建新镜像的。学习 docker commit 能够帮助我们更加深入地理解构建过程和镜像的分层结构。