什么是 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. [Dev Blog] KCV插件 —— Provissy Tools 。

    承蒙各位支持! 正式版已推出,请前往http://tieba.baidu.com/p/3398574166 或者前往:http://provissy.com/?p=7 请不要在这里回复,我无法保证回复 ...

  2. 简单分析android textview xml 的属性设置

    android:ems 设置TextView的宽度为N个字符的宽度. 这样的好处就是,在定义编辑框空间输入多少字符的时候,可以根据固定的值设置编辑框宽度.保证边框和文字的宽度统一.android:ma ...

  3. selenium + python 部署自动化测试环境

    选择selenium和python其实是怀有私心的:码两行python,熟悉熟悉.    selenium优点很多,我最看重的是支持多语言,足够简单,同时支持浏览器. 实际工作中,简单实用真的太重要了 ...

  4. every、some、filter、map、forEach 方法的区别总结

    API功能描述: [every]:Boolean 遍历数组并执行回调,如果每个数组元素都能通过回调函数的测试则返回true,否则返回false.一旦返回false,将立即终止循环. [some]:Bo ...

  5. 状态机图statechart diagram

    [UML]UML系列——状态机图statechart diagram 系列文章 [UML]UML系列——用例图Use Case [UML]UML系列——用例图中的各种关系(include.extend ...

  6. Dotfuscator注册码和XenoCode注册码

    .net加密工具注册码:Dotfuscator注册码如下:     Your   serial   number   is   14705     Your   Confirmation   Numb ...

  7. Grub禁用UUID

    这个属于一个个人喜好问题,我每次看到 df -h 的结果都很郁闷,根目录那一行设备是用uuid表示的,那一串字符真是够长的,看起来非常别扭,所以就自己修改了一下/etc/default/grub文件. ...

  8. DevExpress 学习使用之 SplitContainerControl

    无论是 .net framework 自带还是第三方组件,使用 Split 类控件时通常其 Panel 中都会包含多个子控件,在运行时不可避免遇到因改变 splitter 位置或改变窗体大小引起的界面 ...

  9. ssdt_hook NtOpenProcess

        获取ssdt表中所有函数的地址 for (int i = 0; i < KeServiceDescriptorTable->NumberOfServices; i++) {     ...

  10. vs中web配置可浏览json数据文件

    在日常的前端开发中,我们会制作一些数据文件,常用的有后缀名为json的文件,但是vs在启动项目时,是不能浏览json文件的,常见的错误提示如下图所示 解决这个错误,只需要在web.config文件中配 ...