docker php8.1+nginx base 镜像 dockerfile 配置

这个是docker 配置的base dockerfile 如果搭建php+nginx 环境,可以直接使用这个dockerfile配置。直接在你代码里面的dockerfile 引用此基础镜像。

使用官方的 PHP 镜像作为基础镜像

FROM php:8.1-fpm

更换国内源并安装系统依赖

RUN rm -rf /etc/apt/sources.list.d/* \

&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware' > /etc/apt/sources.list \

&& echo 'deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware' >> /etc/apt/sources.list \

&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware' >> /etc/apt/sources.list \

&& echo 'deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware' >> /etc/apt/sources.list \

&& echo 'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware' >> /etc/apt/sources.list \

&& echo 'deb-src https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware' >> /etc/apt/sources.list \

&& echo 'deb https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware' >> /etc/apt/sources.list \

&& echo 'deb-src https://security.debian.org/debian-security bookworm-security main contrib non-free non-free-firmware' >> /etc/apt/sources.list \

&& apt-get update \

&& apt-get install -y \

locales \

nginx \

libpng-dev \

libjpeg-dev \

libonig-dev \

libxml2-dev \

zip \

unzip \

libzip-dev \

libmcrypt-dev \

libssl-dev \

libfreetype6-dev \

libjpeg62-turbo-dev \

freetds-dev \

libcurl4-openssl-dev \

libsnmp-dev \

libicu-dev \

libc-client-dev \

libkrb5-dev \

libmemcached-dev \

libbz2-dev \

libtidy-dev \

libxslt-dev \

wget \

curl \

cron \

supervisor \

&& sed -i '/zh_CN.UTF-8/s/# //g' /etc/locale.gen \

&& locale-gen zh_CN.UTF-8 \

&& apt-get clean \

&& rm -rf /var/lib/apt/lists/*

设置环境变量以使用中文区域设置

ENV LANG=zh_CN.UTF-8

ENV LANGUAGE=zh_CN:zh

ENV LC_ALL=zh_CN.UTF-8

安装 PHP 扩展

RUN docker-php-ext-install \

bcmath \

bz2 \

calendar \

exif \

gd \

gettext \

mysqli \

opcache \

pdo \

pdo_mysql \

snmp \

soap \

sockets \

tidy \

xml \

xsl \

zip

下载并安装 Composer

RUN 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');" \

&& mkdir /composer \

&& chown www-data:www-data /composer \

&& chmod 755 /composer

设置 Composer 全局路径

ENV COMPOSER_HOME=/composer

ENV PATH=/composer/vendor/bin:$PATH

安装 Redis 和 Yaf 扩展

RUN pecl install redis \

&& docker-php-ext-enable redis \

&& pecl install yaf \

&& docker-php-ext-enable yaf

设置权限和目录

RUN chown www-data:www-data /var/www/html \

&& chmod 755 /var/www/html \

&& mkdir /etc/nginx/conf.d/ssl \

&& chmod 755 /etc/nginx/conf.d/ssl

复制supervisord配置文件

COPY supervisord.conf /etc/supervisor/supervisord.conf

暴露容器的 80 和 443 端口

EXPOSE 80 443

启动 PHP-FPM 和 Nginx 服务

#CMD /etc/init.d/nginx start && /usr/local/sbin/php-fpm -D && tail -f /dev/null

下面是supervisord 配置。用suervisord 控制PHP-fpm 和nginx 的启动

; 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)

; 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.

program:php-fpm

command = /usr/local/sbin/php-fpm --force-stderr --nodaemonize --fpm-config /usr/local/etc/php-fpm.conf

autostart=true

autorestart=true

priority=5

stdout_events_enabled=true

stderr_events_enabled=true

stdout_logfile=/dev/stdout

stdout_logfile_maxbytes=0

stderr_logfile=/dev/stderr

stderr_logfile_maxbytes=0

stopsignal=QUIT

program:nginx

command=/usr/sbin/nginx -g "daemon off;" -c /etc/nginx/nginx.conf

autostart=true

autorestart=true

priority=10

stdout_events_enabled=true

stderr_events_enabled=true

stdout_logfile=/dev/stdout

stdout_logfile_maxbytes=0

stderr_logfile=/dev/stderr

stderr_logfile_maxbytes=0

stopsignal=QUIT

直接把这两份配置上传到你的服务器里面。然后执行下面的命令

docker build -t php-nginx -f /home/data/dockerfile/prod/Dockerfile .

php-nginx 是镜像的名字

/home/data/dockerfile/prod/Dockerfile 是dockerfile 文件路径

最后的点一定要加上。

base 镜像编辑好后,可以上传到阿里云的免费的镜像容器服务。然后在docker部署的时候引用此基础镜像就可以了

相关推荐
小声读源码3 小时前
【技巧】离线安装docker镜像的方法
docker·镜像·技巧·离线
Think Spatial 空间思维7 小时前
【SSL部署与优化】如何为网站启用HTTPS:从Let‘s Encrypt免费证书到Nginx配置
nginx·https·证书·部署·ssl·配置·优化
李匠20248 小时前
C++GO语言微服务之Dockerfile && docker-compose②
c++·容器
斤斤计较8 小时前
Docker 环境安装(2025最新版)
运维·docker·容器
小锋学长生活大爆炸8 小时前
【教程】Docker方式本地部署Overleaf
运维·docker·容器
欧先生^_^9 小时前
Docker 的各种网络模式
网络·docker·容器
掘金者说9 小时前
docker系列-DockerDesktop报错信息(Windows Hypervisor is not present)
运维·docker·容器
酷爱码11 小时前
Nginx的核心功能--正向代理、反向代理、缓存和Rewrite
运维·nginx·缓存
筱宇***11 小时前
Mac的web服务器
mysql·nginx·macos·php
我不是秃头sheep13 小时前
Ubuntu 安装 Docker(镜像加速)完整教程
linux·ubuntu·docker