设为首页 收藏本站
查看: 1227|回复: 0

[经验分享] Working with Docker Images

[复制链接]

尚未签到

发表于 2015-10-13 09:54:09 | 显示全部楼层 |阅读模式
  原文地址:https://docs.docker.com/userguide/dockerimages/
  


Working with Docker Images

  

In the introduction we've discovered that Docker images are the basis of containers. In the previous sectionswe've
used Docker images that already exist, for example the ubuntu image
and thetraining/webapp image.


We've also discovered that Docker stores downloaded images on the Docker host. If an image isn't already present on the host then it'll be downloaded from a registry: by default the Docker
Hub public registry.


In this section we're going to explore Docker images a bit more including:



  • Managing and working with images locally on your Docker host;
  • Creating basic images;
  • Uploading images to Docker Hub.

在介绍中,我们知道docker images是docker的基础容器。在之前的文章中,我们使用了一个已经存在的images,比如ubuntu和training/webapp 这2个images  docker把下载的images存储到docker主机上,如果一个image不在主机上,docker会从一个镜像仓库下载,默认的仓库是 DOCKER HUB 公共仓库。
  接下来将介绍更多关于docker images的内容,包括:
  


  • 使用和管理本地主机上的images
  • 创建一个基础的images
  • 上传images到docker hub(公共images仓库)


Listing images on the host


列出本地主机上已经存在的images

Let's start with listing the images we have locally on our host. You can do this using the docker
imagescommand like so:


使用 docker images 显示本机上的images


$ sudo docker images
REPOSITORY       TAG      IMAGE ID      CREATED      VIRTUAL SIZE
training
/webapp  latest   fc77f57ad303  3 weeks ago  280.5 MB
ubuntu           
13.10    5e019ab7bf6d  4 weeks ago  180 MB
ubuntu           saucy   
5e019ab7bf6d  4 weeks ago  180 MB
ubuntu           
12.04    74fe38d11401  4 weeks ago  209.6 MB
ubuntu           precise  
74fe38d11401  4 weeks ago  209.6 MB
ubuntu           
12.10    a7cf8ae4e998  4 weeks ago  171.3 MB
ubuntu           quantal  a7cf8ae4e998  
4 weeks ago  171.3 MB
ubuntu           
14.04    99ec81b80c55  4 weeks ago  266 MB
ubuntu           latest   
99ec81b80c55  4 weeks ago  266 MB
ubuntu           trusty   
99ec81b80c55  4 weeks ago  266 MB
ubuntu           
13.04    316b678ddf48  4 weeks ago  169.4 MB
ubuntu           raring   
316b678ddf48  4 weeks ago  169.4 MB
ubuntu           
10.04    3db9c44f4520  4 weeks ago  183 MB
ubuntu           lucid   
3db9c44f4520  4 weeks ago  183 MB

We can see the images we've previously used in our user guide. Each has been downloaded from Docker
Hub when we launched a container using that image.


我们看到了在user guaide中使用过的images,当我们启动一个使用这个image的容器时,docker会从docker hub下载它。


We can see three crucial pieces of information about our images in the listing.


在列出信息中,我们可以看到3个字段信息



  • What repository they came from, for example ubuntu.来自于哪个仓库,比如ubuntu
  • The tags for each image, for example 14.04.image的标记,比如
    14.04
  • The image ID of each image.它的ID号


A repository potentially holds multiple variants of an image. In the case of our ubuntu image
we can see multiple variants covering Ubuntu 10.04, 12.04, 12.10, 13.04, 13.10 and 14.04. Each variant is identified by a tag and you can refer to a tagged image like so:


一个仓库可能有一个images的都个发行版,比如ubuntu,他们有10.04 12.04 12.10 13.04 14.04,每个发行版的标记都不同,可以使用tag命令来指定images




ubuntu:14.04
  



So when we run a container we refer to a tagged image like so:


