通过将Nginx Web服务器安装到容器上,构建一个简单的网站,实现Docker作为本地Web开发环境。
步骤如下:
1)创建Dockerfile文件,首先创建一个目录sample,保存Dockerfile。
[root@l-client ~]# mkdir sample
[root@l-client ~]# cd sample
[root@l-client sample]# touch Dockerfile
[root@l-client sample]#
2)配置Nginx文件,所在的目录创建一个名为nginx的目录,用来存放这些配置文件。然后从GitHub上下载实例文件。
[root@l-client sample]# mkdir nginx &&cd nginx
[root@l-client nginx]# wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/global.conf
下载成功后,展示一下global.conf内容如下:
server {
listen 0.0.0.0:80;
server_name _;
root /var/www/html/website;
index index.htmlindex.htm;
access_log /var/log/nginx/default_access.log;
error_log /var/log/nginx/default_error.log;
}
[root@l-client nginx]# wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/nginx/nginx.conf
下载成功后,展示一下nginx.conf内容如下:
user www-data;
worker_processes 4;
pid /run/nginx.pid;
daemon off;
events { }
http {
sendfileon;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include/etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
error_log/var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
include/etc/nginx/conf.d/*.conf;
}
3)为sample网站编写Dockerfile。
[root@l-client sample]# vi Dockerfile
内容如下:
FROM ubuntu:14.04.3
MAINTAINER James Turnbull"[email protected]"
NV REFRESHED_AT 2016-12-25
RUN sudo apt-get -yqq install nginx
RUN mkdir -p /var/www/html/website
ADD nginx/global.conf /etc/nginx/conf.d/
ADD nginx/nginx.conf /etc/nginx/nginx.conf
EXPOSE 80
这个简单的Dockerfile内容包括以下几项:
(1)安装Nginx;
(2)在容器中创建一个目录/var/www/html/website/;
(3)将来自的本地文件的Nginx配置文件添加到镜像中;
(4)公开镜像的80端口。
(5)这些Nginx配置文件是为了运行sample网站而配置的。将文件nginx/global.conf用ADD指令复制到/etc/nginx/conf.d/目录中。
4)构建新的Nginx镜像。
[root@l-client sample]# docker build -tjamtur01/nginx .
5)展示Nginx镜像的构建历史。
[root@l-client sample]# docker historyjamtur01/nginx
IMAGE CREATED CREATED BY SIZE COMMENT
25a2b32d2702 About a minute ago /bin/sh -c #(nop)EXPOSE 80/tcp 0 B
705b78ae3e44 About a minute ago /bin/sh -c #(nop)ADD file:d6698a182fafaf3cb0 415 B
0c0086998e24 About a minute ago /bin/sh -c #(nop)ADD file:9778ae1b43896011cc 286 B
a0851da17b8f 2 minutes ago /bin/sh -c sudo mkdir -p/var/www/html/websit 0 B
06efd6b163ac 2 minutes ago /bin/sh -c sudo apt-get -yqq installnginx 18.13 MB
70e78a7f4121 35 minutes ago /bin/sh -c #(nop)ENV REFRESHED_AT=2016-12-25 0 B
233522435d2c 40 minutes ago /bin/sh -c #(nop)MAINTAINER James Turnbull " 0 B
ebdc8e295a2e 11 months ago /bin/sh -c #(nop)CMD ["/bin/bash"] 0 B
6)下载sample网站测试网页。
[root@l-client sample]# mkdir website &&cd website
[root@l-clientwebsite]# wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/sample/website/index.html
测试网页内容如下:
7)运行第一个Nginx Web站点容器,并做数据卷映射。
[root@l-client sample]# docker run -d -p 80--name website -v $PWD/website:/var/www/html/website jamtur01/nginx nginx
c0bded9df5549010ad12ea8eb3d1dc0e92f12087e227ba3e82b15dee2c173b13
8)查看网站容器。
[root@l-client sample]# docker ps -l
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c0bded9df554 jamtur01/nginx "nginx" 8 minutes ago Up 8 minutes 0.0.0.0:32768->80/tcp website
9)浏览sample网站,测试容器运行是否正确。运行结果如下:
图7.1浏览sample网站
10)修改网站实例网页。
查看网页内容:
[root@l-client sample]# vi$PWD/website/index.html
修改为:
11)重新刷新网站,查看网页内容变化。
图7.2 修改后sample网站
通过Docker容器搭建持续集成软件平台Jenkins,为研发构建开发测试持续集成的环境。
1)创建一个目录jenkins,保存相关的配置信息和内容。
[root@l-client sinatra]# cd
[root@l-client ~]# mkdir jenkins
[root@l-client ~]# cd jenkins/
2)定义jenkins的Dockerfile。
[root@l-client jenkins]# vi Dockerfile
FROM ubuntu:14.04.3
MAINTAINER [email protected]
ENV REFRESHED_AT 2016-12-25
RUN apt-get update -qq && apt-getinstall -qqy curl apt-transport-https
RUN apt-key adv --keyserverhkp://p80.pool.sks-keyservers.net:0 --recv-keys
58118E89F3A912897C070ADBF76221572C52609D
RUN echo deb https://apt.dockerproject.org/repoubuntu
trusty main >/etc/apt/sources.list.d/docker.list
RUN apt-get update -qq && apt-getinstall -qqy iptables ca-certificates openjdk-7-jdk git-core docker.io
ENV JENKINS_HOME /opt/jenkins/data
ENV JENKINS_MIRROR http://mirrors.jenkins-ci.org
RUN mkdir -p $JENKINS_HOME/plugins
RUN curl -f -o /opt/jenkins/jenkins.war -L$JENKINS_MIRROR/war-stable/latest/jenkins.war
RUN for plugin in chucknorris greenballs scm-apigit-client git ws-cleanup; do curl -sf -o $JENKINS_HOME/plugins/${plugin}.hpi-L $JENKINS_MIRROR/plugins/${plugin}/latest/${plugin}.hpi; done
ADD ./dockerjenkins.sh/usr/local/bin/dockerjenkins.sh
RUN chmod +x /usr/local/bin/dockerjenkins.sh
VOLUME /var/lib/docker
EXPOSE 8080
ENTRYPOINT ["/usr/local/bin/dockerjenkins.sh" ]
Dockerfile说明:
(1)jenkins的dockerfile继承了ubuntu:14.0.4.3镜像。
(2)apt-key命令用于管理Debian Linux系统中的软件包密钥。每个发布的deb包,都是通过密钥认证的,apt-key用来管理密钥。
(3)安装源加入需要的docker apt仓库,并安装执行docker和jenkins所需要的包。
(4)创建/opt/jenkins目录,下载jenkins.war到这个目录,并下载jenkins插件。
(5)使用ENV指令把JENKINS_HOME和JENKINS_MIRROR环境变量设置为jenkins的数据目录和镜像站点。
(6)使用VOLUME指令指定/var/lib/docker数据卷,运行容器内部使用宿主机的文件系统作为容器的存储。这样容器的/var/lib/docker目录将保存在宿主机系统的/var/lib/docker/volumes目录下。
(7)jenkins默认对外的8080端口。
(8)最后指定要运行的shell脚本,作为容器的启动命令。这个shell脚本帮助在宿主机上配置docker,允许在docker容器里运行docker,开启docker守护进程,并且启动jenkins。
3)获取dockerjenkins.sh脚本
[root@l-clientjenkins]#wget https://raw.githubusercontent.com/jamtur01/dockerbook-code/master/code/5/jenkins/dockerjenkins.sh
[root@l-client jenkins]# chmod 0755dockerjenkins.sh
4)构建docker-jnekins镜像
[root@l-client jenkins]#docker build -tjamtur01/dockerjenkins
349da36a6408a03887eea50b37ffbc6ff0699028715994221f71e8287df96015.
5)运行docker-jenkins镜像
[oot@l-client jenkins]#docker run -p 8080:8080--name jenkins --privileged -djamtur01/dockerjenkins
6)检查dockerjenkins容器的日志
[root@l-client jenkins]# docker logs jenkins
/proc/self/fd /
/
docker: 'daemon' is not a docker command. See'docker --help'.
Running from: /opt/jenkins/jenkins.war
webroot: EnvVars.masterEnvVars.get("JENKINS_HOME")
Dec 28, 2016 6:07:07 AM MaindeleteWinstoneTempContents
WARNING: Failed to delete the temporary Winstonefile /tmp/winstone/jenkins.war
Dec 28, 2016 6:07:10 AM org.eclipse.jetty.util.log.JavaUtilLoginfo
INFO: Logging initialized @4602ms
Dec 28, 2016 6:07:10 AM winstone.LoggerlogInternal
INFO: Beginning extraction from war file
7)检查jenkins的启动和执行
Dec 28, 2016 6:09:10 AM hudson.WebAppMain$3 run
INFO: Jenkins is fully up and running
图7.3 浏览jenkins网站
本任务测试把Dokcer作为应用服务器和编译管道来运行,模拟研发部门一键快速部署Tomcat Web应用,提供给测试部门进行测试。
构建2个镜像:
第一个镜像负责下载WAR文件,并把war包保存在数据卷上;
第二个镜像负责负责运行Tomcat,启动保持在共享数据卷上的war包。
1)负责下载WAR文件的镜像创建步骤:
(1)创建保存目录和Dockerfile。
[root@l-client redis]# cd
[root@l-client ~]# mkdir fetcher
[root@l-client ~]# cd fetcher/
[root@l-client fetcher]# touch Dockerfile
(2)通过wget获取WAR文件,具体下载地址由用户决定。
[root@l-client fetcher]# vi Dockerfile
FROM ubuntu:14.04.3
MAINTAINER James Turnbull "[email protected]"
ENV REFRESHED_AT 2016-12-25
RUN apt-get -y update
RUN apt-get -y install wget
VOLUME [ "/var/lib/tomcat7/webapps/" ]
WORKDIR /var/lib/tomcat7/webapps/
ENTRYPOINT [ "wget" ]
CMD [ "-?" ]
当执行容器时,指定URL下载war包,下载保存在/var/lib/tomcat7/webapps/ 目录下。此目录也是一个卷和所有容器的工作目录。然后把这个卷共享给运行tomcat服务的容器。
(3)构建镜像
[root@l-client fetcher]# docker build -tjamtur01/fetcher .
Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:14.04.3
--->ebdc8e295a2e
Step 2 : MAINTAINER James Turnbull"[email protected]"
--->Using cache
--->233522435d2c
Step 3 : ENV REFRESHED_AT 2016-12-25
--->Using cache
--->70e78a7f4121
Step 4 : RUN apt-get -y update
--->Running in bda317f06643
---> f35531a71f08
Removing intermediate container bda317f06643
Step 5 : RUN apt-get -y install wget
--->Running in 113c57bc332c
---> 0771b0536da9
Removing intermediate container 113c57bc332c
Step 6 : VOLUME /var/lib/tomcat7/webapps/
--->Running in d3d9221f691c
--->4a5614f493ee
Removing intermediate container d3d9221f691c
Step 7 : WORKDIR /var/lib/tomcat7/webapps/
--->Running in 783910b7770c
--->db08b2409136
Removing intermediate container 783910b7770c
Step 8 : ENTRYPOINT wget
--->Running in 1cd35e6d9301
--->3b3307763663
Removing intermediate container 1cd35e6d9301
Step 9 : CMD -?
--->Running in ae8f6fb31831
--->c74a890eddd5
Removing intermediate container ae8f6fb31831
Successfully built c74a890eddd5
(4)运行容器,并指定URL下载war包
[root@l-clientfetcher]# docker run -t -i --name sample jamtur01/fetcherhttps://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.warhttps://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war
--2016-12-29 07:47:29-- https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war
Resolving tomcat.apache.org (tomcat.apache.org)... 88.198.26.2,140.211.11.105,
2a01:4f8:130:2192::2
Connecting to tomcat.apache.org (tomcat.apache.org)|88.198.26.2|:443...connected.
HTTP request sent, awaiting response... 200 OK
Length: 4606 (4.5K)
Saving to: 'sample.war'
100%[======================>] 4,606 --.-K/s in 0s
2016-12-29 07:47:34 (103 MB/s)- 'sample.war'saved
[4606/4606]
--2016-12-29 07:47:34-- https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war
Reusing existing connection totomcat.apache.org:443.
HTTP request sent, awaiting response... 200 OK
Length: 4606 (4.5K)
Saving to: 'sample.war.1'
100%[===================>] 4,606 3.64KB/s in 1.2s
2016-12-29 07:47:36 (3.64 KB/s)- 'sample.war.1'saved [4606/4606]
FINISHED --2016-12-29 07:47:36--
Total wall clock time: 6.4s
Downloaded: 2 files, 9.0K in 1.2s (7.29 KB/s)
容器运行,指定下载URL地址为:https://tomcat.apache.org/tomcat-7.0-doc/appdev/sample/sample.war,下载目录为容器的工作目录:var/lib/tomcat7/webapps/。
(5)通过docker inspect命令查找卷的存储位置
root@l-client fetcher]# docker inspect -f"{{ range .Mounts }} {{.}} {{end}}" sample
{dc4127683406b0334ec83c37c356b357fbefdf1608cb916491a6a7b091a71b53/var/lib/docker/volumes/dc4127683406b0334ec83c37c356b357fbefdf1608cb916491a6a7b091a71b53/_data/var/lib/tomcat7/webapps local true }
(6)可以通过ls查看卷所在的目录
[root@l-client fetcher]# ls -l/var/lib/docker/volumes/
2)负责运行Tomat和web应用的镜像创建步骤:
(1)创建tomcat7 dockerfile
[root@l-client fetcher]#mkdir tomcat7
[root@l-client fetcher]#cd tomcat7/
[root@l-client tomcat7]#vi Dockerfile
FROM ubuntu:14.04.3
MAINTAINER James Turnbull [email protected]
ENV REFRESHED_AT 2016-12-25
RUN apt-get -y update
RUN apt-get -y install tomcat7 default-jdk
ENV CATALINA_HOME /usr/share/tomcat7
ENV CATALINA_BASE /var/lib/tomcat7
ENV CATALINA_PID /var/run/tomcat7.pid
ENV CATALINA_SH/usr/share/tomcat7/bin/catalina.sh
ENV CATALINA_TMPDIR /tmp/tomcat7-tomcat7-tmp
RUN mkdir -p $CATALINA_TMPDIR
VOLUME [ "/var/lib/tomcat7/webapps/" ]
EXPOSE 8080
ENTRYPOINT ["/usr/share/tomcat7/bin/catalina.sh","run" ]
这个镜像很简单,首先安装java jdk和tomcat服务器,接着设置环境变量,然后创建了/var/lib/tomcat7/webapps/卷,公开tomcat默认的8080端口,最后ENTRYPOINT指定来启动tomcat的命令。
(2)构建tomcat7镜像
[root@l-client tomcat7]# docker build -tjamtur01/tomcat7 .
….
…. done.
Processing triggers for sgml-base(1.26+nmu4ubuntu1)...
--->6bb373b73ddc
Removing intermediate container 6a87159cda44
Step 6 : ENV CATALINA_HOME /usr/share/tomcat7
--->Running in dda79e0e5eca
--->6f847ea986b3
Removing intermediate container dda79e0e5eca
Step 7 : ENV CATALINA_BASE /var/lib/tomcat7
--->Running in e05301f878dc
--->297d325d22ee
Removing intermediate container e05301f878dc
Step 8 : ENV CATALINA_PID /var/run/tomcat7.pid
--->Running in 64bf2b71eb25
--->1cf2c783758d
Removing intermediate container 64bf2b71eb25
Step 9 : ENV CATALINA_SH/usr/share/tomcat7/bin/catalina.sh
--->Running in 3bf3f9cff57f
--->3b766d1471e1
Removing intermediate container 3bf3f9cff57f
Step 10 : ENV CATALINA_TMPDIR/tmp/tomcat7-tomcat7-tmp
--->Running in c25a6b890171
--->96b9faaae6f5
Removing intermediate container c25a6b890171
Step 11 : RUN mkdir -p $CATALINA_TMPDIR
--->Running in b9d820b71657
--->0c5ff83b4fab
Removing intermediate container b9d820b71657
Step 12 : VOLUME /var/lib/tomcat7/webapps/
--->Running in b0b7cdc08643
--->2d5779167f81
Removing intermediate container b0b7cdc08643
Step 13 : EXPOSE 8080
--->Running in 9a9fdeaa5f02
--->df0202997162
Removing intermediate container 9a9fdeaa5f02
Step 14 : ENTRYPOINT/usr/share/tomcat7/bin/catalina.sh run
--->Running in cd1dd633cc90
--->6b15a1f76a5f
Removing intermediate container cd1dd633cc90
Successfully built 6b15a1f76a5f
(3)创建第一个tomcat实例
[root@l-client tomcat7]# docker run --namesample_app --volumes-from sample -d -P jamtur01/tomcat7
c12eca5093ab403635ac44c9335a5954cedf63732f04ff016a6239bd0f04df39
(4)查找tomcat应用的端口
[root@l-client tomcat7]# docker port sample_app8080
0.0.0.0:32798
(5)进入网站查看运行情况。
图7.4 浏览tomcat网站