dockerphpinclude的简单介绍

http://www.itjxue.com  2023-01-24 01:13  来源:未知  点击次数: 

如何 在Docker窗口中部署PHP开发环境

环境部署一直是一个很大的问题,无论是开发环境还是生产环境,但是 Docker

将开发环境和生产环境以轻量级方式打包,提供了一致的环境。极大的提升了开发部署一致性。当然,实际情况并没有这么简单,因为生产环境和开发环境的配置是完全不同的,比如日志等的问题都需要单独配置,但是至少比以前更加简单方便了,这里以

PHP 开发作为例子讲解 Docker 如何布置开发环境。

一般来说,一个 PHP 项目会需要以下工具:

Web 服务器: Nginx/Tengine

Web 程序: PHP-FPM

数据库: MySQL/PostgreSQL

缓存服务: Redis/Memcache

这是最简单的架构方式,在 Docker 发展早期,Docker 被大量的滥用,比如,一个镜像内启动多服务,日志收集依旧是按照 Syslog

或者别的老方式,镜像容量非常庞大,基础镜像就能达到 80M,这和 Docker 当初提出的思想完全南辕北辙了,而 Alpine Linux

发行版作为一个轻量级 Linux 环境,就非常适合作为 Docker 基础镜像,Docker 官方也推荐使用 Alpine 而不是 Debian

作为基础镜像,未来大量的现有官方镜像也将会迁移到 Alpine 上。本文所有镜像都将以 Alpine 作为基础镜像。

Nginx/Tengine

这部分笔者已经在另一篇文章 Docker 容器的 Nginx 实践中讲解了 Tengine 的 Docker 实践,并且给出了

Dockerfile,由于比较偏好 Tengine,而且官方已经给出了 Nginx 的 alpine 镜像,所以这里就用

Tengine。笔者已经将镜像上传到官方 DockerHub,可以通过

codedocker pull chasontang/tengine:2.1.2_f/code

获取镜像,具体请看 Dockerfile。

PHP-FPM

Docker 官方已经提供了 PHP 的 7.0.7-fpm-alpine 镜像,Dockerfile 如下:

FROM alpine:3.4

# persistent / runtime deps

ENV PHPIZE_DEPS \

autoconf \

file \

g++ \

gcc \

libc-dev \

make \

pkgconf \

re2c

RUN apk add --no-cache --virtual .persistent-deps \

ca-certificates \

curl

# ensure www-data user exists

RUN set -x \

addgroup -g 82 -S www-data \

adduser -u 82 -D -S -G www-data www-data

# 82 is the standard uid/gid for "www-data" in Alpine

#

#

#

ENV PHP_INI_DIR /usr/local/etc/php

RUN mkdir -p $PHP_INI_DIR/conf.d

##autogenerated##

ENV PHP_EXTRA_CONFIGURE_ARGS --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data

##/autogenerated##

ENV GPG_KEYS 1A4E8B7277C42E53DBA9C7B9BCAA30EA9C0D5763

ENV PHP_VERSION 7.0.7

ENV PHP_FILENAME php-7.0.7.tar.xz

ENV PHP_SHA256 9cc64a7459242c79c10e79d74feaf5bae3541f604966ceb600c3d2e8f5fe4794

RUN set -xe \

apk add --no-cache --virtual .build-deps \

$PHPIZE_DEPS \

curl-dev \

gnupg \

libedit-dev \

libxml2-dev \

openssl-dev \

sqlite-dev \

curl -fSL "" -o "$PHP_FILENAME" \

echo "$PHP_SHA256 *$PHP_FILENAME" | sha256sum -c - \

curl -fSL "" -o "$PHP_FILENAME.asc" \

export GNUPGHOME="$(mktemp -d)" \

for key in $GPG_KEYS; do \

gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \

done \

gpg --batch --verify "$PHP_FILENAME.asc" "$PHP_FILENAME" \

rm -r "$GNUPGHOME" "$PHP_FILENAME.asc" \

mkdir -p /usr/src \

tar -Jxf "$PHP_FILENAME" -C /usr/src \