使用一个images的标记来启动容器


$ sudo docker run -t -i ubuntu:14.04 /bin/bash

If instead we wanted to build an Ubuntu 12.04 image we'd use:


$ sudo docker run -t -i ubuntu:12.04 /bin/bash

If you don't specify a variant, for example you just use ubuntu,
then Docker will default to using theubuntu:latest image.


如果你不指定具体的发行版,比如仅使用ubuntu,那么docker会使用最新的发行版ubuntu:latest



Tip: We recommend you always use a specific tagged image, for example ubuntu:12.04.
That way you always know exactly what variant of an image is being used.

提示:我们建议最好指定发行版,只有这样你才可以保证你真正使用的image是那个


Getting a new image


获取一个新的image

So how do we get new images? Well Docker will automatically download any image we use that isn't already present on the Docker host. But this can potentially add some time to the launch of a container. If we want to pre-load an image we can download it using
the docker pull command. Let's say
we'd like to download the centos image.


我们如何获取新的images呢?当我们启动容器使用的image不再本地主机上时,docker会自动下载他们。这很耗时,我们可以使用docker pull命令来预先下载我们需要的image。下面的例子下载一个centos镜像。


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

We can see that each layer of the image has been pulled down and now we can run a container from this image and we won't have to wait to download the image.


我们可以看到下载的image的每一个层次,这样当我们使用这个image来启动容器的时候,它就可以马上启动了。


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

Finding images


查找images

One of the features of Docker is that a lot of people have created Docker images for a variety of purposes. Many of these have been uploaded to Docker
Hub. We can search these images on the Docker Hubwebsite.


docker的一个特性是很多人因为各种不同的用途创建了各种不同的images。它们都被上传到了docker hub共有仓库上,我们可以在docker hub的网站上来查找它们。




We can also search for images on the command line using the docker
search command. Let's say our team wants an image with Ruby and Sinatra installed on which to do our web application development. We can search for a suitable image by using the docker
search command to find all the images that contain the term sinatra.


使用docker search命令。比如,当我们的团队需要ruby和sinatra作为web应用程序的开发时,我们使用docker search 来搜索合适的image,使用关键字sinatra


$ sudo docker search sinatra
NAME                                   DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
training
/sinatra                       Sinatra training image                          0                    [OK]
marceldegraaf
/sinatra                  Sinatra test app                                0
mattwarren
/docker-sinatra-demo                                                         0                    [OK]
luisbebop
/docker-sinatra-hello-world                                                   0                    [OK]
bmorearty
/handson-sinatra              handson-ruby + Sinatra for Hands on with D...   0
subwiz
/sinatra                                                                         0
bmorearty
/sinatra                                                                      0
. . .

We can see we've returned a lot of images that use the term sinatra.
We've returned a list of image names, descriptions, Stars (which measure the social popularity of images - if a user likes an image then they can "star" it), and the Official and Automated build statuses. Official repositories are built and maintained by the Stackbrew project,
and Automated repositories are Automated Builds that allow you to validate the source and content of
an image.


我们看到返回了很多包含sinatra的images。其中包括image名字、描述、星级(表示该image的受欢迎程度)、是否官方创建、是否自动创建。官方的images是stackbrew项目组创建和维护的,autimated 资源允许你验证image的来源和内容。


We've  the images available to use and we decided to use the training/sinatra image.
So far we've seen two types of images repositories, images like ubuntu,
which are called base or root images. These base images are provided by Docker Inc and are built, validated and supported. These can be identified by their single word names.


现在我们已经回顾了可用的images,并决定使用training/sinatra镜像。到目前为止,我们看到了2种images 资源。比如ubuntu,被称为基础或则根镜像。这些基础镜像是docker公司创建、验证、支持、提供。他们往往使用一个单词作为他们的名字。


We've also seen user images, for example the training/sinatra image
we've chosen. A user image belongs to a member of the Docker community and is built and maintained by them. You can identify user images as they are always prefixed with the user name, here training,
of the user that created them.


