生成镜像

  • 创建一个空目录docker_php
  • 有Dockerfile,生成镜像的主文件
  • overrides.conf php-fpm 配置文件
  • php-fpm-startup php-fpm 启动脚本

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
####################################
# PHPDocker.io PHP 7.2 / FPM image #
####################################

FROM phpdockerio/php72-cli

# Install FPM
RUN export DEBIAN_FRONTEND=noninteractive \
    && apt-get update \
    && apt-get -y --no-install-recommends install php7.2-fpm \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* /usr/share/doc/*

# PHP-FPM packages need a nudge to make them docker-friendly
COPY overrides.conf /etc/php/7.2/fpm/pool.d/z-overrides.conf

# PHP-FPM has really dirty logs, certainly not good for dockerising
# The following startup script contains some magic to clean these up
COPY php-fpm-startup /usr/bin/php-fpm

RUN chmod 777 /usr/bin/php-fpm

CMD /usr/bin/php-fpm # 处理没有权限

# Open up fcgi port
EXPOSE 9000

提示错误

docker /bin/sh: 1: /usr/bin/php-fpm: Permission denied 添加 RUN chmod 777 /usr/bin/php-fpm 在Dockerfile中

overrides.conf

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
[global]
; Override default pid file
pid = /run/php-fpm.pid

; Avoid logs being sent to syslog
error_log = /proc/self/fd/2

[www]
; Access from webserver container is via network, not socket file
listen = [::]:9000

; Redirect logs to stdout - FPM closes /dev/std* on startup
access.log = /proc/self/fd/2
catch_workers_output = yes

; Required to allow config-by-environment
clear_env = no

php-fpm-startup

1
2
#!/usr/bin/env bash
/usr/sbin/php-fpm7.2 -F -O 2>&1 | sed -u 's,.*: \"\(.*\)$,\1,'| sed -u 's,"$,,' 1>&1

生成镜像

1
docker build -t eee/php7.2 .

查看镜像

1
docker images

拉取DockerHub镜像

拉取nginx

1
docker pull nginx 

启动nginx

1
2
3
docker run --name eee-nginx -p 8888:80 nginx
# --name 别名
# -p 端口映射 本地8888端口映射到容器中80端口

访问 http://localhost:8888 展示 Welcome to nginx!

启动PHP

创建一个index.php文件, 放在/Users/eee/workspace/php目录下面

1
2
<?php
phpinfo();

执行命令

1
2
3
4
docker run --name test-php -v /Users/eee/workspace/php:/www eee/php72

# --name test-php 将容器命名为 test-php
# -v /Users/eee/workspace/php:/www  将主机中项目php 挂载到容器的 /www

启动Nginx

创建一个test-php.conf文件放在/Users/eee/workspace/php/conf.d文件下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
server {
    listen       80;
    server_name  localhost;

    location / {
        root   /www;
        index  index.html index.htm index.php;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}
# /www/:  test-php  php 文件的存储路径

执行命令

1
2
3
4
5
6
7
8
docker run --name test-nginx -p 8888:80 -v /Users/eee/workspace/php/conf.d:/etc/nginx/conf.d -v /Users/eee/workspace/php:/www --link test-php:php nginx


# --name test-nginx 将容器命名为 test-nginx
# -p 8888:80 将本地8888端口映射到容器80端口
# -v /Users/eee/workspace/php/conf.d:/etc/nginx/conf.d 将nginx配置文件挂载到/etc/nginx/conf.d目录下
# -v /Users/eee/workspace/php:/www  将主机中项目php 挂载到容器的 /www 否则不能找到文件
# --link test-php:php 把 test-php 的网络并入 nginx,并通过修改 nginx 的 /etc/hosts,把域名 php 映射成 127.0.0.1,让 nginx 通过 php:9000 访问 php-fpm。

访问 http://localhost:8888 就成功了。

上面的启动方式太繁琐了,使用docker-composer.yaml来启动全部服务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
version: '3'

services:
  php72:
    container_name: mbti-php72
    image: phpdockerio/php72-fpm
    volumes:
      - /Users/eee/workspace/php:/www

  nginx:
    container_name: mbti-nginx
    image: nginx
    ports:
      - 8888:80
    links:
      - php72:php
    volumes:
      - /Users/eee/workspace/php/conf.d:/etc/nginx/conf.d
      - /Users/eee/workspace/php:/www

启动

1
docker-compose up