Docker基础之七: 镜像操作

构建镜像

对Docker感兴趣的朋友可以加我的微信ghostcloud2016,然后我把你加到我们的一个Docker爱好者群组里面。

镜像(Image)是容器的基础。当你运行docker run的时候,你会指定一个Image来运行。比如,最基本的ubuntu镜像和training/webapp镜像。 在上一节中,我们是从Docker Hub下载的ubuntu镜像。在本节中,我们将做如下操作:

  • 管理和运行你的本地镜像
  • 创建基础镜像

1 查看本机的镜像

root@gctest:~/.ghostcloud/proxy/conf# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ghostcloud.cn:5005/gcali   1.0.7               553ff90cea5d        40 hours ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.6               0704f2860bd6        41 hours ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.3               746ac51185f2        42 hours ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.2               b06108e12275        43 hours ago        219.6 MB
ubuntu                     latest              e9ae3c220b23        3 weeks ago         187.9 MB

上面你可以看到你本机的docker镜像,其中ubuntu是一个基础镜像,来自于docker hub,而ghostcloud.cn:5005/gcali是来自于ghostcloud.cn的第三方镜像。我们可以看到有几个镜像都是ghostcloud.cn:5005/gcali,但他们拥有不同的TAG,类似于版本。接下来,我们可以通过docker run来运行镜像。

root@gctest:~/.ghostcloud/proxy/conf# docker run -i -t ubuntu:latest /bin/bash
root@46ffa55f5680:/#

2 获取新的image

我们如何来获取新镜像呢?其实之前我们的docker run包含了镜像的拉取,我们可以通过docker pull拉取镜像,并且不启动镜像。

$ docker pull centos
Pulling repository centos
b7de3133ff98: Pulling dependent layers
5cc9e91966f7: Pulling fs layer
511136ea3c5a: Download complete
ef52fb1fe610: Download complete
. . .

Status: Downloaded newer image for centos

之后,我们可以运行这个镜像:

$ docker run -t -i centos /bin/bash
bash-4.1#

3 查找Image

我们可以通过docker search命令来搜索镜像,假入我们要搜索mysql:

