1. 创建dockerfile存放目录

1.1 创建目录

[root@docker ~]# mkdir -p /dockerfile
[root@docker ~]# cd /dockerfile/
[root@docker dockerfile]# mkdir -p nginx
[root@docker dockerfile]# cd nginx/

2. 创建nginx dockerfile配置文件

2.1 创建文件

[root@docker nginx]# touch Dockerfile

2.2 编写文件

[root@docker nginx]# vim Dockerfile 
[root@docker nginx]# cat Dockerfile 
#this docker file
#VERSION 1
#author:shichao@scajy.cn

FROM centos:7
MAINTAINER shichao@scajy.cn
RUN rpm -ivh http://mirrors.ustc.edu.cn/epel/7/x86_64/Packages/e/epel-release-7-12.noarch.rpm 
RUN yum install -y gcc gcc-c++ make pcre pcre-devel openssl openssl-devel  pcre-davel gd-devel iproute net-tools telnet wget curl && yum clean  all && rm -rf /var/cache/yum/*
RUN useradd -M -s /sbin/nologin nginx
RUN  wget http://nginx.org/download/nginx-1.17.6.tar.gz && tar zxf  nginx-1.17.6.tar.gz && \
	cd nginx-1.17.6  && \
	./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module  --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module && \
	make && make install
ENV PATH /usr/local/nginx/sbin:$PATH

EXPOSE 80

ENTRYPOINT ["nginx"]

CMD ["-g","daemon off;"]

3. 构建nginx配置文件

[root@docker nginx]# docker build -t nginx:v1 .
Sending build context to Docker daemon   2.56kB
Step 1/9 : FROM centos:7
 ---> 7e6257c9f8d8
Step 2/9 : MAINTAINER shichao@scajy.cn
 ---> Using cache
 ---> dc37782fc495
Step 3/9 : RUN yum install -y gcc gcc-c++ make pcre pcre-devel openssl openssl-devel  pcre-davel gd-devel iproute net-tools telnet wget curl && yum clean  all && rm -rf /var/cache/yum/*
 ---> Using cache
 ---> 32738f8a27ef
Step 4/9 : RUN useradd -M -s /sbin/nologin nginx
 ---> Using cache
 ---> 8c44900481e6
Step 5/9 : RUN  wget http://nginx.org/download/nginx-1.17.6.tar.gz && tar zxf  nginx-1.17.6.tar.gz && 	cd nginx-1.17.6  && 	./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module  --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module && 	make && make install
 ---> Using cache
 ---> e490f86836cd
Step 6/9 : ENV PATH /usr/local/nginx/sbin:$PATH
 ---> Using cache
 ---> fc93090b9333
Step 7/9 : EXPOSE 80
 ---> Using cache
 ---> d188570f3558
Step 8/9 : ENTRYPOINT ["nginx"]
 ---> Using cache
 ---> 8602a2e5b992
Step 9/9 : CMD ["-g","daemon off;"]
 ---> Using cache
 ---> a87daa7377d2
Successfully built a87daa7377d2
Successfully tagged nginx:v1

3.1 查看镜像,构建是否成功

[root@docker nginx]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
nginx               v1                  a87daa7377d2        2 minutes ago       365MB
<none>              <none>              ebd89a778c08        6 minutes ago       227MB
nginx               nginx01             2fc3f55169e9        3 days ago          133MB
nginx               latest              7e4d58f0e5f3        4 weeks ago         133MB
busybox             latest              6858809bf669        4 weeks ago         1.23MB
centos              7                   7e6257c9f8d8        2 months ago        203MB
centos              latest              0d120b6ccaa8        2 months ago        215MB

4. 使用nginx镜像创建docker容器

[root@docker nginx]# docker run  -it -d --name=nginx_v1 --network=test -p8081:80 nginx:v1
30593a76c50a9e8b31ec0a829de3b35a8103996c2bcd728171143ca1505fbc88

5. 查看容器是否存在

[root@docker nginx]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                  NAMES
30593a76c50a        nginx:v1            "nginx -g 'daemon of…"   5 seconds ago       Up 4 seconds        0.0.0.0:8081->80/tcp   nginx_v1

6. 访问测试

6.1 本机测试

[root@docker nginx]# curl -I http://127.0.0.1:8081
HTTP/1.1 200 OK
Server: nginx/1.17.6
Date: Tue, 13 Oct 2020 12:00:51 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 13 Oct 2020 11:53:14 GMT
Connection: keep-alive
ETag: "5f85952a-264"
Accept-Ranges: bytes