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-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        6 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   277 kB
Step 1 : 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 2 : ENV http_proxy http://10.144.1.10:8080
 ---> Running in 5d8fcead8508
 ---> 2bde4791e4f2
Removing intermediate container 5d8fcead8508
Step 3 : ENV https_proxy https://10.144.1.10:8080
 ---> Running in 18560282c929
 ---> 30ca06433ccf
Removing intermediate container 18560282c929
Step 4 : ENV PYTHONUNBUFFERED 1
 ---> Running in 0149dc451bd9
 ---> b7d4afee55d8
Removing intermediate container 0149dc451bd9
Step 5 : CMD mkdir /HelloWorld
 ---> Running in 0ada9018a372
 ---> 9080558ca9b5
Removing intermediate container 0ada9018a372
Step 6 : WORKDIR /HelloWorld
 ---> Running in 20fb62c6570e
 ---> 6399a4d5bc30
Removing intermediate container 20fb62c6570e
Step 7 : ADD . /HelloWorld
 ---> efd3cc4074a5
Removing intermediate container 0fd7a739988e
Step 8 : RUN pip install -r requirements.txt
 ---> Running in 2f473aab9dc0
Collecting cffi (from -r requirements.txt (line 1))
  Downloading cffi-1.9.1-cp27-cp27mu-manylinux1_x86_64.whl (387kB)
Collecting cryptography (from -r requirements.txt (line 2))
  Downloading cryptography-1.7.1.tar.gz (420kB)
Collecting Django==1.8.11 (from -r requirements.txt (line 3))
  Downloading Django-1.8.11-py2.py3-none-any.whl (6.2MB)
Collecting dos2unix (from -r requirements.txt (line 4))
  Downloading dos2unix-1.zip
Collecting enum34 (from -r requirements.txt (line 5))
  Downloading enum34-1.1.6-py2-none-any.whl
Collecting httplib2 (from -r requirements.txt (line 6))
  Downloading httplib2-0.9.2.zip (210kB)
Collecting idna (from -r requirements.txt (line 7))
  Downloading idna-2.2-py2.py3-none-any.whl (55kB)
Collecting ipaddress (from -r requirements.txt (line 8))
  Downloading ipaddress-1.0.18-py2-none-any.whl
Collecting MySQL-python (from -r requirements.txt (line 9))
  Downloading MySQL-python-1.2.5.zip (108kB)
Collecting ndg-httpsclient (from -r requirements.txt (line 10))
  Downloading ndg_httpsclient-0.4.2.tar.gz
Requirement already satisfied: pip in /usr/local/lib/python2.7/site-packages (from -r requirements.txt (line 11))
Collecting pyasn1 (from -r requirements.txt (line 12))
  Downloading pyasn1-0.1.9-py2.py3-none-any.whl
Collecting pycparser (from -r requirements.txt (line 13))
  Downloading pycparser-2.17.tar.gz (231kB)
Collecting pyOpenSSL (from -r requirements.txt (line 14))
  Downloading pyOpenSSL-16.2.0-py2.py3-none-any.whl (43kB)
Collecting requests (from -r requirements.txt (line 15))
  Downloading requests-2.12.4-py2.py3-none-any.whl (576kB)
Collecting scripts (from -r requirements.txt (line 16))
  Downloading scripts-2.0-py2.py3-none-any.whl
Requirement already satisfied: setuptools in /usr/local/lib/python2.7/site-packages (from -r requirements.txt (line 17))
Collecting six (from -r requirements.txt (line 18))
  Downloading six-1.10.0-py2.py3-none-any.whl
Collecting sonarqube-api (from -r requirements.txt (line 19))
  Downloading sonarqube_api-1.3.0-py2.py3-none-any.whl
Collecting stua (from -r requirements.txt (line 20))
  Downloading stua-0.2-py2.py3-none-any.whl
Collecting urllib3 (from -r requirements.txt (line 21))
  Downloading urllib3-1.19.1-py2.py3-none-any.whl (104kB)
Collecting xlrd (from -r requirements.txt (line 22))
  Downloading xlrd-1.0.0.tar.gz (2.6MB)
Collecting xlwt (from -r requirements.txt (line 23))
  Downloading xlwt-1.2.0-py2.py3-none-any.whl (99kB)
Collecting pytz (from -r requirements.txt (line 24))
  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/35/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/98/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/67/60/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/38/a3/89/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/44/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/41/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/40/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.11 MySQL-python-1.2.5 cffi-1.9.1 cryptography-1.7.1 dos2unix-1 enum34-1.1.6 httplib2-0.9.2 idna-2.2 ipaddress-1.0.18 ndg-httpsclient-0.4.2 pyOpenSSL-16.2.0 pyasn1-0.1.9 pycparser-2.17 pytz-2016.10 requests-2.12.4 scripts-2.0 six-1.10.0 sonarqube-api-1.3.0 stua-0.2 urllib3-1.19.1 xlrd-1.0.0 xlwt-1.2.0
 ---> 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        6 months ago        1.848 kB

我们可以看到 我们构建的image "demo"已经出现了, 同时我们Dockerfile中依赖的那个Image也被下载了下来。

OK,有了Image那么我们就可以创建container了:

demo代码地址:
https://github.com/jalenyang/HelloDocker.git 
在路上,不顾风雨兼程
原文地址:https://www.cnblogs.com/crazyyang/p/6276919.html