mv "/usr/src/php-$PHP_VERSION" /usr/src/php \

rm "$PHP_FILENAME" \

cd /usr/src/php \

./configure \

--with-config-file-path="$PHP_INI_DIR" \

--with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \

$PHP_EXTRA_CONFIGURE_ARGS \

--disable-cgi \

# --enable-mysqlnd is included here because it's harder to compile after the fact than extensions are (since it's a plugin for several extensions, not an extension in itself)

--enable-mysqlnd \

# --enable-mbstring is included here because otherwise there's no way to get pecl to use it properly (see )

--enable-mbstring \

--with-curl \

--with-libedit \

--with-openssl \

--with-zlib \

make -j"$(getconf _NPROCESSORS_ONLN)" \

make install \

{ find /usr/local/bin /usr/local/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; } \

make clean \

runDeps="$( \

scanelf --needed --nobanner --recursive /usr/local \

| awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \

| sort -u \

| xargs -r apk info --installed \

| sort -u \

)" \

apk add --no-cache --virtual .php-rundeps $runDeps \

apk del .build-deps

COPY docker-php-ext-* /usr/local/bin/

##autogenerated##

WORKDIR /var/www/html

RUN set -ex \

cd /usr/local/etc \

if [ -d php-fpm.d ]; then \

# for some reason, upstream's php-fpm.conf.default has "include=NONE/etc/php-fpm.d/*.conf"

sed 's!=NONE/!=!g' php-fpm.conf.default | tee php-fpm.conf /dev/null; \

cp php-fpm.d/ php-fpm.d/; \

else \

# PHP 5.x don't use "include=" by default, so we'll create our own simple config that mimics PHP 7+ for consistency

mkdir php-fpm.d; \

cp php-fpm.conf.default php-fpm.d/; \

{ \

echo '[global]'; \

echo 'include=etc/php-fpm.d/*.conf'; \

} | tee php-fpm.conf; \

fi \

{ \

echo '[global]'; \

echo 'error_log = /proc/self/fd/2'; \

echo; \

echo '[www]'; \

echo '; if we send this to /proc/self/fd/1, it never appears'; \

echo 'access.log = /proc/self/fd/2'; \

echo; \

echo 'clear_env = no'; \

echo; \

echo '; Ensure worker stdout and stderr are sent to the main error log.'; \

echo 'catch_workers_output = yes'; \

} | tee php-fpm.d/docker.conf \

{ \

echo '[global]'; \

echo 'daemonize = no'; \

echo; \

echo '[www]'; \

echo 'listen = [::]:9000'; \

} | tee php-fpm.d/zz-docker.conf

EXPOSE 9000

CMD ["php-fpm"]

##/autogenerated##

在MAC上利用Docker镜像搭建PHP5.2开发环境

随着MAC操作系统和各种软件的不断升级,在系统上搭建APACHE2.4+PHP5.2的环境变得异常困难,笔者尝试过通过brew install php@5.6的环境,并利用brew-php-swithcer或者brew link php 5.6来和PHP7做切换,但实际使用中,PHP5依赖一些软件包,比如openssl比较老,导致出现各种问题,并不能很好的解决在一个电脑上,运行两套PHP开发环境的需求。基于此,笔者只能另辟蹊径,通过Dokcer来解决这个问题。

1、官网查找一下php可以使用的镜像(别人做好上传的),直接搜比如php5.2之类的

2、笔者找的是flandera/php5.2-apache这个,自带apache,由于缺乏文档,把镜像拉下来,跑一下进去看看

进去后,研究下结构,结果如下:

apache的conf文件目录在/etc/conf/apache2/apache2.conf,此外这个conf文件include了一个空的httpd.conf文件

网站根目录在/var/www下

3、解决思路

编制一个dockerfile文件,将httpd.conf文件打包成新的镜像

将程序通过docker命令映射到容器中

4、编制conf文件

5、编制dockerfile(参考,名称和目录根据实际修改)

6、重新编译并运行

执行命令时候,httpd.conf需要和Dockerfile在同一个目录,如果conf文件有改动,需要重新打包

5、mac电脑打

看是不是正常了