还有一种类型,比如我们选择的training/sinatra镜像。它是由docker的用户创建并维护的,你可以通过指定image名字的前缀来指定他们,比如training。  


Pulling our image


下载我们的image

We've identified a suitable image, training/sinatra,
and now we can download it using the docker
pull command.


现在我们指定了一个image,training/sinatra,我们可以使用docker pull命令来下载它


$ sudo docker pull training/sinatra

The team can now use this image by run their own containers.


然后我们就可以使用这个image来启动容器了


$ sudo docker run -t -i training/sinatra /bin/bash
root@a8cb6ce02d85
:/#

Creating our own images


创建我们自己的images

The team has found the training/sinatra image
pretty useful but it's not quite what they need and we need to make some changes to it. There are two ways we can update and create images.


别人的镜像虽然好,但不一定适合我们。我们可以对他们做一些改变,有2中方法:



  • We can update a container created from an image and commit the results to an image。使用image启动容器,并做更新后提交结果到新的image
  • We can use a Dockerfile to
    specify instructions to create an image. 使用dockerfile 创建新的image



Updating and committing an image


更新并提交一个image

To update an image we first need to create a container from the image we'd like to update.


当我们要更新一个image时,我们先使用它来启动一个容器


$ sudo docker run -t -i training/sinatra /bin/bash
root@0b2616b0e5a8
:/#

Note: Take note of the container ID that has been created, 0b2616b0e5a8,
as we'll need it in a moment.
注意:记住容器的ID ,稍后我们还会用到



Inside our running container let's add the json gem.


这里我们在容器中添加json gem


root@0b2616b0e5a8:/# gem install json

Once this has completed let's exit our container using the exit command.


当结束后,我们使用exit来退出


Now we have a container with the change we want to make. We can then commit a copy of this container to an image using the docker
commit command.


现在我们的容器已经被我们改变了,使用docker commint命令来提交相应的副本。


$ sudo docker commit -m="Added json gem" -a="Kate Smith" \
0b2616b0e5a8 ouruser/sinatra:v2
4f177bd27a9ff0f6dc2a830403925b5360bfe0b93d476f7fc3231110e7f71b1c

Here we've used the docker commit command.
We've specified two flags: -m and -a.
The -m flag allows us to specify
a commit message, much like you would with a commit on a version control system. The-a flag
allows us to specify an author for our update.


-m 来指定提交的信息,跟我们使用的版本控制工具一样。-a 可以指定我们更新的用户信息


We've also specified the container we want to create this new image from, 0b2616b0e5a8 (the
ID we recorded earlier) and we've specified a target for the image:


指定我们要从哪个容器ID来创建我们的副本,最后指定目标image的名字。


ouruser/sinatra:v2

Let's break this target down. It consists of a new user, ouruser,
that we're writing this image to. We've also specified the name of the image, here we're keeping the original image name sinatra.
Finally we're specifying a tag for the image: v2.


这个例子里面,我们指定了一个新用户,ouruser,使用了sinatra的image,最后指定了image的标记v2


We can then look at our new ouruser/sinatra image
using the docker images command.


使用docker images来查看我们创建的新image


$ sudo docker images
REPOSITORY          TAG     IMAGE ID       CREATED       VIRTUAL SIZE
training
/sinatra    latest  5bc342fa0b91   10 hours ago  446.7 MB
ouruser
/sinatra     v2      3c59e02ddd1a   10 hours ago  446.7 MB
ouruser
/sinatra     latest  5db5f8471261   10 hours ago  446.7 MB

To use our new image to create a container we can then:


使用新的image来启动容器


$ sudo docker run -t -i ouruser/sinatra:v2 /bin/bash
root@78e82f680994
:/#

Building an image from a Dockerfile


从dockerfile 来创建 image

