>

营造镜像,使用Docker中的容器

- 编辑:www.bifa688.com -

营造镜像,使用Docker中的容器

1.启动一个Docker容器

对于docker用户来说,自己创建镜像较复杂,因为几乎所有的数据库,中间件,软件都有现成的。我们只需要下载并做些配置就可以了。
但是如果我们找不到现成的镜像或者在镜像中加入特定的功能的话就需要我们自己手动构造镜像了

[root@localhost ~]# docker run -i -t Ubuntu /bin/bash
Unable to find image 'ubuntu' locally
Pulling repository ubuntu
04c5d3b7b065: Download complete
511136ea3c5a: Download complete
c7b7c6419568: Download complete
70c8faa62a44: Download complete
d735006ad9c1: Download complete
Status: Downloaded newer image for ubuntu:latest

Docker提供了两种镜像的方法:

注:

1 docker commit命令

-i:打开容器中的STDIN

2 Dockerfile命令

-t:为容器分配一个伪tty终端

首先来看下docker commit命令的使用方法:

从上面我们可以看出,首先Docker会检查本地是否存在ubuntu镜像,如果在本地没有找到该镜像的话,那么Docker就会去官方的Docker Hub Registry查看Docker Hub中是否有该镜像。Docker一旦找到该镜像,就会下载该镜像并将其保存到本地的宿主机中。

第一步:安装一个ubuntu的容器。使用run命令的时候如果找不到这个容器会自动下载

然后,Docker在文件系统内部用这个镜像创建了一个新的容器。该容器拥有自己的网络、IP地址,以及一个用来可以和宿主机进行通信的桥接网络接口。最后,我们告诉Docker在新容器中要运行什么命令。

root@zhf-linux:/var/lib/docker# docker run -it ubuntu

当容器创建完毕之后,Docker就会执行容器中的/bin/bash命令。这时间我们就可以看到容器内的shell

Unable to find image 'ubuntu:latest' locally

root@8c342c0c275c:/#

latest: Pulling from library/ubuntu

注:8c342c0c275c代表容器的ID

aef6d0613937: Pull complete

2.使用容器

83de9eba49f1: Pull complete

查看该容器的主机名

9087439057e1: Pull complete

root@8c342c0c275c:/# hostname
8c342c0c275c

45d36c0ac9f9: Pull complete

可以看到,容器的主机名就是该容器的ID

e991b5792ff6: Pull complete

查看该主机的hosts文件
root@8c342c0c275c:/# cat /etc/hosts
172.17.0.2 8c342c0c275c
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet

Digest: sha256:ec0e4e8bf2c1178e025099eed57c566959bb408c6b478c284c1683bc4298b683

可以看到Docker为该容器的IP地址添加了一条主机配置项。

Status: Downloaded newer image for ubuntu:latest

查看该容器的IP地址

第二步:进入容器。 ecb05acfdb66是容器ID

root@8c342c0c275c:/# ip a     
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
      valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host
      valid_lft forever preferred_lft forever
4: eth0: <BROADCAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
      valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link
      valid_lft forever preferred_lft forever

root@zhf-linux:/home/zhf# docker run -it ubuntu

 

root@ecb05acfdb66:/#

查看容器中运行的进程

第三步:安装vim. 可以看到VIM并没有安装

root@8c342c0c275c:/# ps -aux
USER      PID %CPU %MEM    VSZ  RSS TTY      STAT START  TIME COMMAND
root        1  0.0  0.1  18168  1976 ?        Ss  09:38  0:00 /bin/bash
root        19  0.0  0.0  15568  1144 ?        R   09:55  0:00 ps -aux

root@ecb05acfdb66:/# vim

 

bash: vim: command not found

在容器中安装一个软件包

安装vim的时候提示找不到vim

