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部署的时候引用此基础镜像就可以了

相关推荐
卡布奇诺-海晨10 分钟前
RockyLinux9.6搭建k8s集群
容器·kubernetes
Huathy-雨落江南,浮生若梦3 小时前
k8s入门教程(集群部署、使用,镜像拉取失败网络问题排查)
网络·容器·kubernetes
罗技1233 小时前
我用Amazon Q写了一个Docker客户端,并上架了懒猫微服商店
运维·docker·容器
2501_911121233 小时前
Nginx+Tomcat 负载均衡群集
nginx·tomcat·负载均衡
爱宇阳4 小时前
使用 Docker Compose 部署 Jenkins(LTS 版)持续集成环境
ci/cd·docker·jenkins
背太阳的牧羊人7 小时前
backend 服务尝试连接 qdrant 容器,但失败了,返回 502 Bad Gateway 问题排查
docker·gateway·qdrant
xiaomu_3478 小时前
基于Linux系统docker封装exe
linux·运维·服务器·docker
❀͜͡傀儡师10 小时前
如何使用k8s安装redis呢
redis·容器·kubernetes
小鱼小鱼.oO13 小时前
阿里云服务器安装nginx并配置前端资源路径(前后端部署到一台服务器并成功访问)
服务器·nginx·阿里云
广东数字化转型16 小时前
nginx怎么使用nginx-rtmp-module模块实现直播间功能
linux·运维·nginx