标签:docker
1.创建一个测试目录
[root@localhost ~]# mkdir /sample
[root@localhost ~]# cd /sample
2.创建Dockerfile文件
[root@localhost sample]# touch Dockerfile
3.编辑Dockerfile文件
[root@localhost sample]# vim Dockerfile
FROM ubuntu:14.04
MAINTAINER ovcer clvn2008@qq.com
ENV REFRESHED_AT 2014-12-28
RUN apt-get update
RUN apt-get -y -q install nginx
RUN mkdir -p /var/www/html
ADD nginx/global.conf /etc/nginx/conf.d/
ADD nginx/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
4.创建nginx目录
[root@localhost sample]# mkdir nginx
[root@localhost sample]# cd nginx
5.下载global.conf配置文件
[root@localhost nginx]# wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/global.conf
--2014-12-29 00:06:21-- https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/global.conf
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 103.245.222.133
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|103.245.222.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:286 [text/plain]
正在保存至: “global.conf”
100%[======================================================================================>] 286 --.-K/s 用时 0s
2014-12-29 00:06:22 (20.1 MB/s) - 已保存 “global.conf” [286/286])
6.下载nginx.conf配置文件
[root@localhost nginx]# wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/nginx.conf
--2014-12-29 00:07:24-- https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/nginx.conf
正在解析主机 raw.githubusercontent.com (raw.githubusercontent.com)... 103.245.222.133
正在连接 raw.githubusercontent.com (raw.githubusercontent.com)|103.245.222.133|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:415 [text/plain]
正在保存至: “nginx.conf”
100%[======================================================================================>] 415 --.-K/s 用时 0s
2014-12-29 00:07:24 (12.9 MB/s) - 已保存 “nginx.conf” [415/415])
7.创建一个新的镜像
[root@localhost sample]# docker build -t ovcer/nginx .
Sending build context to Docker daemon 5.12 kB
Sending build context to Docker daemon
Step 0 : FROM ubuntu:14.04
Pulling repository ubuntu
ed5a78b7b42b: Download complete
ed5a78b7b42b: Pulling image (14.04) from ubuntu
fe95bf7d5f50: Download complete
9a4594fe74ea: Download complete
8c4b1edcceea: Download complete
Status: Downloaded newer image for ubuntu:14.04
---> ed5a78b7b42b
Step 1 : MAINTAINER ovcer clvn2008@qq.com
---> Running in d10273b5c305
---> a07557633931
Removing intermediate container d10273b5c305
Step 2 : ENV REFRESHED_AT 2014-12-28
---> Running in 28e47fed987f
---> 4a05ec370ded
Removing intermediate container 28e47fed987f
Step 3 : RUN apt-get update
---> Running in 4da1284c5c50
Ign http://archive.ubuntu.com trusty InRelease
Ign http://archive.ubuntu.com trusty-updates InRelease
Ign http://archive.ubuntu.com trusty-security InRelease
Hit http://archive.ubuntu.com trusty Release.gpg
Get:1 http://archive.ubuntu.com trusty-updates Release.gpg [933 B]
Get:2 http://archive.ubuntu.com trusty-security Release.gpg [933 B]
Hit http://archive.ubuntu.com trusty Release
Get:3 http://archive.ubuntu.com trusty-updates Release [62.0 kB]
Get:4 http://archive.ubuntu.com trusty-security Release [62.0 kB]
Get:5 http://archive.ubuntu.com trusty/main Sources [1335 kB]
Get:6 http://archive.ubuntu.com trusty/restricted Sources [5335 B]
Get:7 http://archive.ubuntu.com trusty/universe Sources [7926 kB]
Get:8 http://archive.ubuntu.com trusty/main amd64 Packages [1743 kB]
Get:9 http://archive.ubuntu.com trusty/restricted amd64 Packages [16.0 kB]
Get:10 http://archive.ubuntu.com trusty/universe amd64 Packages [7589 kB]
Get:11 http://archive.ubuntu.com trusty-updates/main Sources [193 kB]
Get:12 http://archive.ubuntu.com trusty-updates/restricted Sources [1874 B]
Get:13 http://archive.ubuntu.com trusty-updates/universe Sources [119 kB]
Get:14 http://archive.ubuntu.com trusty-updates/main amd64 Packages [493 kB]
Get:15 http://archive.ubuntu.com trusty-updates/restricted amd64 Packages [14.8 kB]
Get:16 http://archive.ubuntu.com trusty-updates/universe amd64 Packages [298 kB]
Get:17 http://archive.ubuntu.com trusty-security/main Sources [70.1 kB]
Get:18 http://archive.ubuntu.com trusty-security/restricted Sources [1874 B]
Get:19 http://archive.ubuntu.com trusty-security/universe Sources [19.1 kB]
Get:20 http://archive.ubuntu.com trusty-security/main amd64 Packages [229 kB]
Get:21 http://archive.ubuntu.com trusty-security/restricted amd64 Packages [14.8 kB]
Get:22 http://archive.ubuntu.com trusty-security/universe amd64 Packages [98.1 kB]
Fetched 20.3 MB in 9min 10s (36.9 kB/s)
Reading package lists...
---> 2deac99f698a
Removing intermediate container 4da1284c5c50
Step 4 : RUN apt-get -y -q install nginx
---> Running in 80c7ce3c5a63
Reading package lists...
Building dependency tree...
Reading state information...
The following extra packages will be installed:
fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
libxslt1.1 nginx-common nginx-core sgml-base xml-core
Suggested packages:
libgd-tools geoip-bin fcgiwrap nginx-doc sgml-base-doc debhelper
The following NEW packages will be installed:
fontconfig-config fonts-dejavu-core geoip-database libfontconfig1
libfreetype6 libgd3 libgeoip1 libjbig0 libjpeg-turbo8 libjpeg8 libtiff5
libvpx1 libx11-6 libx11-data libxau6 libxcb1 libxdmcp6 libxml2 libxpm4
libxslt1.1 nginx nginx-common nginx-core sgml-base xml-core
0 upgraded, 25 newly installed, 0 to remove and 0 not upgraded.
Need to get 5612 kB of archives.
After this operation, 19.8 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu/ trusty/main libgeoip1 amd64 1.6.0-1 [71.0 kB]
Get:2 http://archive.ubuntu.com/ubuntu/ trusty/main libxau6 amd64 1:1.0.8-1 [8376 B]
Get:3 http://archive.ubuntu.com/ubuntu/ trusty/main libxdmcp6 amd64 1:1.1.1-1 [12.8 kB]
Get:4 http://archive.ubuntu.com/ubuntu/ trusty/main libxcb1 amd64 1.10-2ubuntu1 [38.0 kB]
Get:5 http://archive.ubuntu.com/ubuntu/ trusty/main libx11-data all 2:1.6.2-1ubuntu2 [111 kB]
Get:6 http://archive.ubuntu.com/ubuntu/ trusty/main libx11-6 amd64 2:1.6.2-1ubuntu2 [560 kB]
Get:7 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libxml2 amd64 2.9.1+dfsg1-3ubuntu4.4 [570 kB]
Get:8 http://archive.ubuntu.com/ubuntu/ trusty/main sgml-base all 1.26+nmu4ubuntu1 [12.5 kB]
Get:9 http://archive.ubuntu.com/ubuntu/ trusty/main fonts-dejavu-core all 2.34-1ubuntu1 [1024 kB]
Get:10 http://archive.ubuntu.com/ubuntu/ trusty-updates/main fontconfig-config all 2.11.0-0ubuntu4.1 [47.4 kB]
Get:11 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libfreetype6 amd64 2.5.2-1ubuntu2.2 [305 kB]
Get:12 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libfontconfig1 amd64 2.11.0-0ubuntu4.1 [123 kB]
Get:13 http://archive.ubuntu.com/ubuntu/ trusty/main libjpeg-turbo8 amd64 1.3.0-0ubuntu2 [104 kB]
Get:14 http://archive.ubuntu.com/ubuntu/ trusty/main libjpeg8 amd64 8c-2ubuntu8 [2194 B]
Get:15 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libjbig0 amd64 2.0-2ubuntu4.1 [26.1 kB]
Get:16 http://archive.ubuntu.com/ubuntu/ trusty-updates/main libtiff5 amd64 4.0.3-7ubuntu0.1 [142 kB]
Get:17 http://archive.ubuntu.com/ubuntu/ trusty/main libvpx1 amd64 1.3.0-2 [556 kB]
Get:18 http://archive.ubuntu.com/ubuntu/ trusty/main libxpm4 amd64 1:3.5.10-1 [38.3 kB]
Get:19 http://archive.ubuntu.com/ubuntu/ trusty/main libgd3 amd64 2.1.0-3 [147 kB]
Get:20 http://archive.ubuntu.com/ubuntu/ trusty/main libxslt1.1 amd64 1.1.28-2build1 [145 kB]
Get:21 http://archive.ubuntu.com/ubuntu/ trusty/main geoip-database all 20140313-1 [1196 kB]
Get:22 http://archive.ubuntu.com/ubuntu/ trusty/main xml-core all 0.13+nmu2 [23.3 kB]
Get:23 http://archive.ubuntu.com/ubuntu/ trusty-updates/main nginx-common all 1.4.6-1ubuntu3.1 [17.9 kB]
Get:24 http://archive.ubuntu.com/ubuntu/ trusty-updates/main nginx-core amd64 1.4.6-1ubuntu3.1 [324 kB]
Get:25 http://archive.ubuntu.com/ubuntu/ trusty-updates/main nginx all 1.4.6-1ubuntu3.1 [5218 B]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
dpkg-preconfigure: unable to re-open stdin:
Fetched 5612 kB in 25s (216 kB/s)
Selecting previously unselected package libgeoip1:amd64.
(Reading database ... 11527 files and directories currently installed.)
Preparing to unpack .../libgeoip1_1.6.0-1_amd64.deb ...
Unpacking libgeoip1:amd64 (1.6.0-1) ...
Selecting previously unselected package libxau6:amd64.
Preparing to unpack .../libxau6_1%3a1.0.8-1_amd64.deb ...
Unpacking libxau6:amd64 (1:1.0.8-1) ...
Selecting previously unselected package libxdmcp6:amd64.
Preparing to unpack .../libxdmcp6_1%3a1.1.1-1_amd64.deb ...
Unpacking libxdmcp6:amd64 (1:1.1.1-1) ...
Selecting previously unselected package libxcb1:amd64.
Preparing to unpack .../libxcb1_1.10-2ubuntu1_amd64.deb ...
Unpacking libxcb1:amd64 (1.10-2ubuntu1) ...
Selecting previously unselected package libx11-data.
Preparing to unpack .../libx11-data_2%3a1.6.2-1ubuntu2_all.deb ...
Unpacking libx11-data (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libx11-6:amd64.
Preparing to unpack .../libx11-6_2%3a1.6.2-1ubuntu2_amd64.deb ...
Unpacking libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Selecting previously unselected package libxml2:amd64.
Preparing to unpack .../libxml2_2.9.1+dfsg1-3ubuntu4.4_amd64.deb ...
Unpacking libxml2:amd64 (2.9.1+dfsg1-3ubuntu4.4) ...
Selecting previously unselected package sgml-base.
Preparing to unpack .../sgml-base_1.26+nmu4ubuntu1_all.deb ...
Unpacking sgml-base (1.26+nmu4ubuntu1) ...
Selecting previously unselected package fonts-dejavu-core.
Preparing to unpack .../fonts-dejavu-core_2.34-1ubuntu1_all.deb ...
Unpacking fonts-dejavu-core (2.34-1ubuntu1) ...
Selecting previously unselected package fontconfig-config.
Preparing to unpack .../fontconfig-config_2.11.0-0ubuntu4.1_all.deb ...
Unpacking fontconfig-config (2.11.0-0ubuntu4.1) ...
Selecting previously unselected package libfreetype6:amd64.
Preparing to unpack .../libfreetype6_2.5.2-1ubuntu2.2_amd64.deb ...
Unpacking libfreetype6:amd64 (2.5.2-1ubuntu2.2) ...
Selecting previously unselected package libfontconfig1:amd64.
Preparing to unpack .../libfontconfig1_2.11.0-0ubuntu4.1_amd64.deb ...
Unpacking libfontconfig1:amd64 (2.11.0-0ubuntu4.1) ...
Selecting previously unselected package libjpeg-turbo8:amd64.
Preparing to unpack .../libjpeg-turbo8_1.3.0-0ubuntu2_amd64.deb ...
Unpacking libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Selecting previously unselected package libjpeg8:amd64.
Preparing to unpack .../libjpeg8_8c-2ubuntu8_amd64.deb ...
Unpacking libjpeg8:amd64 (8c-2ubuntu8) ...
Selecting previously unselected package libjbig0:amd64.
Preparing to unpack .../libjbig0_2.0-2ubuntu4.1_amd64.deb ...
Unpacking libjbig0:amd64 (2.0-2ubuntu4.1) ...
Selecting previously unselected package libtiff5:amd64.
Preparing to unpack .../libtiff5_4.0.3-7ubuntu0.1_amd64.deb ...
Unpacking libtiff5:amd64 (4.0.3-7ubuntu0.1) ...
Selecting previously unselected package libvpx1:amd64.
Preparing to unpack .../libvpx1_1.3.0-2_amd64.deb ...
Unpacking libvpx1:amd64 (1.3.0-2) ...
Selecting previously unselected package libxpm4:amd64.
Preparing to unpack .../libxpm4_1%3a3.5.10-1_amd64.deb ...
Unpacking libxpm4:amd64 (1:3.5.10-1) ...
Selecting previously unselected package libgd3:amd64.
Preparing to unpack .../libgd3_2.1.0-3_amd64.deb ...
Unpacking libgd3:amd64 (2.1.0-3) ...
Selecting previously unselected package libxslt1.1:amd64.
Preparing to unpack .../libxslt1.1_1.1.28-2build1_amd64.deb ...
Unpacking libxslt1.1:amd64 (1.1.28-2build1) ...
Selecting previously unselected package geoip-database.
Preparing to unpack .../geoip-database_20140313-1_all.deb ...
Unpacking geoip-database (20140313-1) ...
Selecting previously unselected package xml-core.
Preparing to unpack .../xml-core_0.13+nmu2_all.deb ...
Unpacking xml-core (0.13+nmu2) ...
Selecting previously unselected package nginx-common.
Preparing to unpack .../nginx-common_1.4.6-1ubuntu3.1_all.deb ...
Unpacking nginx-common (1.4.6-1ubuntu3.1) ...
Selecting previously unselected package nginx-core.
Preparing to unpack .../nginx-core_1.4.6-1ubuntu3.1_amd64.deb ...
Unpacking nginx-core (1.4.6-1ubuntu3.1) ...
Selecting previously unselected package nginx.
Preparing to unpack .../nginx_1.4.6-1ubuntu3.1_all.deb ...
Unpacking nginx (1.4.6-1ubuntu3.1) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up libgeoip1:amd64 (1.6.0-1) ...
Setting up libxau6:amd64 (1:1.0.8-1) ...
Setting up libxdmcp6:amd64 (1:1.1.1-1) ...
Setting up libxcb1:amd64 (1.10-2ubuntu1) ...
Setting up libx11-data (2:1.6.2-1ubuntu2) ...
Setting up libx11-6:amd64 (2:1.6.2-1ubuntu2) ...
Setting up libxml2:amd64 (2.9.1+dfsg1-3ubuntu4.4) ...
Setting up sgml-base (1.26+nmu4ubuntu1) ...
Setting up fonts-dejavu-core (2.34-1ubuntu1) ...
Setting up fontconfig-config (2.11.0-0ubuntu4.1) ...
Setting up libfreetype6:amd64 (2.5.2-1ubuntu2.2) ...
Setting up libfontconfig1:amd64 (2.11.0-0ubuntu4.1) ...
Setting up libjpeg-turbo8:amd64 (1.3.0-0ubuntu2) ...
Setting up libjpeg8:amd64 (8c-2ubuntu8) ...
Setting up libjbig0:amd64 (2.0-2ubuntu4.1) ...
Setting up libtiff5:amd64 (4.0.3-7ubuntu0.1) ...
Setting up libvpx1:amd64 (1.3.0-2) ...
Setting up libxpm4:amd64 (1:3.5.10-1) ...
Setting up libgd3:amd64 (2.1.0-3) ...
Setting up libxslt1.1:amd64 (1.1.28-2build1) ...
Setting up geoip-database (20140313-1) ...
Setting up xml-core (0.13+nmu2) ...
Setting up nginx-common (1.4.6-1ubuntu3.1) ...
Processing triggers for ureadahead (0.100.0-16) ...
Setting up nginx-core (1.4.6-1ubuntu3.1) ...
invoke-rc.d: policy-rc.d denied execution of start.
Setting up nginx (1.4.6-1ubuntu3.1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.4) ...
Processing triggers for sgml-base (1.26+nmu4ubuntu1) ...
---> fd74b1e8526f
Removing intermediate container 80c7ce3c5a63
Step 5 : RUN mkdir -p /var/www/html
---> Running in efffe2f5e4be
---> 453936efc1a0
Removing intermediate container efffe2f5e4be
Step 6 : ADD nginx/global.conf /etc/nginx/conf.d/
---> 22b0a3ad12f8
Removing intermediate container 076fe71258ba
Step 7 : ADD nginx/nginx.conf /etc/nginx/nginx.conf
---> a4480bfd21f6
Removing intermediate container bbbd280d0214
Step 8 : EXPOSE 80
---> Running in 11909906941c
---> c0fbdb246c6a
Removing intermediate container 11909906941c
Successfully built c0fbdb246c6a
8.创建一个网站测试页面
[root@localhost ~]# cd /sample
[root@localhost sample]# mkdir website
[root@localhost sample]# cd website
[root@localhost website]# echo "www.rhcau.com" > index.html
[root@localhost website]# cd ..
9.创建容器
[root@localhost sample]# docker run -d -p 80 --name website -v $PWD/website:/var/www/html/website ovcer/nginx nginx
03d08b269b7fc546e56945b5be6ccbc4ac9fe95287be1a919b2855b8f83c4c4f
10.查看容器是否运行
[root@localhost sample]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
03d08b269b7f ovcer/nginx:latest "nginx" 28 seconds ago Up 27 seconds 0.0.0.0:49153->80/tcp website
本文出自 “云人生” 博客,请务必保留此出处http://ovcer.blog.51cto.com/1145188/1596947
标签:docker
原文地址:http://ovcer.blog.51cto.com/1145188/1596947