首页 > 编程语言 >php docker image

php docker image

时间:2024-09-30 14:11:26浏览次数:9  
标签:PHP install image && docker php

Quick reference

Note: the description for this image is longer than the Hub length limit of 25000, so the "Supported tags" list has been trimmed to compensate. See also docker/hub-feedback#238⁠ and docker/roadmap#475⁠.

See "Supported tags and respective Dockerfile links" at https://github.com/docker-library/docs/tree/master/php/README.md⁠

Quick reference (cont.)

  • Where to file issues:
    link

  • Supported architectures: (more info⁠)

    • amd64, arm32v5, arm32v6, arm32v7, arm64v8, i386, mips64le, ppc64le, riscv64, s390x
  • Published image artifact details:

  • Image updates

    • official-images repo's library/php label⁠
    • official-images repo's library/php file⁠ (history⁠)
  • Source of this description

What is PHP?

PHP is a server-side scripting language designed for web development, but which can also be used as a general-purpose programming language. PHP can be added to straight HTML or it can be used with a variety of templating engines and web frameworks. PHP code is usually processed by an interpreter, which is either implemented as a native module on the web-server or as a common gateway interface (CGI).

wikipedia.org/wiki/PHP

logo

How to use this image

Create a Dockerfile in your PHP project

FROM php:8.2-cli
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp
CMD [ "php", "./your-script.php" ]

Then, run the commands to build and run the Docker image:

docker build -t my-php-app .
docker run -it --rm --name my-running-app my-php-app

Run a single PHP script

For many simple, single file projects, you may find it inconvenient to write a complete Dockerfile. In such cases, you can run a PHP script by using the PHP Docker image directly:

docker run -it --rm --name my-running-script -v "$PWD":/usr/src/myapp -w /usr/src/myapp php:8.2-cli php your-script.php

How to install more PHP extensions

Many extensions are already compiled into the image, so it's worth checking the output of php -m or php -i before going through the effort of compiling more.

We provide the helper scripts docker-php-ext-configure, docker-php-ext-install, and docker-php-ext-enable to more easily install PHP extensions.

In order to keep the images smaller, PHP's source is kept in a compressed tar file. To facilitate linking of PHP's source with any extension, we also provide the helper script docker-php-source to easily extract the tar or delete the extracted source. Note: if you do use docker-php-source to extract the source, be sure to delete it in the same layer of the docker image.

FROM php:8.2-cli
RUN docker-php-source extract \
    # do important things \
    && docker-php-source delete

PHP Core Extensions

For example, if you want to have a PHP-FPM image with the gd extension, you can inherit the base image that you like, and write your own Dockerfile like this:

FROM php:8.2-fpm
RUN apt-get update && apt-get install -y \
        libfreetype-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd

Remember, you must install dependencies for your extensions manually. If an extension needs custom configure arguments, you can use the docker-php-ext-configure script like this example. There is no need to run docker-php-source manually in this case, since that is handled by the configure and install scripts.

If you are having difficulty figuring out which Debian or Alpine packages need to be installed before docker-php-ext-install, then have a look at the install-php-extensions project⁠. This script builds upon the docker-php-ext-* scripts and simplifies the installation of PHP extensions by automatically adding and removing Debian (apt) and Alpine (apk) packages. For example, to install the GD extension you simply have to run install-php-extensions gd. This tool is contributed by community members and is not included in the images, please refer to their Git repository for installation, usage, and issues.

See also "Dockerizing Compiled Software"⁠ for a description of the technique Tianon uses for determining the necessary build-time dependencies for any bit of software (which applies directly to compiling PHP extensions).

Default extensions

Some extensions are compiled by default. This depends on the PHP version you are using. Run php -m in the container to get a list for your specific version.

PECL extensions

Some extensions are not provided with the PHP source, but are instead available through PECL⁠. To install a PECL extension, use pecl install to download and compile it, then use docker-php-ext-enable to enable it:

FROM php:8.2-cli
RUN pecl install redis-5.3.7 \
    && pecl install xdebug-3.2.1 \
    && docker-php-ext-enable redis xdebug