root@46ffa55f5680:/# exitroot@gctest:~/.ghostcloud/proxy/conf# docker search ubuntu
NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                           Ubuntu is a Debian-based Linux operating s...   2778      [OK]
ubuntu-upstart                   Upstart is an event-based replacement for ...   48        [OK]
dorowu/ubuntu-desktop-lxde-vnc   Ubuntu with openssh-server and NoVNC on po...   28                   [OK]
sequenceiq/hadoop-ubuntu         An easy way to try Hadoop on Ubuntu             25                   [OK]
torusware/speedus-ubuntu         Always updated official Ubuntu docker imag...   25                   [OK]
ubuntu-debootstrap               debootstrap --variant=minbase --components...   20        [OK]
tleyden5iwx/ubuntu-cuda          Ubuntu 14.04 with CUDA drivers pre-installed    18                   [OK]
rastasheep/ubuntu-sshd           Dockerized SSH service, built on top of of...   15                   [OK]
consol/ubuntu-xfce-vnc           Ubuntu container with "headless" VNC sessi...   7                    [OK]
n3ziniuka5/ubuntu-oracle-jdk     Ubuntu with Oracle JDK. Check tags for ver...   5                    [OK]
nuagebec/ubuntu                  Simple always updated Ubuntu docker images...   4                    [OK]
ioft/armhf-ubuntu                [ABR] Ubuntu Docker images for the ARMv7(a...   4                    [OK]
nimmis/ubuntu                    This is a docker images different LTS vers...   3                    [OK]
maxexcloo/ubuntu                 Docker base image built on Ubuntu with Sup...   2                    [OK]
densuke/ubuntu-jp-remix          Ubuntu Linuxの日本語remix風味です                       1                    [OK]
seetheprogress/ubuntu            Ubuntu image provided by seetheprogress us...   1                    [OK]
sylvainlasnier/ubuntu            Ubuntu 15.04 root docker images with commo...   1                    [OK]
tvaughan/ubuntu                  https://github.com/tvaughan/docker-ubuntu       0                    [OK]
rallias/ubuntu                   Ubuntu with the needful                         0                    [OK]
zoni/ubuntu                                                                      0                    [OK]
partlab/ubuntu                   Simple Ubuntu docker images.                    0                    [OK]
vicamo/ubuntu-phablet-jiexi      Dockerfile for developing Ubuntu JieXi PDK.     0                    [OK]
densuke/ubuntu-supervisor        densuke/ubuntu-jp-remix:trusty 上で supe...       0                    [OK]
teamrock/ubuntu                  TeamRock's Ubuntu image configured with AW...   0                    [OK]
esycat/ubuntu                    Ubuntu LTS                                      0                    [OK]

4 更新自己的Image

容器实际会在Image上增加一个读写文件层,我们可以将已经运行的容器通过docker comit生成新的镜像:

root@gctest:~/.ghostcloud/proxy/conf# docker ps
CONTAINER ID        IMAGE                            COMMAND                  CREATED             STATUS              PORTS               NAMES
2d961ac3b9fe        ghostcloud.cn:5005/gcali:1.0.7   "/usr/local/ghostclou"   36 hours ago        Up 36 hours                             gcsagent
root@gctest:~/.ghostcloud/proxy/conf# docker commit 2d96 ghostcloud:test
df9744f284bd8fd778ae7e782f2348ba24e4d9a51877c5e48b2f552fdadb526c
root@gctest:~/.ghostcloud/proxy/conf# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
ghostcloud                 test                df9744f284bd        4 seconds ago       219.6 MB
ghostcloud.cn:5005/gcali   1.0.7               553ff90cea5d        40 hours ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.6               0704f2860bd6        41 hours ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.3               746ac51185f2        43 hours ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.2               b06108e12275        43 hours ago        219.6 MB
ubuntu                     latest              e9ae3c220b23        3 weeks ago         187.9 MB

执行命令之后,我们就有了新的镜像。因此,可以通过commit来对容器做快照,是不是很酷?

5 根据Dockerfile编译镜像

通过docker commit可以从一个已有的容器得到镜像,那么如何从头生成一个镜像呢?我们可以通过docker build来进行。首先我们创建一个Dockerfile

root@gctest:~/ghostcloud# cat Dockerfile
# This is a comment
FROM ubuntu:latest
MAINTAINER Shev Yan <yandong@ghostcloud.cn>
CMD echo 'hello my image from Dockerfile.'

接着,我们进入Dockerfile所在的目录,并执行build:

root@gctest:~/ghostcloud# docker build -t myimage .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:latest
 ---> e9ae3c220b23
Step 2 : MAINTAINER Shev Yan <yandong@ghostcloud.cn>
 ---> Running in 9c78817e17f1
 ---> dab9385c6296
Removing intermediate container 9c78817e17f1
Step 3 : CMD echo 'hello my image from Dockerfile.'
 ---> Running in d8963fe120aa
 ---> cfcfc50fb4fb
Removing intermediate container d8963fe120aa
Successfully built cfcfc50fb4fb

FROM: 表示源自于哪个基础镜像 MAINTAINER: 表示由谁维护 CMD: 表示镜像默认执行的命令

让我们来查看一下新生成的Image:

root@gctest:~/ghostcloud# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED              VIRTUAL SIZE
myimage                    latest              cfcfc50fb4fb        About a minute ago   187.9 MB
ghostcloud                 test                df9744f284bd        5 minutes ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.7               553ff90cea5d        40 hours ago         219.6 MB
ghostcloud.cn:5005/gcali   1.0.6               0704f2860bd6        42 hours ago         219.6 MB
ghostcloud.cn:5005/gcali   1.0.3               746ac51185f2        43 hours ago         219.6 MB
ghostcloud.cn:5005/gcali   1.0.2               b06108e12275        43 hours ago         219.6 MB
ubuntu                     latest              e9ae3c220b23        3 weeks ago          187.9 MB

运行新生成的镜像:

root@gctest:~/ghostcloud# docker run myimage
hello my image from Dockerfile.

给Image设置tag:

root@gctest:~/ghostcloud# docker tag cfcf myimage:v1
root@gctest:~/ghostcloud# docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             VIRTUAL SIZE
myimage                    latest              cfcfc50fb4fb        8 minutes ago       187.9 MB
myimage                    v1                  cfcfc50fb4fb        8 minutes ago       187.9 MB
ghostcloud                 test                df9744f284bd        11 minutes ago      219.6 MB
ghostcloud.cn:5005/gcali   1.0.7               553ff90cea5d        41 hours ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.6               0704f2860bd6        42 hours ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.3               746ac51185f2        43 hours ago        219.6 MB
ghostcloud.cn:5005/gcali   1.0.2               b06108e12275        43 hours ago        219.6 MB
ubuntu                     latest              e9ae3c220b23        3 weeks ago         187.9 MB

6 删除Image

root@gctest:~/ghostcloud# docker rmi myimage:v1
Untagged: myimage:v1
上一篇:快速看懂备案取消接入须知和操作步骤


下一篇:express的路由配置优化