前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Dockerfile构建PHP,Nginx,Composer,lnmp环境

Dockerfile构建PHP,Nginx,Composer,lnmp环境

作者头像
Petrochor
发布2022-06-07 15:43:27
8520
发布2022-06-07 15:43:27
举报
文章被收录于专栏:StephenStephen

之前在博文中有说过用docker搭建php环境,但那是用官方镜像,然后多个容器关联,开发起来其实很不方便,那么如何使用Dockerfile构建一个PHP环境呢,为方便移植,快速构建PHP环境,今天试着写了个Dockerfile,包含了php、nginx、composer、git基础环境。

在目录下创建 Dockerfilesupervisord.confnginx.confindex.php 几个文件,比如,我这里的工作目录是 /data/www/test1/

Dockerfile:

代码语言:javascript
复制
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

代码语言:javascript
复制
; 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

代码语言:javascript
复制
    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

代码语言:javascript
复制
<?php

phpinfo();

构建镜像:

代码语言:javascript
复制
docker build -t web:1.0 .

需要稍等几分钟,出现以下信息表示构建成功:

代码语言:javascript
复制
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

最后,运行容器

代码语言:javascript
复制
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 的相关信息,表示环境构建成功

GitHub :https://github.com/mhzuhe/php-web.git

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019-12-15 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器镜像服务
容器镜像服务(Tencent Container Registry,TCR)为您提供安全独享、高性能的容器镜像托管分发服务。您可同时在全球多个地域创建独享实例,以实现容器镜像的就近拉取,降低拉取时间,节约带宽成本。TCR 提供细颗粒度的权限管理及访问控制,保障您的数据安全。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档