一、准备工作

1、下载基础镜像

docker pull centos:7.8.2003

2、创建存放nginx的目录和Dockerfile文件

mkdir /home/nginx

cd /opt/nginx

touch Dockerfile

3、下载nginx源码包到nginx目录下

wget http://nginx.org/download/nginx-1.18.0.tar.gz

二、编写Dockerfile

1、配置文件,注意添加daemon off;不是以守护进程启动,否则进程进行完成会自动关闭,容器无法再访问

vim Dockerfile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
FROM centos:7.8.2003

MAINTAINER opsuser@gwops.com

ADD nginx-1.18.0.tar.gz /usr/local/src
WORKDIR /usr/local/src/nginx-1.18.0

RUN yum install -y gcc gcc-c++ glibc make autoconf openssl openssl-devel \
&& yum install -y libxslt-devel -y gd gd-devel GeoIP GeoIP-devel pcre pcre-devel \
&& useradd -M -s /sbin/nologin nginx && BUILD_CONFIG="--prefix=/usr/local/nginx \
--http-client-body-temp-path=/var/cache/nginx/client_temp \
--http-proxy-temp-path=/var/cache/nginx/proxy_temp \
--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \
--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \
--http-scgi-temp-path=/var/cache/nginx/scgi_temp \
--with-http_stub_status_module \
--with-http_ssl_module \
--with-stream \
--with-http_v2_module \
--with-threads" && ./configure $BUILD_CONFIG \
&& mkdir -p /var/cache/nginx && make && make install


ENV PATH /usr/local/nginx/sbin:$PATH

EXPOSE 80
EXPOSE 443

ENTRYPOINT ["nginx"]

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

2执行docker build进行构建,注意命令后的.需要把文件夹下载的nginx打包进去

docker build -t centos7.8_nginx1.18:v1 .

Successfully built 3ae0d07c99ec
Successfully tagged centos7.8_nginx1.18:v1

3构建成功提示,并查看

[root@nginx]# docker image list
REPOSITORY TAG IMAGE ID CREATED SIZE
centos7.8_nginx1.18 v1 3ae0d07c99ec About a minute ago 462MB

三、启动新镜像

1、-d后台运行,-p指定端口,-v映射路径

mkdir -p /home/nginx/www /home/nginx/conf/

1
docker run -d -p 8168:80 --name nginx-rancher-168 -v /home/nginx/www:/usr/share/nginx/html -v /home/nginx/conf/nginx.conf:/usr/local/nginx/conf/nginx.conf  centos7.8_nginx1.18:v1

2、查看容器

1
2
3
# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
7487ad7d9bc1 centos_nginx:v1 "nginx -g 'daemon of…" About a minute ago Up About a minute 0.0.0.0:80->80/tcp nginx-v1

3、浏览器访问地址即可

四、重新编译nginx

1、进入容器内部

docker exec -it nginx-v1 /bin/bash

2、重新编译

在./configure 后添加参数,增加需要的模块