什么是 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-world software 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

  1. 定义自己的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的更多相关文章

  1. docker第三篇 镜像管理基础

    docker 工作原理: 常用的命令docker run .create .start... 都是客户端命令 Docker Daemon 接收到客户端传过来的命令以后 docker daemon会根据 ...

  2. docker+k8s基础篇三

    Docker+K8s基础篇(三) kubernetes上的资源 A:k8s上的常用资源 Pod的配置清单 A:Pod上的清单定义 B:Pod创建资源的方法 C:spec下其它字段的介绍 Pod的生命周 ...

  3. Docker系列教程04-Docker构建镜像的三种方式

    简介 创建镜像的方法主要有三种:基于已有镜像的容器创建.基于本地模板导入.基于Dockerfile创建. 今天就逐一讲述为大家讲述,如何构建属于自己的docker镜像. 1.基于容器构建镜像 基于已有 ...

  4. 深入浅出Docker(三):Docker开源之路

    背景 Docker从一开始的概念阶段就致力于使用开源驱动的方式来发展,它的成功缘于国外成熟的开源文化氛围,以及可借鉴的社区运营经验.通过本文详细的介绍,让大家可以全面了解一个项目亦或者一项技术是如何通 ...

  5. AspNetCore容器化(Docker)部署(三) —— Docker Compose容器编排

    一.前言 上一篇部署了一个最基础的helloworld应用,创建了两个容器和一个network,还算应付得过来. 如果该应用继续引入mysql.redis.job等若干服务,到时候发布一次得工作量之大 ...

  6. Docker基础用法篇

    Docker基础用法篇 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装docker 1>.依赖的基础环境 64 bits CPU Linux Kerner 3.10+ ...

  7. docker+k8s基础篇五

    Docker+K8s基础篇(五) service资源介绍 A:service资源的工作特性 service的使用 A:service字段介绍 B:ClusterIP的简单使用 C:NodePort的简 ...

  8. docker+k8s基础篇二

    Docker+K8s基础篇(二) docker的资源控制 A:docker的资源限制 Kubernetes的基础篇 A:DevOps的介绍 B:Kubernetes的架构概述 C:Kubernetes ...

  9. docker+k8s基础篇一

    Docker+K8s基础篇(一) docker的介绍 A:为什么是docker B:k8s介绍 docker的使用 A:docker的安装 B:docker的常用命令 C:docker容器的启动和操作 ...

随机推荐

  1. 点击按钮,通过JS代码实现复制INPUT表单,表格:

    获取表单: <SCRIPT LANGUAGE="JavaScript"> function copyinput() { var input=document.getEl ...

  2. lsof基本使用

    当你想在计算机上启动一个服务,电脑已经建议"port already in use",此时,可以使用lsof命令查看占用端口的进程(lsof -i:port). lsof这是LiS ...

  3. 浏览器检测(BrowserDetect.js)使用

    浏览器检测是在工作中经常用到的,如果只是简单判断当前是什么浏览器的话可以通过window.navigator.useragent这样的js来直接判断就可以了! 但是针对浏览器版本要求比较高的时候,如果 ...

  4. .Net中批量添加数据的几种实现方法比较

    在.Net中经常会遇到批量添加数据,如将Excel中的数据导入数据库,直接在DataGridView控件中添加数据再保存到数据库等等. 方法一:一条一条循环添加 通常我们的第一反应是采用for或for ...

  5. asp.net访问WebService的各种方式

    WebService的访问形式主要有:SOAP调用.XMLHTTP POST.GET调用.MicroSoft.XMLDOMC调用.webbehavior.htc调用 我们知道的在C#后台本地调用Web ...

  6. WCF 服务端异常封装

    通常WCF服务端异常的详细信息只有在调试环境下才暴露出来,但我目前有需求需要将一部分异常的详细信息传递到客户端,又需要保证一定的安全性. 最简单的办法当然是在服务端将异常捕获后,序列化传给客户端,但这 ...

  7. windows server 2003断开远程之后自动注销用户

    windows server 2003断开远程之后自动注销用户 2011-07-30 09:42:52     我来说两句      收藏    我要投稿 最近一台服务器老是断开远程之后过没多久就自动 ...

  8. Redmine(Ruby)配置经验

    Redmine(Ruby)配置经验记录在配置Redmine邮件同步过程中遇到的各种问题与解决方法 1. 如何安装Redminehttp://www.redmine.org/projects/redmi ...

  9. JQuery的两个each方法的注意点

    Jquery官网上两个each用法: http://api.jquery.com/jQuery.each/ http://api.jquery.com/each/ 使用时注意点 <!DOCTYP ...

  10. Dom解析xml源代码

    import java.io.File; import java.io.IOException; import javax.xml.parsers.DocumentBuilder; import ja ...