php 在 Docker 中安装 GD
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39657058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Installing GD in Docker
提问by evilscary
I am a complete Docker novice but am having to maintain an existing system. The Dockerfile I am using is as below:
我是一个完整的 Docker 新手,但必须维护现有系统。我使用的 Dockerfile 如下:
FROM php:5.6-apache
RUN docker-php-ext-install mysql mysqli
RUN apt-get update -y && apt-get install -y sendmail
RUN apt-get update && \
apt-get install -y \
zlib1g-dev
RUN docker-php-ext-install mbstring
RUN docker-php-ext-install zip
RUN docker-php-ext-install gd
When I run 'docker build [sitename]' everything seems ok until I get the error:
当我运行 'docker build [sitename]' 时一切正常,直到出现错误:
configure: error: png.h not found.
The command '/bin/sh -c docker-php-ext-install gd' returned a non-zero code: 1
What is the cause of this error?
这个错误的原因是什么?
回答by lmtx
You should add the libpng-dev
package to your Dockerfile
:
您应该将libpng-dev
软件包添加到您的Dockerfile
:
FROM php:5.6-apache
RUN docker-php-ext-install mysql mysqli
RUN apt-get update -y && apt-get install -y sendmail libpng-dev
RUN apt-get update && \
apt-get install -y \
zlib1g-dev
RUN docker-php-ext-install mbstring
RUN docker-php-ext-install zip
RUN docker-php-ext-install gd
Then go to directory with Dockerfile
and run:
然后转到目录Dockerfile
并运行:
docker build -t sitename .
docker build -t sitename .
It worked in my case:
它在我的情况下有效:
Removing intermediate container f03522715567
Successfully built 9d69212196a2
Let me know if you get any errors.
如果您有任何错误,请告诉我。
EDIT:
编辑:
You should see something like this:
您应该会看到如下内容:
REPOSITORY TAG IMAGE ID CREATED SIZE
sitename latest 9d69212196a2 19 minutes ago 414 MB
<none> <none> b6c69576a359 25 minutes ago 412.3 MB
EDIT2:
编辑2:
Just to double check everything:
只是为了仔细检查一切:
Please run the docker build
command this way:
请以docker build
这种方式运行命令:
docker build -t sitename:1.0 .
docker build -t sitename:1.0 .
(adding :1.0
should not change anything, I added it just to have additional row in docker images
output)
(添加:1.0
不应该改变任何东西,我添加它只是为了在docker images
输出中有额外的行)
Then start the container:
然后启动容器:
docker run --name sitename_test -p 80:80 sitename:1.0
docker run --name sitename_test -p 80:80 sitename:1.0
It should work just fine.
它应该工作得很好。
I assumed that apache is using standard port (80) - maybe you need to adjust that. If you have other services/containers listening on port 80 you can make your container listening on other port:
我假设 apache 正在使用标准端口 (80) - 也许你需要调整它。如果您有其他服务/容器侦听端口 80,则可以让容器侦听其他端口:
docker run --name sitename_test -p 8080:80 sitename:1.0
docker run --name sitename_test -p 8080:80 sitename:1.0
That will redirect the traffic from port 8080 to port 80 "inside" the container.
这会将流量从端口 8080 重定向到容器“内部”的端口 80。
Normally you run container in the background. To do this add the -d
option to the docker run
command (but for testing purposes you can omit -d
to see output in the console).
通常你在后台运行容器。为此,将-d
选项添加到docker run
命令中(但出于测试目的,您可以省略-d
在控制台中查看输出)。
If you decide to run container in the background you can check logs using docker logs sitename_test
. To follow the logs (and see updates in logs) use -f
option:
如果您决定在后台运行容器,您可以使用docker logs sitename_test
. 要跟踪日志(并查看日志中的更新),请使用-f
选项:
docker logs -f sitename_test
docker logs -f sitename_test
Hope that helps.
希望有帮助。
回答by giapnh
This Dockerfile
worked with Php7
https://hub.docker.com/r/giapnh/php7-gd
这适用Dockerfile
于 Php7
https://hub.docker.com/r/giapnh/php7-gd
FROM php:7-fpm
RUN docker-php-ext-install mysqli pdo pdo_mysql
RUN apt-get update -y && apt-get install -y libwebp-dev libjpeg62-turbo-dev libpng-dev libxpm-dev \
libfreetype6-dev
RUN apt-get update && \
apt-get install -y \
zlib1g-dev
RUN docker-php-ext-install mbstring
RUN apt-get install -y libzip-dev
RUN docker-php-ext-install zip
RUN docker-php-ext-configure gd --with-gd --with-webp-dir --with-jpeg-dir \
--with-png-dir --with-zlib-dir --with-xpm-dir --with-freetype-dir \
--enable-gd-native-ttf
RUN docker-php-ext-install gd
CMD ["php-fpm"]
EXPOSE 9000
回答by phaberest
It is not the case of the OP, but I found that for those that are using php:7.4-fpm-alpine
the syntax is a bit different
OP 的情况并非如此,但我发现对于那些使用php:7.4-fpm-alpine
语法的人来说有点不同
FROM php:7.4-fpm-alpine
# ... Other instructions ...
# Setup GD extension
RUN apk add --no-cache \
freetype \
libjpeg-turbo \
libpng \
freetype-dev \
libjpeg-turbo-dev \
libpng-dev \
&& docker-php-ext-configure gd \
--with-freetype=/usr/include/ \
# --with-png=/usr/include/ \ # No longer necessary as of 7.4; https://github.com/docker-library/php/pull/910#issuecomment-559383597
--with-jpeg=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-enable gd \
&& apk del --no-cache \
freetype-dev \
libjpeg-turbo-dev \
libpng-dev \
&& rm -rf /tmp/*
# ... Other instructions ...
回答by Hirnhamster
Unfortunately, some php extensions have dependencies to other programs. There is a project called docker-php-extension-installerthat you can use to install PHP extensions. It will make sure that the required dependencies are present as well.
不幸的是,一些 php 扩展依赖于其他程序。有一个名为docker-php-extension-installer的项目可用于安装 PHP 扩展。它将确保所需的依赖项也存在。
Since I need that external script in multiple containers, I've put it in a shared scriptthat I then include in the required Dockerfile.
由于我需要在多个容器中使用该外部脚本,因此我将它放在一个共享脚本中,然后将其包含在所需的 Dockerfile 中。
Script (at .shared/scripts/install_php_extensions.sh)
脚本(在 .shared/scripts/install_php_extensions.sh)
#!/bin/sh
# add wget
apt-get update -yqq && apt-get -f install -yyq wget
# download helper script
wget -q -O /usr/local/bin/install-php-extensions https://raw.githubusercontent.com/mlocati/docker-php-extension-installer/master/install-php-extensions \
|| (echo "Failed while downloading php extension installer!"; exit 1)
# install all required extensions
chmod uga+x /usr/local/bin/install-php-extensions && sync && install-php-extensions \
gd \
;
Dockerfile
文件
# get the scripts from the build context and make sure they are executable
COPY .shared/scripts/ /tmp/scripts/
RUN chmod +x -R /tmp/scripts/
# install extensions
RUN /tmp/scripts/install_php_extensions.sh
Caution: Make sure to use the correct build contextin this case.