root@8c342c0c275c:/# apt-get update && apt-get install vim
Ign trusty InRelease     
Ign trusty-updates InRelease
Ign trusty-security InRelease
Hit trusty Release.gpg
Get:1 trusty-updates Release.gpg [933 B]
Get:2 trusty-security Release.gpg [933 B]
Hit trusty Release
Get:3 trusty-updates Release [62.0 kB]
Get:4 trusty-security Release [62.0 kB]
Get:5 trusty/main Sources [1335 kB]
Get:6 trusty/restricted Sources [5335 B]           
Get:7 trusty/universe Sources [7926 kB]             
Get:8 trusty/main amd64 Packages [1743 kB]         
Get:9 trusty/restricted amd64 Packages [16.0 kB]   
Get:10 trusty/universe amd64 Packages [7589 kB]     
Get:11 trusty-updates/main Sources [193 kB]         
Get:12 trusty-updates/restricted Sources [1874 B]   
Get:13 trusty-updates/universe Sources [119 kB]     
Get:14 trusty-updates/main amd64 Packages [493 kB] 
Get:15 trusty-updates/restricted amd64 Packages [14.8 kB]
Get:16 trusty-updates/universe amd64 Packages [298 kB]
Get:17 trusty-security/main Sources [70.1 kB]       
Get:18 trusty-security/restricted Sources [1874 B] 
Get:19 trusty-security/universe Sources [19.1 kB]   
Get:20 trusty-security/main amd64 Packages [229 kB] 
Get:21 trusty-security/restricted amd64 Packages [14.8 kB]
Get:22 trusty-security/universe amd64 Packages [98.1 kB]
Fetched 20.3 MB in 41s (490 kB/s)                                             
Reading package lists... Done
Reading package lists... Done
Building dependency tree     
Reading state information... Done
The following extra packages will be installed:
  libgpm2 libpython2.7 libpython2.7-minimal libpython2.7-stdlib vim-runtime
Suggested packages:
  gpm ctags vim-doc vim-scripts
The following NEW packages will be installed:
  libgpm2 libpython2.7 libpython2.7-minimal libpython2.7-stdlib vim
  vim-runtime