FROM php:8.2-cli
RUN apt-get update && apt-get install -y libmemcached-dev libssl-dev zlib1g-dev \
    && pecl install memcached-3.2.0 \
    && docker-php-ext-enable memcached

It is strongly recommended that users use an explicit version number in their pecl install invocations to ensure proper PHP version compatibility (PECL does not check the PHP version compatibility when choosing a version of the extension to install, but does when trying to install it). Beyond the compatibility issue, it's also a good practice to ensure you know when your dependencies receive updates and can control those updates directly.

Unlike PHP core extensions, PECL extensions should be installed in series to fail properly if something went wrong. Otherwise errors are just skipped by PECL. For example, pecl install memcached-3.2.0 && pecl install redis-5.3.7 instead of pecl install memcached-3.2.0 redis-5.3.7. However, docker-php-ext-enable memcached redis is fine to be all in one command.

Other extensions

Some extensions are not provided via either Core or PECL; these can be installed too, although the process is less automated:

FROM php:8.2-cli
RUN curl -fsSL '[url-to-custom-php-module]' -o module-name.tar.gz \
    && mkdir -p module-name \
    && sha256sum -c "[shasum-value]  module-name.tar.gz" \
    && tar -xf module-name.tar.gz -C module-name --strip-components=1 \
    && rm module-name.tar.gz \
    && ( \
        cd module-name \
        && phpize \
        && ./configure --enable-module-name \
        && make -j "$(nproc)" \
        && make install \
    ) \
    && rm -r module-name \
    && docker-php-ext-enable module-name

The docker-php-ext-* scripts can accept an arbitrary path, but it must be absolute (to disambiguate from built-in extension names), so the above example could also be written as the following:

FROM php:8.2-cli
RUN curl -fsSL '[url-to-custom-php-module]' -o module-name.tar.gz \
    && mkdir -p /tmp/module-name \
    && sha256sum -c "[shasum-value]  module-name.tar.gz" \
    && tar -xf module-name.tar.gz -C /tmp/module-name --strip-components=1 \
    && rm module-name.tar.gz \
    && docker-php-ext-configure /tmp/module-name --enable-module-name \
    && docker-php-ext-install /tmp/module-name \
    && rm -r /tmp/module-name

Running as an arbitrary user

For running the Apache variants as an arbitrary user, there are a couple choices:

  • If your kernel is version 4.11 or newer⁠, you can add --sysctl net.ipv4.ip_unprivileged_port_start=0 (which will be the default in a future version of Docker⁠) and then --user should work as it does for FPM.
  • If you adjust the Apache configuration to use an "unprivileged" port (greater than 1024 by default), then --user should work as it does for FPM regardless of kernel version.