Using the docker commit command is
a pretty simple way of extending an image but it's a bit cumbersome and it's not easy to share a development process for images amongst a team. Instead we can use a new command, docker
build, to build new images from scratch.


使用docker commit 来扩张一个image比较简单,但它不容易在一个团队中分享它。我们使用docker build 来创建一个新的image


To do this we create a Dockerfile that
contains a set of instructions that tell Docker how to build our image.


为此,我们需要创建一个dockerfile,包含一些如何创建我们的image的指令


Let's create a directory and a Dockerfile first.


现在,我们来创建一个目录和一个dockerfile


$ mkdir sinatra
$ cd sinatra
$ touch
Dockerfile

Each instruction creates a new layer of the image. Let's look at a simple example now for building our own Sinatra image for our development team.


每一条指令都创建一个image的新的一层,下面是一个简单的例子,关于如何为我们的开发团队创建sinatra镜像


# This is a comment
FROM ubuntu
:14.04
MAINTAINER
Kate Smith <ksmith@example.com>
RUN apt
-get -qq update
RUN apt
-get -qqy install ruby ruby-dev
RUN gem install sinatra


Let's look at what our Dockerfile does.
Each instruction prefixes a statement and is capitalized.


INSTRUCTION statement

Note: We use # to
indicate a comment
使用#来注释



The first instruction FROM tells
Docker what the source of our image is, in this case we're basing our new image on an Ubuntu 14.04 image.


FROM指令告诉docker 使用哪个image源,


Next we use the MAINTAINER instruction
to specify who maintains our new image.


接着是维护者的信息


Lastly, we've specified three RUN instructions.
A RUN instruction executes a command
inside the image, for example installing a package. Here we're updating our APT cache, installing Ruby and RubyGems and then installing the Sinatra gem.


最后,我们指定了3条run指令。每一条run指令在image执行一条命令,比如安装一个软件包,在这里我们使用apt 来安装了一些软件






Note: There are a lot more
instructions available to us in a Dockerfile.



Now let's take our Dockerfile and
use the docker build command to
build an image.


现在,让我们来使用docker build来通过dockerfile创建image
$ sudo docker build -t=&quot;ouruser/sinatra:v2&quot; .
Uploading context  2.56 kB
Uploading context
Step 0 : FROM ubuntu:14.04
---> 99ec81b80c55
Step 1 : MAINTAINER Kate Smith <ksmith@example.com>
---> Running in 7c5664a8a0c1
---> 2fa8ca4e2a13
Removing intermediate container 7c5664a8a0c1
Step 2 : RUN apt-get -qq update
---> Running in b07cc3fb4256
---> 50d21070ec0c
Removing intermediate container b07cc3fb4256
Step 3 : RUN apt-get -qqy install ruby ruby-dev
---> Running in a5b038dd127e
Selecting previously unselected package libasan0:amd64.
(Reading database ... 11518 files and directories currently installed.)
Preparing to unpack .../libasan0_4.8.2-19ubuntu1_amd64.deb ...
. . .
Setting up ruby (1:1.9.3.4) ...
Setting up ruby1.9.1 (1.9.3.484-2ubuntu1) ...
Processing triggers for libc-bin (2.19-0ubuntu6) ...
---> 2acb20f17878
Removing intermediate container a5b038dd127e
Step 4 : RUN gem install sinatra
---> Running in 5e9d0065c1f7
. . .
Successfully installed rack-protection-1.5.3
Successfully installed sinatra-1.4.5
4 gems installed
---> 324104cde6ad
Removing intermediate container 5e9d0065c1f7
Successfully built 324104cde6ad

We've specified our docker build command
and used the -t flag to identify
our new image as belonging to the user ouruser,
the repository name sinatra and
given it the tag v2.


使用-t标记来指定新的image的用户信息和命令


We've also specified the location of our Dockerfile using
the . to indicate a Dockerfile in
the current directory.


