使用Docker搭建http服务器一台。
要求搭建完成后映射docker80端口。httpd页面显示自己的名字和IP地址
CentOS 7 下Docker的安装
Docker简介
Docker 是一个开源的应用容器引擎,让开发者可以打包他们的应用以及依赖包到一个可移植的容器中,然后发布到任何流行的 Linux 机器上,也可以实现虚拟化。容器是完全使用沙箱机制,相互之间不会有任何接口。Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器。Docker 容器通过 Docker 镜像来创建。容器与镜像的关系类似于面向对象编程中的对象与类。官网地址:http://www.docker.com/docker所需环境准备
系统要求CentOS 7.X 以上版本,内核至少3.10,64-bit
uname -r //查看内核
3.10.0-327.el7.x86_64
yum update //升级本地yum包
创建docker的yum库
# cd /etc/yum.repos.d/ && vi docker.repo
[dockerrepo]
name=DockerRepository
baseurl=https://yum.dockerproject.org/repo/main/centos/7
enabled=1
gpgcheck=1
gpgkey=https://yum.dockerproject.org/gpg
安装docker
yum install docker-io
启动docker服务并设置开机自动启动
systemctl start docker.service
systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
验证docker是否安装成功
docker info
Containers: 0
Running: 0
Paused: 0
Stopped: 0
Images: 0
Server Version: 1.10.3
Storage Driver: devicemapper
Pool Name: docker-253:0-34868263-pool
Pool Blocksize: 65.54 kB
Base Device Size: 10.74 GB
Backing Filesystem: xfs
Data file: /dev/loop0
Metadata file: /dev/loop1
Data Space Used: 11.8 MB
Data Space Total: 107.4 GB
Data Space Available: 26.96 GB
Metadata Space Used: 581.6 kB
Metadata Space Total: 2.147 GB
Metadata Space Available: 2.147 GB
Udev Sync Supported: true
Deferred Removal Enabled: false
Deferred Deletion Enabled: false
Deferred Deleted Device Count: 0
Data loop file: /var/lib/docker/devicemapper/devicemapper/data
WARNING: Usage of loopback devices is strongly discouraged for production use. Either use `--storage-opt dm.thinpooldev` or use `--storage-opt dm.no_warn_on_loop_devices=true` to suppress this warning.
Metadata loop file: /var/lib/docker/devicemapper/devicemapper/metadata
Library Version: 1.02.135-RHEL7 (2016-09-28)
Execution Driver: native-0.2
Logging Driver: journald
Plugins:
Volume: local
Network: host bridge null
Kernel Version: 3.10.0-327.el7.x86_64
Operating System: CentOS Linux 7 (Core)
OSType: linux
Architecture: x86_64
Number of Docker Hooks: 2
CPUs: 2
Total Memory: 3.86 GiB
Name: localhost.localdomain
ID: WZJQ:VTFD:LVRA:SSU3:MFNC:75V3:JEZT:KTVR:HF2G:WXR5:34AR:N2N5
WARNING: bridge-nf-call-iptables is disabled
WARNING: bridge-nf-call-ip6tables is disabled
Registries: docker.io (secure)
查看安装后的docker版本
docker version
Client:
Version: 1.10.3
API version: 1.22
Package version: docker-common-1.10.3-59.el7.centos.x86_64
Go version: go1.6.3
Git commit: 3999ccb-unsupported
Built: Thu Dec 15 17:24:43 2016
OS/Arch: linux/amd64
Server:
Version: 1.10.3
API version: 1.22
Package version: docker-common-1.10.3-59.el7.centos.x86_64
Go version: go1.6.3
Git commit: 3999ccb-unsupported
Built: Thu Dec 15 17:24:43 2016
OS/Arch: linux/amd64
3.编写Dockerfile
Dockfile是一种被Docker程序解释的脚本,Dockerfile由一条一条的指令组成,每条指令对应Linux下面的一条命令。Docker程序将这些Dockerfile指令翻译真正的Linux命令。Dockerfile有自己书写格式和支持的命令,Docker程序解决这些命令间的依赖关系,类似于Makefile。Docker程序将读取Dockerfile,根据指令生成定制的image。相比image这种黑盒子,Dockerfile这种显而易见的脚本更容易被使用者接受,它明确的表明image是怎么产生的。有了Dockerfile,当我们需要定制自己额外的需求时,只需在Dockerfile上添加或者修改指令,重新生成image即可,省去了敲命令的麻烦。
3.1 Docker加速器
之所以提到这个是因为,在build镜像的过程中需要从服务器pull images,但是不幸的的Docker官网已经被墙,所以需要借助国内第三方,我使用的阿里云的。
安装或升级Docker
您可以通过阿里云的镜像仓库下载: mirrors.aliyun.com/help/docker-engine
curl -sSL http://acs-public-mirror.oss-cn-hangzhou.aliyuncs.com/docker-engine/internet | sh -
配置Docker加速器
您可以使用如下的脚本将mirror的配置添加到docker daemon的启动参数中。
系统要求 CentOS 7 以上,Docker 1.9 以上。
sudo sed -i "s|ExecStart=/usr/bin/docker daemon|ExecStart=/usr/bin/docker daemon --registry-mirror=https://g9d5s6dr.mirror.aliyuncs.com|g"
sudo systemctl daemon-reload
sudo service docker restart
Dockerfile文件内容如下:
FROM centos:centos6
MAINTAINER liuqiang <402588603@qq.com>
RUN yum -y update; yum clean all
RUN yum -y install httpd && yum clean all
RUN touch /var/www/html/index.html
RUN echo "I have finished, pls give me five!!!! --liuqiang " >>
/var/www/html/index.html
EXPOSE 80
#Simple startup script to avoid some issues observed with container restart
ADD run-httpd.sh /run-httpd.sh //注意脚本目录
RUN chmod -v +x /run-httpd.sh //赋予脚本执行权限
CMD ["/run-httpd.sh"]
run-httpd.sh脚本内容如下
cat >> run-httpd.sh << EOF
#!/bin/bash
# Make sure we're not confused by old, incompletely-shutdown httpd
# context after restarting the container. httpd won't start correctly
# if it thinks it is already running.
rm -rf /run/httpd/*
exec /usr/sbin/apachectl -D FOREGROUND
EOF
注意:Dockerfile需借助脚本来重启httpd服务,并且最好放置于同一目录
docker build -t liuqiang/httpd:centos6 .
flag provided but not defined: -it
See '/usr/bin/docker-current --help'.
docker build -t liuqiang/httpd .
Sending build context to Docker daemon 5.12 kB
Step 1 : FROM centos:centos6
---> 8315978ceaaa
Step 2 : MAINTAINER liuqiang <402588603@qq.com>
---> Using cache
---> 658aeb6d114a
Step 3 : RUN yum -y update; yum clean all
---> Using cache
---> 8f1c933b2c3c
Step 4 : RUN yum -y install httpd && yum clean all
---> Using cache
---> 98ee768720c7
Step 5 : RUN touch /var/www/html/index.html
---> Using cache
---> b69a69656752
Step 6 : RUN echo "I have finished, pls give me five!!!! --liuqiang" >> /var/www/html/index.html
---> Using cache
---> b4c9fd4843fb
Step 7 : EXPOSE 80
---> Using cache
---> e4b5491564c2
Step 8 : ADD run-httpd.sh /run-httpd.sh
---> Using cache
---> ed7ec84907d1
Step 9 : RUN chmod -v +x /run-httpd.sh
---> Using cache
---> 081c6a101c16
Step 10 : CMD /run-httpd.sh
---> Using cache
---> 15eaa6d6d407
Successfully built 15eaa6d6d407
此时通过Dockerfile创建的镜像已经创建成功,并无报错
接下来运行容器
docker run -it -p 80:80 liuqiang/httpd:centos6
docker: Error response from daemon: failed to create endpoint clever_keller on network bridge: Error starting userland proxy: listen tcp 0.0.0.0:80: bind: address already in use.
启动
docker run -d -p 80:80 liuqiang/httpd:centos6
0582508431eb5713c1401843e68cb36b51de3bdd23dfa468cbe7496d71091e25
curl http://localhost
I have finished, pls give me five!!!! --liuqiang //curl测试成功
浏览器测试确认下
那些年 弹指间逝去的青春