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