0 upgraded, 6 newly installed, 0 to remove and 2 not upgraded.
Need to get 9083 kB of archives.
After this operation, 42.9 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
Get:1 trusty/main libgpm2 amd64 1.20.4-6.1 [16.5 kB]
Get:2 trusty/main libpython2.7-minimal amd64 2.7.6-8 [307 kB]
Get:3 trusty/main libpython2.7-stdlib amd64 2.7.6-8 [1872 kB]
Get:4 trusty/main libpython2.7 amd64 2.7.6-8 [1044 kB]
Get:5 trusty/main vim-runtime all 2:7.4.052-1ubuntu3 [4888 kB]
Get:6 trusty/main vim amd64 2:7.4.052-1ubuntu3 [956 kB]
Fetched 9083 kB in 51s (175 kB/s)                                             
Selecting previously unselected package libgpm2:amd64.
(Reading database ... 11527 files and directories currently installed.)
Preparing to unpack .../libgpm2_1.20.4-6.1_amd64.deb ...
Unpacking libgpm2:amd64 (1.20.4-6.1) ...
Selecting previously unselected package libpython2.7-minimal:amd64.
Preparing to unpack .../libpython2.7-minimal_2.7.6-8_amd64.deb ...
Unpacking libpython2.7-minimal:amd64 (2.7.6-8) ...
Selecting previously unselected package libpython2.7-stdlib:amd64.
Preparing to unpack .../libpython2.7-stdlib_2.7.6-8_amd64.deb ...
Unpacking libpython2.7-stdlib:amd64 (2.7.6-8) ...
Selecting previously unselected package libpython2.7:amd64.
Preparing to unpack .../libpython2.7_2.7.6-8_amd64.deb ...
Unpacking libpython2.7:amd64 (2.7.6-8) ...
Selecting previously unselected package vim-runtime.
Preparing to unpack .../vim-runtime_2:7.4.052-1ubuntu3_all.deb ...
Adding 'diversion of /usr/share/vim/vim74/doc/help.txt to /usr/share/vim/vim74/doc/help.txt.vim-tiny by vim-runtime'
Adding 'diversion of /usr/share/vim/vim74/doc/tags to /usr/share/vim/vim74/doc/tags.vim-tiny by vim-runtime'
Unpacking vim-runtime (2:7.4.052-1ubuntu3) ...
Selecting previously unselected package vim.
Preparing to unpack .../vim_2:7.4.052-1ubuntu3_amd64.deb ...
Unpacking vim (2:7.4.052-1ubuntu3) ...
Setting up libgpm2:amd64 (1.20.4-6.1) ...
Setting up libpython2.7-minimal:amd64 (2.7.6-8) ...
Setting up libpython2.7-stdlib:amd64 (2.7.6-8) ...
Setting up libpython2.7:amd64 (2.7.6-8) ...
Setting up vim-runtime (2:7.4.052-1ubuntu3) ...
Processing /usr/share/vim/addons/doc
Setting up vim (2:7.4.052-1ubuntu3) ...
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vim (vim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vimdiff (vimdiff) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rvim (rvim) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/rview (rview) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.basic to provide /usr/bin/editor (editor) in auto mode
Processing triggers for libc-bin (2.19-0ubuntu6.4) ...

root@ecb05acfdb66:/# apt-get install -y vim

 

Reading package lists... Done

退出容器

Building dependency tree

root@8c342c0c275c:/# exit
exit

Reading state information... Done

当我们输入exit后,容器就停止工作了。只有在指定的/bin/bash命令处于运行状态的时间,容器才会相应地处于运行状态。一旦退出容器,/bin/bash命令也就结束了,这时容器也就停止了。

E: Unable to locate package vim

 

这是由于软件源没有更新的原因,执行下apt-get update 得到最新的软件包 再次执行apt-get install vim就可以成功的安装了

查看系统中容器的列表

安装完成后docker ps可以看到当前正在运行的容器

[root@localhost ~]# docker ps -a
CONTAINER ID        IMAGE              COMMAND            CREATED            STATUS                    PORTS              NAMES
8c342c0c275c        ubuntu:latest      "/bin/bash"        26 minutes ago      Exited (0) 5 minutes ago                      sharp_bohr 

root@zhf-linux:/home/zhf# docker ps

 

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

8c342c0c275c:代表容器的ID,

ecb05acfdb66 ubuntu "/bin/bash" 34 minutes ago Up 34 minutes sleepy_brahmagupta

ubuntu:latest  :创建容器的镜像

sleepy_brahmagupta是Docker为容器随即分配的名字

"/bin/bash"  :容器最后执行的命令

第四步:重新开启一个终端保存新镜像:新镜像名为ubuntu-with-vi

26 minutes ago  :创建时间

root@zhf-linux:/home/zhf# docker commit sleepy_brahmagupta ubuntu-with-vi

Exited (0) 5 minutes :容器退出的状态

sha256:a8ad90906fcc274df1b9072c690e1c6b8561aeb8d986e0f9caa649ca954ffcd8

 sharp_bohr      :容器的名称

从size来看镜像因为安装了软件而变大了。

Docker中容器的命名

root@zhf-linux:/home/zhf# docker images

Docker在创建容器时会自动为容器生成一个随机的名称。那么如果我们想在创建一个容器时指定该容器的名称可以使���如下命令:

REPOSITORY TAG IMAGE ID CREATED SIZE

[root@localhost ~]# docker run --name ovcer_the_container -i -t ubuntu /bin/bash
root@1ce9f640478d:/#

ubuntu-with-vi latest a8ad90906fcc 54 seconds ago 205 MB

上面的命令将会创建一个名为ovcer_the_container的容器。对于一个合法的容器的名称来说只可以包括以下字符:小写字母a~z、大写字母A-Z、数字0~9、下划线、圆点、横线。

ubuntu latest fb14c85f88d3 5 days ago 109 MB

 

httpd latest 7659d5a9a057 8 weeks ago 189 MB

CentOS 6/7系列安装Docker http://www.linuxidc.com/Linux/2014-07/104768.htm

hello-world latest abd130ec0722 4 months ago 665 kB

Docker的搭建Gitlab CI 全过程详解 http://www.linuxidc.com/Linux/2013-12/93537.htm

第五步:进入新镜像执行vim命令:

Docker安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm

root@zhf-linux:/home/zhf# docker run -it ubuntu-with-vi

在 Docker 中使用 MySQL http://www.linuxidc.com/Linux/2014-01/95354.htm

root@189eac7e1d60:/# which vim

在Ubuntu Trusty 14.04 (LTS) (64-bit)安装Docker http://www.linuxidc.com/Linux/2014-10/108184.htm

/usr/bin/vim

Docker安装应用(CentOS 6.5_x64) http://www.linuxidc.com/Linux/2014-07/104595.htm

这就是docker commit创建新镜像的方法。这种方法效率低而且可重复性很弱。比如要在其他操作系统镜像中加入vi还要重复前面所有的步骤。下面就来看下采用dockerfile的方法

Ubuntu 14.04安装Docker  http://www.linuxidc.com/linux/2014-08/105656.htm

第一步:首先创建Dockerfile,内容如下。 命令必须大写

阿里云CentOS 6.5 模板上安装 Docker http://www.linuxidc.com/Linux/2014-11/109107.htm

FROM ubuntu

Docker 的详细介绍:请点这里
Docker 的下载地址:请点这里

RUN apt-get update && apt-get install -y vim

本文永久更新链接地址:http://www.linuxidc.com/Linux/2014-12/110948.htm

第二步:执行命令root@zhf-linux:/home/zhf/docker# docker build -t ubuntu-with-vi-dockerfile .

图片 1

Dockerfile 中的 ADD、COPY 等命令可以将 build context 中的文件添加到镜像。此例中,build context 为当前目录/home/zhf/docker,该目录下的所有文件和子目录都会被发送给 Docker daemon。

第三步:完成创建

root@zhf-linux:/home/zhf/docker# docker build -t ubuntu-with-vi-dockerfile .

Sending build context to Docker daemon 2.048 kB

Step 1/2 : FROM ubuntu

---> fb14c85f88d3

Step 2/2 : RUN apt-get update && apt-get install -y vim

---> Running in d46f693665cd

Get:1 xenial-security InRelease [102 kB]

Get:2 xenial InRelease [247 kB]

Get:3 xenial-security/universe Sources [56.7 kB]

Get:4 xenial-security/main i386 Packages [473 kB]

Get:5 xenial-updates InRelease [102 kB]

Get:6 xenial-backports InRelease [102 kB]

Get:7 xenial/universe Sources [9802 kB]

Get:8 xenial-security/restricted i386 Packages [13.0 kB]

Get:9 xenial-security/universe i386 Packages [198 kB]

Get:10 xenial-security/multiverse i386 Packages [3674 B]

Get:11 xenial/main i386 Packages [1552 kB]

Get:12 xenial/restricted i386 Packages [14.5 kB]

Get:13 xenial/universe i386 Packages [9804 kB]

Get:14 xenial/multiverse i386 Packages [172 kB]

Get:15 xenial-updates/universe Sources [233 kB]

Get:16 xenial-updates/main i386 Packages [827 kB]

Get:17 xenial-updates/restricted i386 Packages [13.7 kB]

Get:18 xenial-updates/universe i386 Packages [680 kB]

Get:19 xenial-updates/multiverse i386 Packages [17.3 kB]

Get:20 xenial-backports/main i386 Packages [5166 B]

Get:21 xenial-backports/universe i386 Packages [7149 B]

Fetched 24.4 MB in 4min 36s (88.4 kB/s)

Reading package lists… (中间过程省略)

.

.

.

.

.

Processing triggers for libc-bin (2.23-0ubuntu9) ...

---> 4ab7bb9b7cb4

Removing intermediate container d46f693665cd

Successfully built 4ab7bb9b7cb4

从上面可以看到步骤大致为几步:

1 执行FROM,将ubuntu作为镜像。ID=fb14c85f88d3

2 执行RUN安装VIM

3 启动 ID=d46f693665cd的临时容器,在容器中通过apt-get安装vim

4 安装成功后,将容器保存为镜像。ID=4ab7bb9b7cb4并且删除了ID=d46f693665cd的临时容器

通过docker images查看

root@zhf-linux:/home/zhf/docker# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

ubuntu-with-vi-dockerfile latest 4ab7bb9b7cb4 6 minutes ago 205 MB

ubuntu-with-vi latest a8ad90906fcc 23 minutes ago 205 MB

ubuntu latest fb14c85f88d3 5 days ago 109 MB

httpd latest 7659d5a9a057 8 weeks ago 189 MB

hello-world latest abd130ec0722 4 months ago 665 kB

root@3514d5772446:/# which vim

/usr/bin/vim

也可以通过docker history来查看Dockerfile的执行过程

root@zhf-linux:/home/zhf# docker history ubuntu-with-vi-dockerfile

IMAGE CREATED CREATED BY SIZE COMMENT

4ab7bb9b7cb4 2 days ago /bin/sh -c apt-get update && apt-get insta... 96 MB

fb14c85f88d3 8 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B

<missing> 8 days ago /bin/sh -c mkdir -p /run/systemd && echo '... 7 B

<missing> 8 days ago /bin/sh -c sed -i 's/^#s*(deb.*universe... 2.76 kB

<missing> 8 days ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0 B

<missing> 8 days ago /bin/sh -c set -xe && echo '#!/bin/sh' >... 745 B

<missing> 8 days ago /bin/sh -c #(nop) ADD file:db6281c0035641a... 109 MB

和ubuntu的来比较一下,ubuntu-with-vi-dockerfile 与 ubuntu 镜像相比,确实只是多了顶部的一层 35ca89798937,由 apt-get 命令创建,大小为 97.07MB。docker history 也向我们展示了镜像的分层结构,每一层由上至下排列。

root@zhf-linux:/home/zhf# docker history ubuntu

IMAGE CREATED CREATED BY SIZE COMMENT

fb14c85f88d3 8 days ago /bin/sh -c #(nop) CMD ["/bin/bash"] 0 B

<missing> 8 days ago /bin/sh -c mkdir -p /run/systemd && echo '... 7 B

<missing> 8 days ago /bin/sh -c sed -i 's/^#s*(deb.*universe... 2.76 kB

<missing> 8 days ago /bin/sh -c rm -rf /var/lib/apt/lists/* 0 B

<missing> 8 days ago /bin/sh -c set -xe && echo '#!/bin/sh' >... 745 B

<missing> 8 days ago /bin/sh -c #(nop) ADD file:db6281c0035641a... 109 MB

下面继续看下docker的缓存,Dockerfile的内容如下:

FROM ubuntu

RUN apt-get update && apt-get install -y vim

COPY Dockerfile /

执行结果:

root@zhf-linux:/home/zhf/docker# docker build -t ubuntu-with-v-dockerfile_2 .

Sending build context to Docker daemon 2.048 kB

Step 1/3 : FROM ubuntu

---> fb14c85f88d3

Step 2/3 : RUN apt-get update && apt-get install -y vim

---> Using cache

---> 4ab7bb9b7cb4

Step 3/3 : COPY Dockerfile /

---> 03943952ca8b

Removing intermediate container a9f1c9e81ea6

Successfully built 03943952ca8b

1 首先确保执行docker build命令的路径下的Dockerfile存在,也就是这个例子中的/home/zhf/docker存在Dockerfile

2 在第二步的RUN命令中镜像ID=4ab7bb9b7cb4和之前ubuntu-with-vi-dockerfile中的是一样的

图片 2

 

由于之前使用过相同的RUN指令,这次直接使用缓存中的镜像4ab7bb9b7cb4

3 启动临时容器,复制testfile,提交新的镜像层03943952ca8b并且删除临时容器

我们再做一个实验,如果将Dockerfile中的执行顺序改变一下看下运行结果是什么

我们将COPY命令和RUN命令调换一下顺序

FROM ubuntu

COPY Dockerfile /

RUN apt-get update && apt-get install -y vim

执行结果:可以看到在复制文件的时候运行的是一个新的镜像层。

Sending build context to Docker daemon 2.048 kB

Step 1/3 : FROM ubuntu

---> fb14c85f88d3

Step 2/3 : COPY Dockerfile /

---> 30ee5666cc73

Removing intermediate container fcc01972c127

Step 3/3 : RUN apt-get update && apt-get install -y vim

---> Running in 1be3495fdfb1

Get:1 xenial-security InRelease [102 kB]

Get:2 xenial InRelease [247 kB]

Get:3 xenial-security/universe Sources [56.7 kB]

Get:4 xenial-updates InRelease [102 kB]

Get:5 xenial-backports InRelease [102 kB]

Get:6 xenial-security/main i386 Packages [474 kB]

Get:7 xenial/universe Sources [9802 kB]

Get:8 xenial-security/restricted i386 Packages [13.0 kB]

Get:9 xenial-security/universe i386 Packages [198 kB]

Get:10 xenial-security/multiverse i386 Packages [3674 B]

Fetched 24.4 MB in 5min 55s (68.8 kB/s)

Reading package lists...

Reading package lists...

Building dependency tree...

Reading state information...

The following additional packages will be installed:

file libexpat1 libgpm2 libmagic1 libmpdec2 libpython3.5 libpython3.5-minimal

libpython3.5-stdlib libsqlite3-0 libssl1.0.0 mime-support vim-common

vim-runtime

Suggested packages:

gpm ctags vim-doc vim-scripts vim-gnome-py2 | vim-gtk-py2 | vim-gtk3-py2

| vim-athena-py2 | vim-nox-py2

The following NEW packages will be installed:

file libexpat1 libgpm2 libmagic1 libmpdec2 libpython3.5 libpython3.5-minimal

libpython3.5-stdlib libsqlite3-0 libssl1.0.0 mime-support vim vim-common

vim-runtime

0 upgraded, 14 newly installed, 0 to remove and 0 not upgraded.

Need to get 12.2 MB of archives.

After this operation, 58.2 MB of additional disk space will be used.

Get:1 xenial/main i386 libgpm2 i386 1.20.4-6.1 [16.0 kB]

Get:2 xenial/main i386 libmagic1 i386 1:5.25-2ubuntu1 [222 kB]

Get:3 xenial/main i386 file i386 1:5.25-2ubuntu1 [21.1 kB]

Get:4 xenial-updates/main i386 libexpat1 i386 2.1.0-7ubuntu0.16.04.3 [74.1 kB]

.

.

.

.

Processing triggers for libc-bin (2.23-0ubuntu9) ...

---> 3950939de1c3

Removing intermediate container 1be3495fdfb1

Successfully built 3950939de1c3

Dockerfile 中每一个指令都会创建一个镜像层,上层是依赖于下层的。无论什么时候,只要某一层发生变化,其上面所有层的缓存都会失效。 也就是说,如果我们改变 Dockerfile 指令的执行顺序,或者修改或添加指令,都会使缓存失效。

本文由bifa688.com发布,转载请注明来源:营造镜像,使用Docker中的容器