功能
归档
304
2017 年 04 月 07 日
Docker镜像(二)

实验平台:CentOS 7.4
kernel版本:3.10.0-693.11.1.el7.x86_64(3.8+)
Docker版本:17.05.0-ce
至少使用CentOS 64位7.1及更高版本x86_64


操作步骤:


一、搜索镜像


1.查询centosl镜像

$ docker search centos

NAME                               DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
centos                             The official build of CentOS.                   4214      [OK]
ansible/centos7-ansible            Ansible on Centos7                              109                  [OK]
jdeathe/centos-ssh                 CentOS-6 6.9 x86_64 / CentOS-7 7.4.1708 x8...   94                   [OK]

2.Docker官方存储镜像

https://hub.docker.com/
#这是Docker官方网站专用来存储所有可用的镜像

二、下载容器镜像


1.通过docker pull命令来下载镜像

$ docker pull jdeathe/centos-ssh

Using default tag: latest
latest: Pulling from jdeathe/centos-ssh
18b8eb7e7f01: Pull complete
860745e600d8: Pull complete
0b33913cd50e: Pull complete
ea532aedd5cb: Pull complete
569867c554aa: Pull complete
c30eb30fb482: Pull complete
ea52a698d5c5: Pull complete
b05c4caee4c7: Pull complete
bd7d9c31e53d: Pull complete
28838bbbea9d: Pull complete
b136be7af5a1: Pull complete
421c16d7dbd7: Pull complete
6a6b24ac27be: Pull complete
b9d9e19c962a: Pull complete
Digest: sha256:da6069a83e1ab84b7e471827f416fe4053337417dd03e7cdf35c4ca8fca807ef
Status: Downloaded newer image for jdeathe/centos-ssh:latest

#执行pull命令的时候要写完整的名字,比如"jdeathe/centos-ssh"。
#在docker的镜像索引网站上面,镜像都是按照用户名/镜像名的方式来存储的。

2.在容器中执行命令

$ docker run jdeathe/centos-ssh echo "hello word"
hello word

3.查看容器

$ docker ps -a

CONTAINER ID        IMAGE                                            COMMAND                  CREATED             STATUS                     PORTS                          NAMES
7d87620d9d9e        jdeathe/centos-ssh                               "echo 'hello world'"     5 seconds ago       Exited (0) 4 seconds ago                                  brave_ardinghelli
#docker run命令有两个参数,一个是镜像名,一个是要在镜像中运行的命令。

三、在容器中安装程序


1.安装程序

$ docker run jdeathe/centos-ssh yum -y install vim

Loaded plugins: fastestmirror, ovl, versionlock
Determining fastest mirrors
 * base: mirrors.163.com
 * epel: mirrors.ustc.edu.cn
 * extras: mirrors.163.com
 * ius: mirrors.tongji.edu.cn
 * updates: mirrors.163.com
Resolving Dependencies
...省略yum自动安装过程
Complete!

#注意在执行yum安装时要带上-y参数,yum命令会进入交互模式,需要用户输入命令来确认回应,在docker环境中是无法响应这种交互的。

2.获取容器ID

$ docker ps -l

CONTAINER ID        IMAGE                COMMAND                CREATED             STATUS                     PORTS               NAMES
27be43e4c674        jdeathe/centos-ssh   "yum -y install vim"   4 minutes ago       Exited (0) 2 minutes ago                       amazing_noyce
#获取容器ID(CONTAINER ID)

3.保存容器

$ docker commit 27be jdeathe/vim

sha256:4f8fb6bd87ca024e7672a0a03439dce05001d2af0db8fc39e94c0c553a6a9b61
#无需拷贝完整的id,通常来讲最开始的三至四个字母即可区分
#执行完docker commit命令之后,会返回新版本镜像的id号

4.查看是否保存成功

$ docker images

REPOSITORY                                TAG                 IMAGE ID            CREATED              SIZE
jdeathe/vim                               latest              2c9dcfab993d        About a minute ago   431MB
#列出所有安装过的镜像

四、运行新镜像


1.在刚保存的镜像(jdeathe/vim)中运行vim/ping命令

$ docker run jdeathe/vim ping www.baidu.com

PING www.a.shifen.com (220.181.111.188) 56(84) bytes of data.
64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=1 ttl=50 time=36.8 ms
64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=2 ttl=50 time=36.9 ms
64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=3 ttl=50 time=36.9 ms
64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=4 ttl=50 time=36.8 ms
$ docker run jdeathe/vim vim /etc/resolv.conf

Vim: Warning: Output is not to a terminal
Vim: Warning: Input is not from a terminal
#有个bug进去了退不了~

2.查询正在运行的镜像

$ docker ps

CONTAINER ID        IMAGE               COMMAND                CREATED             STATUS                      PORTS               NAMES
71d2c786c4d1        jdeathe/vim         "ping www.baidu.com"   13 seconds ago      Up 12 seconds (unhealthy)   22/tcp              pensive_knuth
#只能查到正在运行的镜像
$ docker inspect 71d
[
    {
        "Id": "71d2c786c4d1e72f2825069bd3b2996a12958d4a1d2f18167485c781ed3e8a51",
        "Created": "2018-05-07T01:25:06.620865272Z",
        "Path": "ping",
        "Args": [
            "www.baidu.com"
        ],

Over ~

留言
Loading...

发表评论

电子邮件地址不会被公开。 必填项已用*标注