之前在博文中有说过用docker搭建php环境,但那是用官方镜像,然后多个容器关联,开发起来其实很不方便,那么如何使用Dockerfile构建一个PHP环境呢,为方便移植,快速构建PHP环境,今天试着写了个Dockerfile,包含了php、nginx、composer、git基础环境。
在目录下创建 Dockerfile
、 supervisord.conf
、 nginx.conf
、 index.php
几个文件,比如,我这里的工作目录是 /data/www/test1/
。
Dockerfile:
FROM php:7.2-fpm
MAINTAINER Stephen "mhzuhe@163.com"
RUN apt-get update && \
apt-get install -y curl telnet git zlib1g-dev && \
docker-php-ext-install zip pdo pdo_mysql opcache mysqli && \
apt-get install -y nginx && \
apt-get install -y supervisor && \
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
php composer-setup.php --install-dir=/usr/local/bin --filename=composer && \
php -r "unlink('composer-setup.php');" && \
apt-get clean && rm -rf /var/cache/apt/*
COPY ./supervisord.conf /etc/supervisor/
EXPOSE 80
CMD ["/usr/bin/supervisord"]
守护进程,supervisord.conf
; supervisor config file
[unix_http_server]
file=/var/run/supervisor.sock ; (the path to the socket file)
chmod=0700 ; sockef file mode (default 0700)
[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
nodaemon=true
; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
; The [include] section can just contain the "files" setting. This
; setting can list multiple files (separated by whitespace or
; newlines). It can also contain wildcards. The filenames are
; interpreted as relative to this file. Included files *cannot*
; include files themselves.
[include]
files = /etc/supervisor/conf.d/*.conf
[program:php]
command=/usr/local/sbin/php-fpm --nodaemonize
[program:nginx]
command=/usr/sbin/nginx -g 'daemon off;'
[program:cron]
command=cron -f
nginx.conf
server {
listen 80;
server_name localhost;
root /var/www/html;
index index.html index.htm index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
index.php
<?php
phpinfo();
构建镜像:
docker build -t web:1.0 .
需要稍等几分钟,出现以下信息表示构建成功:
Removing intermediate container 091fb0d704e9
---> aa08dab03d2b
Step 4/5 : EXPOSE 80
---> Running in 15b7b3767670
Removing intermediate container 15b7b3767670
---> d09007ef562a
Step 5/5 : CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
---> Running in e97fbd2058fc
Removing intermediate container e97fbd2058fc
---> e3a832bb7457
Successfully built e3a832bb7457
Successfully tagged web:1.0
最后,运行容器
docker run --name web -d -p 80:80 -v /data/www/test1/:/var/www/html -v /data/www/test1/nginx.conf:/etc/nginx/sites-enabled/default web:1.0
浏览器打开 127.0.0.1:80
,如果出现 phpinfo
的相关信息,表示环境构建成功