Docker PHP 安装扩展汇总

FROM php:7.2-fpm

Core, ctype, curl

date, dom

fileinfo, filter, ftp

hash

iconv

json

libxml

mbstring, mysqlnd

openssl

pcre, PDO, pdo_sqlite, Phar, posix

readline, Reflection, session, SimpleXML, sodium, SPL, sqlite3, standard

tokenizer

xml, xmlreader, xmlwriter

zlib

RUN docker-php-ext-install -j$(nproc) bcmath calendar exif gettext

sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm

RUN apt-get update

apt-get install -y --no-install-recommends libbz2-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) bz2

RUN apt-get update

apt-get install -y --no-install-recommends libenchant-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) enchant

RUN apt-get update

apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/

docker-php-ext-install -j$(nproc) gd

RUN apt-get update

apt-get install -y --no-install-recommends libgmp-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) gmp

RUN apt-get update

apt-get install -y --no-install-recommends libxml2-dev libtidy-dev libxslt1-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) soap wddx xmlrpc tidy xsl

RUN apt-get update

apt-get install -y --no-install-recommends libzip-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) zip

RUN apt-get update

apt-get install -y --no-install-recommends libsnmp-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) snmp

RUN apt-get update

apt-get install -y --no-install-recommends libpq-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) pgsql pdo_pgsql

RUN apt-get update

apt-get install -y --no-install-recommends libpspell-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) pspell

RUN apt-get update

apt-get install -y --no-install-recommends librecode-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) recode

RUN apt-get update

apt-get install -y --no-install-recommends firebird-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) pdo_firebird

RUN apt-get update

apt-get install -y --no-install-recommends freetds-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-configure pdo_dblib --with-libdir=lib/x86_64-linux-gnu

docker-php-ext-install -j$(nproc) pdo_dblib

RUN apt-get update

apt-get install -y --no-install-recommends libldap2-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-configure ldap --with-libdir=lib/x86_64-linux-gnu

docker-php-ext-install -j$(nproc) ldap

RUN apt-get update

apt-get install -y --no-install-recommends libc-client-dev libkrb5-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-configure imap --with-kerberos --with-imap-ssl

docker-php-ext-install -j$(nproc) imap

RUN apt-get update

apt-get install -y --no-install-recommends firebird-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) interbase

RUN apt-get update

apt-get install -y --no-install-recommends libicu-dev

rm -r /var/lib/apt/lists/*

docker-php-ext-install -j$(nproc) intl

RUN apt-get update \

apt-get install -y --no-install-recommends libmcrypt-dev

rm -r /var/lib/apt/lists/*

pecl install mcrypt-1.0.1

docker-php-ext-enable mcrypt

RUN export CFLAGS=" PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS"

apt-get update

apt-get install -y --no-install-recommends libmagickwand-dev

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

pecl install imagick-3.4.3

docker-php-ext-enable imagick

RUN apt-get update \

apt-get install -y --no-install-recommends zlib1g-dev libmemcached-dev

rm -r /var/lib/apt/lists/*

pecl install memcached

docker-php-ext-enable memcached

RUN pecl install redis-4.0.1 docker-php-ext-enable redis

RUN docker-php-ext-configure opcache --enable-opcache docker-php-ext-install opcache

RUN set -ex;

docker-php-source extract;

{

echo '# ' ;

echo 'AC_DEFUN([PHP_ALWAYS_SHARED],[])dnl';

echo;

cat /usr/src/php/ext/odbc/config.m4;

} temp.m4;

mv temp.m4 /usr/src/php/ext/odbc/config.m4;

apt-get update;

apt-get install -y --no-install-recommends unixodbc-dev;

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

docker-php-ext-configure odbc --with-unixODBC=shared,/usr;

docker-php-ext-configure pdo_odbc --with-pdo-odbc=unixODBC,/usr;

docker-php-ext-install odbc pdo_odbc;

docker-php-source delete

LABEL Author="Leo"

LABEL Version="1.0.25-fpm"

LABEL Description="PHP FPM 7.2 镜像. All extensions."

(责任编辑:IT教学网)

更多

推荐excel文章