先上文件结构
compose_nginx/
├── docker-compose.yml
├── nginx
│ ├── Dockerfile
│ └── nginx-1.12.2.tar.gz
└── wwwroot
└── index.html
使用dockerfile制作一个自定义的镜像,镜像中编译安装nginx-1.12.2
nginx的源码包可到nginx的官网下载
下面是各文件内容:
docker-compose.yaml
version: '3'
services:
nginx: # 服务名称为nginx
hostname: nginx
build:
context: ./nginx
dockerfile: Dockerfile # 指定dockerfile
ports:
- 1215:80
- 1216:443
networks:
- cluster # 网络命名空间
volumes:
- ./wwwroot:/usr/local/nginx/html # 挂载
networks:
cluster:
Dockerfile
自定义的镜像,里面编译安装了nginx
FROM acktsw/centos
MAINTAINER this is for docker-compose
ADD nginx-1.12.2.tar.gz /usr/local/src
RUN yum -y install gcc gcc-c++ make pcre* zlib-devel
RUN useradd -M -s /sbin/nologin nginx
WORKDIR /usr/local/src
WORKDIR nginx-1.12.2
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
EXPOSE 80
EXPOSE 443
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
CMD ["nginx"]
镜像下载速度较慢,可尝试将镜像修改为centos:7
wwwroot目录下的index.html则可以自定义
echo "docker-compose nginx" >> wwwroot/index.html
启动
docker-compose -f docker-compose.yml up -d
-d 后台启动
-f 指定yml文件
部分部署过程:
Removing intermediate container 03b341f7caa9
---> f8e399652c97
Step 9/13 : RUN ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
---> Running in 83bd3bfc0408
Removing intermediate container 83bd3bfc0408
---> ad95ab7d4555
Step 10/13 : EXPOSE 80
---> Running in 87dca6817b39
Removing intermediate container 87dca6817b39
---> 0a9edc93454a
Step 11/13 : EXPOSE 443
---> Running in 3cf74082c3b8
Removing intermediate container 3cf74082c3b8
---> 44e8cf3a3e61
Step 12/13 : RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
---> Running in 29ebc1d1a7df
待部署完成后,尝试访问
注意指定端口1215
[root@localhost compose_nginx]# curl 127.0.0.1:1215
docker-compose nginx
部署成功!