Docker 第三篇--构建Image
什么是 docker Image 和container?
我们先来看看官网是怎么说的。
Docker Engine provides the core Docker technology that enables images and containers. As the last step in your installation, you ran the docker run hello-world command. The command you ran had three parts.

An image is a filesystem and parameters to use at runtime. It doesn’t have state and never changes. A container is a running instance of an image. When you ran the command, Docker Engine:
- checked to see if you had the
hello-worldsoftware image - downloaded the image from the Docker Hub (more about the hub later)
- loaded the image into the container and “ran” it
Depending on how it was built, an image might run a simple, single command and then exit. This is what hello-world did.
A Docker image, though, is capable of much more. An image can start software as complex as a database, wait for you (or someone else) to add data, store the data for later use, and then wait for the next person.
Who built the hello-world software image though? In this case, Docker did but anyone can. Docker Engine lets people (or companies) create and share software through Docker images. Using Docker Engine, you don’t have to worry about whether your computer can run the software in a Docker image — a Docker container can always run it.
简单来说Image是一个无状态的文件系统可以在运行时接收参数, 根据这些参数创建instance. 而创建出来的Instance就是容器。
上一章我们在确认Docker安装是否成功的时候, 我们是通过运行一个容器进行的。
[root@master ~]# docker run --rm hello-world
那么容器是如何创建的呢? 容器的创建是通过image来的。 "hello-world" 就是我们的image.
我们可以查看本机的所有的Image:
[root@master ~]# clear
[root@master ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
hello-world latest c54a2cc56cbb months ago 1.848 kB
"hello-world"这个image我们是从docker hub上下载下来的。 我们要构建自己的applicaiton就要创建自己的container, 如果要创建自己的container那么我们就要创建自己的Image。
构建Image
- 定义自己的Dockerfile, 通过Dockerfile 我们就可以自动化的构建出我们自己的Image了。
FROM python:2.7
ENV http_proxy=http://x.x.x.x:8080
ENV https_proxy=https://x.x.x.x:8080
CMD mkdir /HelloWorld
WORKDIR /HelloWorld
ADD . /HelloWorld
RUN pip install -r requirements.txt
上面我们定义了一个自己的Dockerfile, 然后我们来逐条的解释下这个Dockerfile
- 我们要构建的是一个django的web app, 所以我们的image基于python:2.7的image来构建。
- 然后我们设置了2个环境变量, 分别设置了http和https的代理。这样做是为了可以在容器内通过pip安装我们需要的软件。
- 然后我们在容器内创建了一个目录'HelloWorld'并且设置为工作空间。
- 下一步我们把本机当前目录的内容添加到容器内的工作空间中。
- 最后我们安装所有我们需要的软件。
然后我们就可以通过 "docker build -t demo ." 来自动构建出我们的Image了。
[root@master HelloDocker]# docker build -t demo .
Sending build context to Docker daemon kB
Step : FROM python:2.7
2.7: Pulling from library/python 57de64c72267: Pull complete
4306be1e8943: Pull complete
871436ab7225: Downloading [=======================> ] 62.17 MB/129.8 MB
37c937b0ca47: Download complete
608a51124afe: Download complete
086c59e7b25f: Download complete
871436ab7225: Pull complete
37c937b0ca47: Pull complete
608a51124afe: Pull complete
086c59e7b25f: Pull complete
Digest: sha256:b21b2ba9b8bb8c8acc52915ac9c35be0bc08a9a7cb0a7852f8d2a0c5d4887f72
Status: Downloaded newer image for python:2.7
---> acf0d719f268
Step : ENV http_proxy http://10.144.1.10:8080
---> Running in 5d8fcead8508
---> 2bde4791e4f2
Removing intermediate container 5d8fcead8508
Step : ENV https_proxy https://10.144.1.10:8080
---> Running in 18560282c929
---> 30ca06433ccf
Removing intermediate container 18560282c929
Step : ENV PYTHONUNBUFFERED
---> Running in 0149dc451bd9
---> b7d4afee55d8
Removing intermediate container 0149dc451bd9
Step : CMD mkdir /HelloWorld
---> Running in 0ada9018a372
---> 9080558ca9b5
Removing intermediate container 0ada9018a372
Step : WORKDIR /HelloWorld
---> Running in 20fb62c6570e
---> 6399a4d5bc30
Removing intermediate container 20fb62c6570e
Step : ADD . /HelloWorld
---> efd3cc4074a5
Removing intermediate container 0fd7a739988e
Step : RUN pip install -r requirements.txt
---> Running in 2f473aab9dc0
Collecting cffi (from -r requirements.txt (line ))
Downloading cffi-1.9.-cp27-cp27mu-manylinux1_x86_64.whl (387kB)
Collecting cryptography (from -r requirements.txt (line ))
Downloading cryptography-1.7..tar.gz (420kB)
Collecting Django==1.8. (from -r requirements.txt (line ))
Downloading Django-1.8.-py2.py3-none-any.whl (.2MB)
Collecting dos2unix (from -r requirements.txt (line ))
Downloading dos2unix-.zip
Collecting enum34 (from -r requirements.txt (line ))
Downloading enum34-1.1.-py2-none-any.whl
Collecting httplib2 (from -r requirements.txt (line ))
Downloading httplib2-0.9..zip (210kB)
Collecting idna (from -r requirements.txt (line ))
Downloading idna-2.2-py2.py3-none-any.whl (55kB)
Collecting ipaddress (from -r requirements.txt (line ))
Downloading ipaddress-1.0.-py2-none-any.whl
Collecting MySQL-python (from -r requirements.txt (line ))
Downloading MySQL-python-1.2..zip (108kB)
Collecting ndg-httpsclient (from -r requirements.txt (line ))
Downloading ndg_httpsclient-0.4..tar.gz
Requirement already satisfied: pip in /usr/local/lib/python2./site-packages (from -r requirements.txt (line ))
Collecting pyasn1 (from -r requirements.txt (line ))
Downloading pyasn1-0.1.-py2.py3-none-any.whl
Collecting pycparser (from -r requirements.txt (line ))
Downloading pycparser-2.17.tar.gz (231kB)
Collecting pyOpenSSL (from -r requirements.txt (line ))
Downloading pyOpenSSL-16.2.-py2.py3-none-any.whl (43kB)
Collecting requests (from -r requirements.txt (line ))
Downloading requests-2.12.-py2.py3-none-any.whl (576kB)
Collecting scripts (from -r requirements.txt (line ))
Downloading scripts-2.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools in /usr/local/lib/python2./site-packages (from -r requirements.txt (line ))
Collecting six (from -r requirements.txt (line ))
Downloading six-1.10.-py2.py3-none-any.whl
Collecting sonarqube-api (from -r requirements.txt (line ))
Downloading sonarqube_api-1.3.-py2.py3-none-any.whl
Collecting stua (from -r requirements.txt (line ))
Downloading stua-0.2-py2.py3-none-any.whl
Collecting urllib3 (from -r requirements.txt (line ))
Downloading urllib3-1.19.-py2.py3-none-any.whl (104kB)
Collecting xlrd (from -r requirements.txt (line ))
Downloading xlrd-1.0..tar.gz (.6MB)
Collecting xlwt (from -r requirements.txt (line ))
Downloading xlwt-1.2.-py2.py3-none-any.whl (99kB)
Collecting pytz (from -r requirements.txt (line ))
Downloading pytz-2016.10-py2.py3-none-any.whl (483kB)
Building wheels for collected packages: cryptography, dos2unix, httplib2, MySQL-python, ndg-httpsclient, pycparser, xlrd
Running setup.py bdist_wheel for cryptography: started
Running setup.py bdist_wheel for cryptography: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels//c3/d6/cc2e097314f1a505e80e232cca8818242ec903f7d9fe727d05
Running setup.py bdist_wheel for dos2unix: started
Running setup.py bdist_wheel for dos2unix: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels//ad/a1/8b14b328d126d118e979fd7ff3979762d0a9ca236c594983dd
Running setup.py bdist_wheel for httplib2: started
Running setup.py bdist_wheel for httplib2: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/c7///e0be8ccfc1e08f8ff1f50d99ea5378e204580ea77b0169fb55
Running setup.py bdist_wheel for MySQL-python: started
Running setup.py bdist_wheel for MySQL-python: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels//a3//ec87e092cfb38450fc91a62562055231deb0049a029054dc62
Running setup.py bdist_wheel for ndg-httpsclient: started
Running setup.py bdist_wheel for ndg-httpsclient: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels//6b/b1/eef816d523c0aa93f350fd2a78d74769e010e2f26623921b76
Running setup.py bdist_wheel for pycparser: started
Running setup.py bdist_wheel for pycparser: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/a8/0b//dc95621f9d3a0da7bc191b8a71f0e8182ffd3cc5f33ac55005
Running setup.py bdist_wheel for xlrd: started
Running setup.py bdist_wheel for xlrd: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels//d4/6c/df6603e86ef3183ba2ecc97c5c3f1bf92802d54aa939522235
Successfully built cryptography dos2unix httplib2 MySQL-python ndg-httpsclient pycparser xlrd
Installing collected packages: pycparser, cffi, idna, pyasn1, six, enum34, ipaddress, cryptography, Django, dos2unix, httplib2, MySQL-python, pyOpenSSL, ndg-httpsclient, requests, stua, scripts, sonarqube-api, urllib3, xlrd, xlwt, pytz
Successfully installed Django-1.8. MySQL-python-1.2. cffi-1.9. cryptography-1.7. dos2unix- enum34-1.1. httplib2-0.9. idna-2.2 ipaddress-1.0. ndg-httpsclient-0.4. pyOpenSSL-16.2. pyasn1-0.1. pycparser-2.17 pytz-2016.10 requests-2.12. scripts-2.0 six-1.10. sonarqube-api-1.3. stua-0.2 urllib3-1.19. xlrd-1.0. xlwt-1.2.
---> 3dd17703d9ee
Removing intermediate container 2f473aab9dc0
Successfully built 3dd17703d9ee
[root@master HelloDocker]#
从上面这些执行的日志我们就可以清晰的看到docker 是怎么样一步一步的来构建一个image了, 并且可以和我们定义的Dockerfile的每一步对应起来。
这时我们可以查看下我们本地的Image了:
[root@master HelloDocker]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
demo latest 3dd17703d9ee 2 hours ago 726 MB
python 2.7 acf0d719f268 2 weeks ago 676.1 MB
hello-world latest c54a2cc56cbb months ago 1.848 kB
我们可以看到 我们构建的image "demo"已经出现了, 同时我们Dockerfile中依赖的那个Image也被下载了下来。
OK,有了Image那么我们就可以创建container了:
demo代码地址:
https://github.com/jalenyang/HelloDocker.git
Docker 第三篇--构建Image的更多相关文章
- docker第三篇 镜像管理基础
docker 工作原理: 常用的命令docker run .create .start... 都是客户端命令 Docker Daemon 接收到客户端传过来的命令以后 docker daemon会根据 ...
- docker+k8s基础篇三
Docker+K8s基础篇(三) kubernetes上的资源 A:k8s上的常用资源 Pod的配置清单 A:Pod上的清单定义 B:Pod创建资源的方法 C:spec下其它字段的介绍 Pod的生命周 ...
- Docker系列教程04-Docker构建镜像的三种方式
简介 创建镜像的方法主要有三种:基于已有镜像的容器创建.基于本地模板导入.基于Dockerfile创建. 今天就逐一讲述为大家讲述,如何构建属于自己的docker镜像. 1.基于容器构建镜像 基于已有 ...
- 深入浅出Docker(三):Docker开源之路
背景 Docker从一开始的概念阶段就致力于使用开源驱动的方式来发展,它的成功缘于国外成熟的开源文化氛围,以及可借鉴的社区运营经验.通过本文详细的介绍,让大家可以全面了解一个项目亦或者一项技术是如何通 ...
- AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排
一.前言 上一篇部署了一个最基础的helloworld应用,创建了两个容器和一个network,还算应付得过来. 如果该应用继续引入mysql.redis.job等若干服务,到时候发布一次得工作量之大 ...
- Docker基础用法篇
Docker基础用法篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装docker 1>.依赖的基础环境 64 bits CPU Linux Kerner 3.10+ ...
- docker+k8s基础篇五
Docker+K8s基础篇(五) service资源介绍 A:service资源的工作特性 service的使用 A:service字段介绍 B:ClusterIP的简单使用 C:NodePort的简 ...
- docker+k8s基础篇二
Docker+K8s基础篇(二) docker的资源控制 A:docker的资源限制 Kubernetes的基础篇 A:DevOps的介绍 B:Kubernetes的架构概述 C:Kubernetes ...
- docker+k8s基础篇一
Docker+K8s基础篇(一) docker的介绍 A:为什么是docker B:k8s介绍 docker的使用 A:docker的安装 B:docker的常用命令 C:docker容器的启动和操作 ...
随机推荐
- CodeIgniter学习一:基础知识
1. url片段(CI域名组成说明) example.com/index.php/test/index 第一部分(test):控制器 第二部分(index):方法,动作 如果第二部分 ...
- 【工作笔记一】【转】Visual Studio 2012常用快捷键总结
Visual Studio 2012常用快捷键总结 原文 http://blog.csdn.net/yl2isoft/article/details/9886379 写在前面: 都知道,合理使用 ...
- linux sendmail 邮件服务器架设(fedora 8)
linux sendmail 邮件服务器架设(fedora 8) 2009-01-22 17:27 3316人阅读 评论(2) 收藏 举报 邮件服务器linuxprotocolscaching测试lo ...
- MVC 过滤器1
ASP.NET MVC 过滤器(一) 前言 前面的篇幅中,了解到了控制器的生成的过程以及在生成的过程中的各种注入点,按照常理来说篇幅应该到了讲解控制器内部的执行过程以及模型绑定.验证这些知识了.但是呢 ...
- Dynamics CRM 多个Form显示不同的Ribbon按钮
昨天群里熊宸(Microfoft MVP)给大家分享了以“Dynamics CRM 客户端编程概况”为主题,在群里给大家做了一次精彩的分享. 一些朋友聊到了Ribbon按钮在Form上的显示问题,因为 ...
- 10.26最后的模拟DAY2 改造二叉树[中序遍历+严格递增的最长不下降子序列]
改造二叉树 [题目描述] 小Y在学树论时看到了有关二叉树的介绍:在计算机科学中,二叉树是每个结点最多有两个子结点的有序树.通常子结点被称作“左孩子”和“右孩子”.二叉树被用作二叉搜索树和二叉堆.随后他 ...
- TDD单元测试驱动
使用IdleTest进行TDD单元测试驱动开发演练(2) [前言] 1. 有关上篇请参见<使用IdleTest进行TDD单元测试驱动开发演练(1)>,有关本篇用到Entity Fram ...
- office文件的预览
使用FlexPaper实现office文件的预览(C#版) 需求很简单,用户上传office文件(word.excel.ppt)后,可以预览上传的这些文件.搜索的相关的资料后.整理如下: Step1. ...
- FlexPaper+SWFTool+操作类=在线预览PDF(转)
引言 由于客户有在线预览PDF格式的需求,在网上找了一下解决方案,觉得FlexPaper用起来还是挺方便的,flexpaper是将pdf转换为swf格式的文件预览的,所以flexpaper一般和swf ...
- 图解Javascript引用类型之数组
以图说事明理,恰当时候会事半功陪.今天我就尝试着用图的方式讲讲“JavaScript引用类型之数组”.望更多童鞋给我反馈! 好东西分享给大家,但要尊重事实!!!因此特别说明:本图非我本人亲自所作,乃我 ...