使用了.来指出dockerfile的位置在当前目录



Note: You can also specify a path to a Dockerfile.
注意:你也可以指定一个dockfile的路径



Now we can see the build process at work. The first thing Docker does is upload the build context: basically the contents of the directory you're building in. This is done because the Docker daemon does the actual build of the image and it needs the local context
to do it.


我们可以看到build进程在执行操作。它要做的第一件事情就是上传这个dockfile内容,因为所有的操作都要依据它来进行。


Next we can see each instruction in the Dockerfile being
executed step-by-step. We can see that each step creates a new container, runs the instruction inside that container and then commits that change - just like the docker
commit work flow we saw earlier. When all the instructions have executed we're left with the 324104cde6ad image
(also helpfully tagged as ouruser/sinatra:v2)
and all intermediate containers will get removed to clean things up.


然后,我们看到dockfile中的指令被一条一条的执行。每一步都创建了一个新的容器,在容器中执行指令并提交就跟之前介绍过的docker commit一样。当所有的指令都执行完毕之后,返回了一个image id,并且所有的中间步骤所产生的容器都被删除和清理了。



Note: An image can't have more than 127 layers regardless of the storage driver. This limitation is set globally to encourage optimization of the overall size
of images.
注意:一个image不能



We can then create a container from our new image.


$ sudo docker run -t -i ouruser/sinatra:v2 /bin/bash
root@8196968dac35
:/#

Note: This is just the briefest introduction to creating images. We've skipped a whole bunch of other instructions that you can use. We'll see more of those instructions
in later sections of the Guide or you can refer to the Dockerfile reference
for a detailed description and examples of every instruction.



Setting tags on an image



You can also add a tag to an existing image after you commit or build it. We can do this using the docker
tag command. Let's add a new tag to our ouruser/sinatra image.


$ sudo docker tag 5db5f8471261 ouruser/sinatra:devel

The docker tag command takes the
ID of the image, here 5db5f8471261,
and our user name, the repository name and the new tag.


Let's see our new tag using the docker images command.


$ sudo docker images ouruser/sinatra
REPOSITORY          TAG     IMAGE ID      CREATED        VIRTUAL SIZE
ouruser
/sinatra     latest  5db5f8471261  11 hours ago   446.7 MB
ouruser
/sinatra     devel   5db5f8471261  11 hours ago   446.7 MB
ouruser
/sinatra     v2      5db5f8471261  11 hours ago   446.7 MB

Push an image to Docker Hub



Once you've built or created a new image you can push it to Docker Hub using the docker
pushcommand. This allows you to share it with others, either publicly, or push it into a private repository.


使用docker push上传image


$ sudo docker push ouruser/sinatra
The push refers to a repository [ouruser/sinatra] (len: 1)
Sending image list
Pushing repository ouruser/sinatra (3 tags)
. . .

Remove an image from the host


从本地移除image

You can also remove images on your Docker host in a way similar to containers using the docker
rmicommand.


dcoker rmi 移除


Let's delete the training/sinatra image
as we don't need it anymore.


$ sudo docker rmi training/sinatra
Untagged: training/sinatra:latest
Deleted: 5bc342fa0b91cabf65246837015197eecfa24b2213ed6a51a8974ae250fedd8d
Deleted: ed0fffdcdae5eb2c3a55549857a8be7fc8bc4241fb19ad714364cbfd7a56b22f
Deleted: 5c58979d73ae448df5af1d8142436d81116187a7633082650549c52c3a2418f0

Note: In order to remove an image from the host, please make sure that there are no containers actively based on it.



Next steps



Until now we've seen how to build individual applications inside Docker containers. Now learn how to build whole application stacks with Docker by linking together multiple Docker containers.


Go to Linking Containers Together.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-126159-1-1.html 上篇帖子: 干货 | Docker文件系统的分层与隔离 下篇帖子: Docker最有价值的八个应用场景
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表