docker level1
所有的Dockerfile 都需要FROM命令,这会设置基础镜像 base image。
例如
`FROM ubuntu`
这会为下面的命令设置一个镜像的上下文。
##RUN 命令
RUN 会以FROM 镜像的上下文运行run命令,并且commit结果。产生的结果会用于Dockerfile的下一个命令。
commit是非常廉价的,集装箱可以从任何镜像的历史中产生,非常像版本控制。
`RUN command` 就相当于 `docker run image command + docker commit container_id` 的效果
例如
```
FROM ubuntu
RUN apt-get install -y memcached
```
这个命令就会产生一个带有 memcached的镜像。
##docker build
如果你准备好了你的Dockerfile ready. 就可以用docker build 命令来产生你的镜像。
有几种方法来这么做
1. 从当前目录`docker build .`
2. 从标准输入`docker build - < Dockerfile`
3. 从github`docker build github.com/creack/docker-firefox`
最后一个例子中,docker 会clone github 的仓库 把他作为上下文。仓库根目录下的Dockerfile会被用来创建镜像。
···
root@precise64:/home/vagrant/dockerfiles# cat Dockerfile
FROM ubuntu
RUN apt-get install -y memcached
root@precise64:/home/vagrant/dockerfiles# docker build .
Uploading context 20480 bytes
Step 1 : FROM ubuntu
Pulling repository ubuntu
Pulling image 8dbd9e392a964056420e5d58ca5cc376ef18e2de93b5cc90e868a1bbc8318c1c (precise) from ubuntu
Pulling image b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc (quantal) from ubuntu
Pulling b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc metadata
Pulling b750fe79269d2ec9a3c593ef05b4332b1d1a02a62b4accb2c21d589ff2f5f2dc fs layer
Downloading 10.24 kB/10.24 kB (100%)
Pulling 27cf784147099545 metadata
Pulling 27cf784147099545 fs layer
Downloading 10.24 kB/10.24 kB (100%)
Pulling 27cf784147099545 metadata
Pulling 27cf784147099545 fs layer
Downloading 94.86 MB/94.86 MB (100%)
---> 8dbd9e392a96
Step 2 : RUN apt-get install -y memcached
---> Running in dedc7af62d9f
Reading package lists...
Building dependency tree...
The following extra packages will be installed:
libclass-isa-perl libevent-2.0-5 libgdbm3 libswitch-perl netbase perl
perl-modules
Suggested packages:
libcache-memcached-perl libmemcached perl-doc libterm-readline-gnu-perl
libterm-readline-perl-perl make libpod-plainer-perl
The following NEW packages will be installed:
libclass-isa-perl libevent-2.0-5 libgdbm3 libswitch-perl memcached netbase
perl perl-modules
0 upgraded, 8 newly installed, 0 to remove and 0 not upgraded.
Need to get 8070 kB of archives.
After this operation, 32.8 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ precise/main libgdbm3 amd64 1.8.3-10 [35.3 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ precise/main netbase all 4.47ubuntu1 [15.0 kB]
Get:3 http://archive.ubuntu.com/ubuntu/ precise/main libclass-isa-perl all 0.36-3 [11.9 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ precise/main libevent-2.0-5 amd64 2.0.16-stable-1 [127 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ precise/main perl-modules all 5.14.2-6ubuntu2 [3369 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ precise/main perl amd64 5.14.2-6ubuntu2 [4417 kB]
Get:7 http://archive.ubuntu.com/ubuntu/ precise/main libswitch-perl all 2.16-2 [19.2 kB]
Get:8 http://archive.ubuntu.com/ubuntu/ precise/main memcached amd64 1.4.13-0ubuntu2 [75.3 kB]
debconf: delaying package configuration, since apt-utils is not installed
Fetched 8070 kB in 7s (1136 kB/s)
Selecting previously unselected package libgdbm3.
(Reading database ... 7545 files and directories currently installed.)
Unpacking libgdbm3 (from .../libgdbm3_1.8.3-10_amd64.deb) ...
Selecting previously unselected package netbase.
Unpacking netbase (from .../netbase_4.47ubuntu1_all.deb) ...
Selecting previously unselected package libclass-isa-perl.
Unpacking libclass-isa-perl (from .../libclass-isa-perl_0.36-3_all.deb) ...
Selecting previously unselected package libevent-2.0-5.
Unpacking libevent-2.0-5 (from .../libevent-2.0-5_2.0.16-stable-1_amd64.deb) ...
Selecting previously unselected package perl-modules.
Unpacking perl-modules (from .../perl-modules_5.14.2-6ubuntu2_all.deb) ...
Selecting previously unselected package perl.
Unpacking perl (from .../perl_5.14.2-6ubuntu2_amd64.deb) ...
Selecting previously unselected package libswitch-perl.
Unpacking libswitch-perl (from .../libswitch-perl_2.16-2_all.deb) ...
Selecting previously unselected package memcached.
Unpacking memcached (from .../memcached_1.4.13-0ubuntu2_amd64.deb) ...
Setting up libgdbm3 (1.8.3-10) ...
Setting up netbase (4.47ubuntu1) ...
Setting up libclass-isa-perl (0.36-3) ...
Setting up libevent-2.0-5 (2.0.16-stable-1) ...
Setting up perl-modules (5.14.2-6ubuntu2) ...
Setting up perl (5.14.2-6ubuntu2) ...
update-alternatives: using /usr/bin/prename to provide /usr/bin/rename (rename) in auto mode.
Setting up memcached (1.4.13-0ubuntu2) ...
Starting memcached: memcached.
Setting up libswitch-perl (2.16-2) ...
Processing triggers for libc-bin ...
ldconfig deferred processing now taking place
---> 1dcfa24c8ca6
Successfully built 1dcfa24c8ca6
···
```
root@precise64:/home/vagrant/dockerfiles# docker images
REPOSITORY TAG ID CREATED SIZE
ubuntu 12.04 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
ubuntu 12.10 b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
ubuntu latest 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
ubuntu precise 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
ubuntu quantal b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
<none> <none> 1275893fed44 9 minutes ago 12.3 kB (virtual 566.4 MB)
<none> <none> 1dcfa24c8ca6 About a minute ago 52.27 MB (virtual 183.8 MB)
```
会发现docker 没有名字,这个时候可以用tag命令命名
···
root@precise64:/home/vagrant/dockerfiles# docker tag 1dcfa24c8ca6 memcached
root@precise64:/home/vagrant/dockerfiles# docker images
REPOSITORY TAG ID CREATED SIZE
ubuntu 12.04 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
ubuntu 12.10 b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
ubuntu latest 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
ubuntu precise 8dbd9e392a96 4 months ago 131.5 MB (virtual 131.5 MB)
ubuntu quantal b750fe79269d 4 months ago 24.65 kB (virtual 180.1 MB)
memcached latest 1dcfa24c8ca6 4 minutes ago 52.27 MB (virtual 183.8 MB)
<none> <none> 1275893fed44 12 minutes ago 12.3 kB (virtual 566.4 MB)
···
当然还可以更简洁,在docker build的时候 带上 -t tag 表示生成名字为tag的镜像。
例如 `docker build -t memcached`
如果我们希望我们的ubuntu包是最新的那么就需要运行
```
echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
apt-get update
```
在dockerFile里面可以写成
```
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
```
最后我们的dockerfile就是这样的了。
```
FROM ubuntu
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
RUN apt-get install -y memcached
```
代码总是需要注释的。所以也适用于Dockerfile.注释以*#*开头。
可以用来标记dockerfile的版本,命令的目的等。所以把上面的命令带上注释的话就变成。
```
# Memcached
#
# VERSION 1.0
# use the ubuntu base image provided by dotCloud
FROM ubuntu
# make sure the package repository is up to date
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
# install memcached
RUN apt-get install -y memcached
```
####MAINTAINER 命令
MAINTAINER 标记维护者的信息,例如
`MAINTAINER Guillaume J. Charmes, guillaume@dotcloud.com`
最终dockerfile的版本就是
```
# Memcached
#
# VERSION 1.0
# use the ubuntu base image provided by dotCloud
FROM ubuntu
MAINTAINER Guillaume J. Charmes, guillaume@dotcloud.com
# make sure the package repository is up to date
RUN echo "deb http://archive.ubuntu.com/ubuntu precise main universe" > /etc/apt/sources.list
RUN apt-get update
# install memcached
RUN apt-get install -y memcached
```