For running the FPM variants as an arbitrary user, the --user flag to docker run should be used (which can accept both a username/group in the container's /etc/passwd file like --user daemon or a specific UID/GID like --user 1000:1000).

"E: Package 'php-XXX' has no installation candidate"

As of docker-library/php#542⁠, this image blocks the installation of Debian's PHP packages. There is some additional discussion of this change in docker-library/php#551 (comment)⁠, but the gist is that installing Debian's PHP packages in this image leads to two conflicting installations of PHP in a single image, which is almost certainly not the intended outcome.

For those broken by this change and looking for a workaround to apply in the meantime while a proper fix is developed, adding the following simple line to your Dockerfile should remove the block (with the strong caveat that this will allow the installation of a second installation of PHP, which is definitely not what you're looking for unless you really know what you're doing):

RUN rm /etc/apt/preferences.d/no-debian-php

The proper solution to this error is to either use FROM debian:XXX and install Debian's PHP packages directly, or to use docker-php-ext-install, pecl, and/or phpize to install the necessary additional extensions and utilities.

Configuration

This image ships with the default php.ini-development⁠ and php.ini-production⁠ configuration files.

It is strongly recommended to use the production config for images used in production environments!

The default config can be customized by copying configuration files into the $PHP_INI_DIR/conf.d/ directory.

Example

FROM php:8.2-fpm-alpine

# Use the default production configuration
RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

In many production environments, it is also recommended to (build and) enable the PHP core OPcache extension for performance. See the upstream OPcache documentation⁠ for more details.

Image Variants

The php images come in many flavors, each designed for a specific use case.

Some of these tags may have names like bookworm or bullseye in them. These are the suite code names for releases of Debian⁠ and indicate which release the image is based on. If your image needs to install any additional packages beyond what comes with the image, you'll likely want to specify one of these explicitly to minimize breakage when there are new releases of Debian.

php:<version>-cli**

This variant contains the PHP CLI⁠ tool with default mods. If you need a web server, this is probably not the image you are looking for. It is designed to be used both as a throw away container (mount your source code and start the container to start your app), as well as a base from which to build other images.

It also is the only variant which contains the (not recommended) php-cgi binary, which is likely necessary for some things like PPM⁠.

Note that all variants of php contain the PHP CLI (/usr/local/bin/php).

php:<version>-apache**

This image contains Debian's Apache httpd in conjunction with PHP (as mod_php) and uses mpm_prefork by default.

Apache with a Dockerfile

FROM php:7.2-apache
COPY src/ /var/www/html/

Where src/ is the directory containing all your PHP code. Then, run the commands to build and run the Docker image:

docker build -t my-php-app .
docker run -d --name my-running-app my-php-app

We recommend that you add a php.ini configuration file; see the "Configuration" section for details.

Apache without a Dockerfile

docker run -d -p 80:80 --name my-apache-php-app -v "$PWD":/var/www/html php:7.2-apache

Changing DocumentRoot (or other Apache configuration)

Some applications may wish to change the default DocumentRoot in Apache (away from /var/www/html). The following demonstrates one way to do so using an environment variable (which can then be modified at container runtime as well):

FROM php:7.1-apache

ENV APACHE_DOCUMENT_ROOT /path/to/new/root

RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf

A similar technique could be employed for other Apache configuration options.

php:<version>-fpm

This variant contains PHP-FPM, which is a FastCGI implementation for PHP. See the PHP-FPM website⁠ for more information about PHP-FPM.

In order to use this image variant, some kind of reverse proxy (such as NGINX, Apache, or other tool which speaks the FastCGI protocol) will be required.

Some potentially helpful resources:

  • PHP-FPM.org⁠
  • simplified example by @md5⁠
  • very detailed article by Pascal Landau⁠
  • Stack Overflow discussion⁠
  • Apache httpd Wiki example⁠

WARNING: the FastCGI protocol is inherently trusting, and thus extremely insecure to expose outside of a private container network -- unless you know exactly what you are doing (and are willing to accept the extreme risk), do not use Docker's --publish (-p) flag with this image variant.

php:<version>-alpine

This image is based on the popular Alpine Linux project⁠, available in the alpine official image. Alpine Linux is much smaller than most distribution base images (~5MB), and thus leads to much slimmer images in general.

This variant is useful when final image size being as small as possible is your primary concern. The main caveat to note is that it does use musl libc⁠ instead of glibc and friends⁠, so software will often run into issues depending on the depth of their libc requirements/assumptions. See this Hacker News comment thread⁠ for more discussion of the issues that might arise and some pro/con comparisons of using Alpine-based images.

To minimize image size, it's uncommon for additional related tools (such as git or bash) to be included in Alpine-based images. Using this image as a base, add the things you need in your own Dockerfile (see the alpine image description for examples of how to install packages if you are unfamiliar).

License

View license information⁠ for the software contained in this image.

As with all Docker images, these likely also contain other software which may be under other licenses (such as Bash, etc from the base distribution, along with any direct or indirect dependencies of the primary software being contained).

Some additional license information which was able to be auto-detected might be found in the repo-info repository's php/ directory⁠.

As for any pre-built image usage, it is the image user's responsibility to ensure that any use of this image complies with any relevant licenses for all software contained within.

标签:PHP,install,image,&&,docker,php
From: https://www.cnblogs.com/zakun/p/18441736

相关文章

  • 【Linux】Docker下载与使用-nginx
    目录一、Docker介绍     二、Docker结构三、下载Daocker1.在linux上下载docker,执行以下命令即可:2.开启docker3.执行以下操作并进行使用四、在Docker上安装nginx一、Docker介绍             Docker:是给予Go语言实现的开源项目。   ......
  • php: 查看opcache的使用情况
    一,代码:opcache的各项参数设置效果怎么样?是否需要调整?我们需要了解一个opcache使用情况的统计,以供调整参数时处理看php代码:<?php//确保已经启用了Opcacheif(extension_loaded('ZendOPcache')&&function_exists('opcache_get_status')){$status=opcache_get_stat......
  • php 文件上传
    环境ubuntu22.04thinkphp6.1.0php-fpm8.1nginx1.18.0实现文件上传本质上就是html页面这边提交一个表单,请求上传文件<formaction="http://192.168.1.13:8888/view/upload"method="post"name="file_upload"target="_blank"enctype="mul......
  • php email功能实现:详细步骤与配置技巧?
    phpemail发送功能详细教程?如何使用phpemail服务?无论是用户注册、密码重置,还是订单确认,电子邮件都是与用户沟通的重要手段。AokSend将详细介绍如何实现phpemail功能,并提供一些配置技巧,帮助你更好地掌握这一关键技术。phpemail:配置环境在开始使用phpemail功能之前,确保你......
  • thinkphp6调用微信商户支付-合单支付工具代码开发
    合单支付基本在加盟店或是分公司或是营销系统里面常见。他的出现,打破了传统提现支付或是转账支付。他的业务原理其实很简单,就是需要优先申请非普通商户,其次是每个入驻的商户都需要申请普通商户。在这之前一定要申请好对应的场景服务,比如公众号支付就需要优先申请公众号,小程序支......
  • pbootcms模板报错提示PHP Warning: Unknown: open_basedir restriction
    遇到PbootCMS模板中出现类似 PHPWarning:Unknown:open_basedirrestrictionineffect.File 的错误提示,通常是由于PHP的 open_basedir 配置限制导致的。这种情况下,可以通过调整PHP版本或修改 open_basedir 配置来解决问题。解决方案1.更换PHP版本根据你的描......
  • PbootCMS百度编辑器ueditor在PHP7下多图上传名字重复问题
    针对百度编辑器UEditor在PHP7环境下多图上传名字重复的问题,PbootCMSV1.3.8已经进行了修复。以下是具体的修改步骤和详细说明,供遇到类似问题的开发者参考:修改步骤修改 /ueditor/dialogs/attachment/attachment.js 文件将 _this.fileList.push(json); 修改为:javascri......
  • dockerfile测试例子
    #Seehttps://aka.ms/customizecontainertolearnhowtocustomizeyourdebugcontainerandhowVisualStudiousesthisDockerfiletobuildyourimagesforfasterdebugging.FROM127.0.0.1/basic/mcr.microsoft.com/dotnet/aspnet:6.0ASbaseWORKDIR/appEXPOSE......
  • 木偶人在线音乐播放器PHP网站源码
    源码介绍木偶人音乐播放器是一款开源的基于Meting的在线音乐播放器。具有音乐搜索、播放、下载、歌词同步显示、个人网易云音乐播放列表同步等功能。前端界面参照QQ音乐网页版进行布局,同时采用了流行的响应式设计,无论是在PC端还是在手机端,均能给您带来原生app般的使......
  • [Docker学习笔记]Docker的原理&&Docker常见命令
    文章目录什么是DockerDocker的优势Docker的原理Docker的安装Docker的`namespaces`Docker的常见命令`dockerversion`:查看版本信息`dockerinfo`查看docker详细信息我们关注的信息`dockersearch`:镜像搜索`dockerpull`:镜像拉取到本地`dockerpush`:推送本地镜像......