1. 创建dockerfile存放目录

1.1 创建目录

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

2. 创建php dockerfile配置文件

2.1 创建文件

[root@docker nginx]# touch Dockerfile

2.2 下载php包

  • 官网下载地址: https://www.php.net/

    [root@docker php]# wget https://www.php.net/distributions/php-7.2.34.tar.gz
    [root@docker php]# ll -a
    总用量 19472
    drwxr-xr-x. 2 root root       49 10月 13 20:38 .
    drwxr-xr-x. 4 root root       30 10月 13 20:21 ..
    -rw-r--r--. 1 root root        0 10月 13 20:21 Dockerfile
    -rw-r--r--. 1 root root 19936114 10月 13 20:37 php-7.2.34.tar.gz
    

2.3 编写文件

[root@docker php]# vim Dockerfile 
[root@docker php]# cat Dockerfile 
#this docker file
#VERSION 1
#author:shichao@scajy.cn
FROM centos:7
MAINTAINER  shichao@scajy.cn
RUN yum install -y gcc gcc-c++ make gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel
ADD php-7.2.34.tar.gz /tmp/

RUN cd /tmp/php-7.2.34 && \
    ./configure --prefix=/usr/local/php \
    --with-config-file-path=/usr/local/php/etc \
    --with-mysql --with-mysqli \
    --with-openssl --with-zlib --with-curl --with-gd \
    --with-jpeg-dir --with-png-dir --with-iconv \
    --enable-fpm --enable-zip --enable-mbstring && \
    make -j 4 && \
    make install && \
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf && \
    cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf && \
    sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.d/www.conf && \
    sed -i "21a \daemonize = no" /usr/local/php/etc/php-fpm.conf && \
    cp  php.ini-production /usr/local/php/etc/php.ini

RUN rm -rf /tmp/php-7.2.34* && yum clean all

WORKDIR /usr/local/php
EXPOSE 9000
CMD ["./sbin/php-fpm", "-c", "/usr/local/php/etc/php-fpm.conf"]

3. 构建php配置文件

[root@docker php]# docker build -t php:v1 .
Sending build context to Docker daemon  19.94MB
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 gd-devel libxml2-devel libcurl-devel libjpeg-devel libpng-devel openssl-devel
 ---> Using cache
 ---> fc741796df21
Step 4/9 : ADD php-7.2.34.tar.gz /tmp/
 ---> Using cache
 ---> 10e40e4e3211
Step 5/9 : RUN cd /tmp/php-7.2.34 &&     ./configure --prefix=/usr/local/php     --with-config-file-path=/usr/local/php/etc     --with-mysql --with-mysqli     --with-openssl --with-zlib --with-curl --with-gd     --with-jpeg-dir --with-png-dir --with-iconv     --enable-fpm --enable-zip --enable-mbstring &&     make -j 4 &&     make install &&     cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf &&     cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf &&     sed -i "s/127.0.0.1/0.0.0.0/" /usr/local/php/etc/php-fpm.conf &&     sed -i "21a \daemonize = no" /usr/local/php/etc/php-fpm.conf &&     cp  php.ini-production /usr/local/php/etc/php.ini
 ---> Using cache
 ---> 7a39c5288966
Step 6/9 : RUN rm -rf /tmp/php-7.2.34* && yum clean all
 ---> Using cache
 ---> 48884b61b259
Step 7/9 : WORKDIR /usr/local/php
 ---> Using cache
 ---> 46e1d65adc72
Step 8/9 : EXPOSE 9000
 ---> Using cache
 ---> f76e5fc23518
Step 9/9 : CMD ["./sbin/php-fpm", "-c", "/usr/local/php/etc/php-fpm.conf"]
 ---> Using cache
 ---> e797501fb114
Successfully built e797501fb114
Successfully tagged php:v1

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

[root@docker php]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php                 v1                  e797501fb114        24 minutes ago      1.17GB
nginx               v1                  a87daa7377d2        2 hours ago         365MB
<none>              <none>              ebd89a778c08        2 hours 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. 使用php镜像创建docker容器

[root@docker php]# docker run -it -d --name=php_v1 --network=test  php:v1
237aa105fab91184a88b30c53851923000f4809289998b79704d2b6756f0c0c1

5. 验证php容器是否运行

[root@docker php]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED              STATUS              PORTS                  NAMES
237aa105fab9        php:v1              "./sbin/php-fpm -c /…"   About a minute ago   Up About a minute   9000/tcp               php_v1
30593a76c50a        nginx:v1            "nginx -g 'daemon of…"   2 hours ago          Up 2 hours          0.0.0.0:8081->80/tcp   nginx_v1