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

相关推荐
恩爸编程1 小时前
探索 Nginx:Web 世界的幕后英雄
运维·nginx·nginx反向代理·nginx是什么·nginx静态资源服务器·nginx服务器·nginx解决哪些问题
鸠摩智首席音效师2 小时前
Docker 中如何限制CPU和内存的使用 ?
docker·容器
Michaelwubo2 小时前
Docker dockerfile镜像编码 centos7
运维·docker·容器
jingyu飞鸟2 小时前
centos-stream9系统安装docker
linux·docker·centos
努力--坚持3 小时前
电商项目-网站首页高可用(一)
nginx·lua·openresty
好像是个likun3 小时前
使用docker拉取镜像很慢或者总是超时的问题
运维·docker·容器
玖疯子5 小时前
介绍 Docker 的基本概念和优势,以及在应用程序开发中的实际应用。
docker
暴富的Tdy5 小时前
【快速上手Docker 简单配置方法】
docker·容器·eureka
Karoku0666 小时前
【k8s集群应用】kubeadm1.20高可用部署(3master)
运维·docker·云原生·容器·kubernetes
豆豆豆豆变6 小时前
docker之compose篇
docker·容器·自动化运维