环境: CentOS 7 Linux 3.10.0-1160.el7.x86_64
具体要求如下:
(1)基于centos基础镜像;
(2)指定作者信息;
(3)安装nginx服务,将提供的dest目录(提供默认主页index.html)传到镜像内,并将dest目录内的前端文件复制到nginx的工作目录;
(4)暴露80端口;
(5)设置服务自启动。
(6)验证镜像。
步骤
①创建项目目录
[root@node1 ~]# mkdir nginx
[root@node1 ~]# cd nginx
②创建html文件
[root@node1 ~]# cd nginx/
[root@node1 nginx]# cat index.html
<html>
<head>
<title> node1 docker nginx demo </title>
</head>
<body>
<h1> This is Nginx - node1 </h1>
</body>
</html>
③创建Dockerfile
Dockerfile包含了Docker的指令。在nginx目录下创建Dockerfile,文件名必须是Dockerfile。
[root@node1 nginx]# cat Dockerfile
FROM centos:7
MAINTAINER "mysql <mysql@163.com>"
RUN yum install -y gcc gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel wget\
&& useradd -r -s /sbin/nologin nginx \
&& yum clean allRUN wget http://nginx.org/download/nginx-1.16.1.tar.gz && tar -xf nginx-1.16.1.tar.gz -C /usr/local/src/
RUN cd /usr/local/src/nginx-1.16.1 \
&& ./configure --prefix=/apps/nginx \
&& make \
&& make install \
&& rm -rf /usr/local/src/nginx*
#COPY nginx.conf /apps/nginx/conf/nginx.confWORKDIR /usr/local/src/nginx
COPY index.html /apps/nginx/html/
RUN ln -s /apps/nginx/sbin/nginx /usr/sbin/nginx
EXPOSE 80
CMD ["/usr/sbin/nginx","-g","daemon off;"]
了解Dockerfile指令 : http://t.csdn.cn/hJf7A
④构建Docker 镜像
[root@node1 nginx]# docker build -t nginx:v1.1 .
……
Successfully built bc151e88c131
Successfully tagged nginx:v1.1
⑤登陆到镜像仓库
[root@node1 nginx]# docker login -u admin -p Harbor12345 192.168.19.133:8081
⑥打标记并将nginx:v1.1镜像推送到远程仓库
[root@node1 nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx v1.1 bc151e88c131 About an hour ago 347MB[root@node1 nginx]# docker tag nginx:v1.1 node1:8081/test/nginx:v1.1
[root@node1 nginx]# docker push node1:8081/test/nginx:v1.1
⑦运行镜像
[root@node1 nginx]# docker run -p 80:80 -d nginx:v1.1
068f91f7655046047cf231144e1500bf44ee75eeb3e2aaad367eabce771e9ce0
[root@node1 nginx]# curl 192.168.19.133
<html>
<head>
<title> node1 docker nginx demo </title>
</head>
<body>
<h1> This is Nginx - node1 </h1>
</body>
</html>