下载镜像并初始化系统

docker pull centos:7.6.1810

测试启动容器

docker run -it centos:7.6.1810 /bin/bash
exit

创建目录环境,宿主机下

cd /opt/
mkdir dockerfile/{web/{nginx,tomcat,jdk,apache},system/{centos,ubuntu,redhat}} -pv
cd /opt/dockerfile/web/nginx

准备Nginx安装包

wget –c http://nginx.org/download/nginx-1.16.1.tar.gz

准备Nginx配置文件

cd /usr/local/src/
wget https://nginx.org/download/nginx-1.16.1.tar.gz
tar xvf nginx-1.16.1.tar.gz
cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_sub_module
make
make install

修改并复制配置文件至nginx目录

vim nginx.conf
user  www;
daemon off;
        location ~ \.php$ {
            root           html;
            fastcgi_pass   0.0.0.0:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

cp /usr/local/nginx/conf/nginx.conf /opt/dockerfile/web/nginx/nginx.conf

编写 Dockerfile

vim ./Dockerfile

#"#"为注释, 等于 shell 脚本的中# #除了注释行之外的第一行,必须是 From xxx (xxx 是基础镜像)

#第一行先定义基础镜像,后面的本地有效的镜像名,如果本地没 有会从远程仓库下载,第一行很重要

#My Dockerfile
From centos:7.6.1810
MAINTAINER silence 2178937492@qq.com
RUN rpm -ivh http://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm
RUN yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pcre-devel zlib zlib-devel openssl openssl-devel iproute net-tools iotop make
ADD nginx-1.16.1.tar.gz /usr/local/src/
RUN cd /usr/local/src/nginx-1.16.1 && ./configure --prefix=/usr/local/nginx --with-http_sub_module && make && make install
RUN cd /usr/local/nginx/
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
RUN useradd www -s /sbin/nologin -u 1000
RUN ln -sv /usr/local/nginx/sbin/nginx /usr/sbin/nginx
RUN echo "Dockerfile for Nginx" > /usr/local/nginx/html/index.html
ADD index.php /usr/local/nginx/html/
EXPOSE 80 443
CMD ["nginx"]

执行镜像构建

docker build -t nginx-php /opt/dockerfile/web/nginx/

提示

 ---> 65244b07269c
Successfully built 65244b07269c
Successfully tagged 172.24.77.241/nginx/nginx-1.16.1:v2

查看是否生成本地镜像

docker images

从镜像启动容器

docker run -d -p 80:80 --name nginx-php 172.24.77.241/nginx/nginx-php /usr/sbin/nginx

上传镜像到harbor

docker login 172.24.77.241
docker push 172.24.77.241/nginx/nginx-php

测试访问

基于Nginx镜像制nginx+php-fpm镜像

创建对应目录

mkdir -p /opt/dockerfile/web/php

准备配置文件

docker pull php:5.6-fpm
docker run -d --name  php5 php:5.6-fpm
docker cp d9f84073241f:/usr/local/etc/php/php.ini-production ./php.ini
docker cp d9f84073241f:/usr/local/etc/php-fpm.conf.default ./php-fpm.conf
docker cp 6ec90a30e7ca:/usr/local/etc/php-fpm.d/www.conf ./www.conf
vim php.ini
user = www
group = www
;listen = 127.0.0.1:9000

准备启动文件

vim run_php.sh
#!/bin/bash
/usr/local/php/sbin/php-fpm -c /etc/php.ini -y /usr/local/php/etc/php-fpm.conf
/usr/local/nginx/sbin/nginx
tail -f /etc/hosts
chmod +x run_php.sh 

编写 Dockerfile

cd /opt/dockerfile/web/php
vim Dockerfile
FROM nginx-php:latest
MAINTAINER silence 2178937492@qq.com
RUN yum install epel-release -y && \
    yum install -y gcc gcc-c++ make gd-devel libxml2-devel \
    libcurl-devel libjpeg-devel libpng-devel openssl-devel \
    libmcrypt-devel libxslt-devel libtidy-devel autoconf \
    iproute net-tools telnet wget curl && \
    yum clean all && \
    rm -rf /var/cache/yum/*
RUN wget http://docs.php.net/distributions/php-5.6.36.tar.gz && \
    tar zxf php-5.6.36.tar.gz && \
    cd php-5.6.36 && \
    ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --enable-fpm --enable-opcache \
    --with-mysql --with-mysqli --with-pdo-mysql \
    --with-openssl --with-zlib --with-curl --with-gd \
    --with-jpeg-dir --with-png-dir --with-freetype-dir \
    --enable-mbstring --with-mcrypt --enable-hash && \
    make -j 4 && make install && \
    sed -i "90a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
    mkdir /usr/local/php/log && \
    cd / && rm -rf php* && \
    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
ENV PATH $PATH:/usr/local/php/sbin:/usr/local/php/bin
WORKDIR /usr/local/php
RUN useradd www -s /sbin/nologin -u 1000
ADD php.ini /usr/local/php/etc/php.ini
ADD php-fpm.conf /usr/local/php/etc/php-fpm.conf
ADD run_php.sh /usr/bin
EXPOSE 9000
CMD ["/usr/bin/run_php.sh"]

执行镜像构建

docker build -t nginx-php /opt/dockerfile/web/php/

提示

 ---> cd6ea7d90c0e
Successfully built cd6ea7d90c0e
Successfully tagged nginx-php:latest

查看镜像是否生成

docker images
REPOSITORY                           TAG                 IMAGE ID            CREATED             SIZE
nginx-php                           latest              cd6ea7d90c0e        4 minutes ago       1.1GB

从镜像启动容器

docker run -d -p 9000:9000 -p 80:80 --name nginx16-php5 nginx-php

查看容器启动

CONTAINER ID     IMAGE          COMMAND              CREATED           STATUS              PORTS                                                 NAMES
605b75536c33    nginx-php     "/usr/bin/run_php.sh"   2 seconds ago       Up 2 seconds        0.0.0.0:80->80/tcp, 0.0.0.0:9000->9000/tcp, 443/tcp   nginx16-php5

关闭所有容器

docker rm -fv `docker ps -a -q`

测试访问

http://172.24.77.242/index.php