获取 Composer(php 依赖管理器)以在 docker 镜像构建上运行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34875581/
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
Get composer (php dependency manager) to run on a docker image build
提问by Miquel Adell
NOTE: I no longer use this environment so there is no way for me to test the answers and accept one. I'm sorry.
注意:我不再使用此环境,因此我无法测试答案并接受答案。抱歉。
TL;DRCan you point me to an example of a docker image that uses composer to handle PHP dependencies?
All my questions in this post are regarding composer the php dependency toolnotdocker-composerthe successor of fig.
TL;DR你能指出一个使用 composer 处理 PHP 依赖项的 docker 镜像示例吗?
我在这篇文章中的所有问题都是关于composer php 依赖工具而不是docker-composer fig的继任者。
I'm trying to build my own docker image to run wordpress installed as a composer dependency.
我正在尝试构建我自己的 docker 镜像来运行作为 composer 依赖项安装的 wordpress。
I'm working on building a docker image using docker php image as a base and what I need to do is install composer and run a composer update command either on image creation time or on image build time (don't know if both would be ok).
我正在使用 docker php 映像作为基础构建 docker 映像,我需要做的是安装 composer 并在映像创建时间或映像构建时间运行 composer update 命令(不知道两者是否都是好的)。
I can run everything just fine by manually executing all the steps (running a docker image, bashing into it, and copying and pasting every step).
我可以通过手动执行所有步骤(运行 docker 映像,猛击它,然后复制和粘贴每个步骤)来运行一切正常。
But when I put all that steps on a Dockerfile I don't get composer to write the files.
但是当我将所有这些步骤放在 Dockerfile 上时,我没有让 Composer 来编写文件。
I've been trying to get a minimum failing example for some time but the one I've got is quite not mininum.
一段时间以来,我一直试图获得一个最小的失败示例,但我得到的示例并不是最小的。
My test is composed of the following (links to the relevant github repos below)
我的测试由以下内容组成(链接到下面的相关 github 存储库)
Dockerfile
文件
NFORMATION ~~~#
# based on
# https://hub.docker.com/r/richarvey/nginx-php-fpm/
# and
#?https://hub.docker.com/_/wordpress/
FROM php:7.0.2-apache
MAINTAINER Miquel Adell <[email protected]>
ENV WORDPRESS_VERSION 4.4.1
#~~~?DEPENDENCIES ~~~#
# Add PHP repository to apt source
RUN apt-get update \
&& apt-get install -y \
libpng12-dev \
libjpeg-dev \
curl \
sed \
zlib1g-dev \
&& docker-php-ext-install \
zip \
mysqli
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
#~~~ DIRS ~~~#
WORKDIR /var/www/html/
#~~~ WORDPRESS ~~~#
COPY files/composer.json composer.json
ONBUILD RUN composer update
docker-compose.yml
docker-compose.yml
wordpress:
image: miqueladell/composed_wordpress_test
links:
- wordpress_db:mysql
environment:
- VIRTUAL_HOST=miqueladell.dev
- WORDPRESS_DB_NAME=wordpress
ports:
- "80"
wordpress_db:
image: miqueladell/mariadb-utf8mb4
environment:
- MYSQL_ROOT_PASSWORD=password
My test is as follows
我的测试如下
Build an image executing this command in a directory containing the Dockerfile pasted above
docker build -t miqueladell/composed_wordpress_test .
(no errors in the log)
Use that image to build a container by running the following command in a directory containing the docker-compose.yml pasted above
docker-compose up
(no errors in the log)
bash into the running container to be able to see if the files are there
docker exec -i -t miqueladellv2_wordpress_1 bash
ls of /var/www/html
root@bff14367658b:/var/www/html# ls -al total 12 drwxr-xr-x 2 www-data www-data 4096 Jan 19 10:50 . drwxr-xr-x 5 root root 4096 Jan 19 10:50 .. -rw-r--r-- 1 root root 138 Jan 15 09:18 composer.json
在包含上面粘贴的 Dockerfile 的目录中构建执行此命令的映像
docker build -t miqueladell/composed_wordpress_test .
(日志中没有错误)
通过在包含上面粘贴的 docker-compose.yml 的目录中运行以下命令,使用该图像构建容器
docker-compose up
(日志中没有错误)
bash 进入正在运行的容器,以便能够查看文件是否在那里
docker exec -i -t miqueladellv2_wordpress_1 bash
/var/www/html 的 ls
root@bff14367658b:/var/www/html# ls -al total 12 drwxr-xr-x 2 www-data www-data 4096 Jan 19 10:50 . drwxr-xr-x 5 root root 4096 Jan 19 10:50 .. -rw-r--r-- 1 root root 138 Jan 15 09:18 composer.json
You can see in step 4 that composer updateseems to not have run at all.
您可以在第 4 步中看到composer update似乎根本没有运行。
I've tried using both
我试过同时使用
RUN composer update
and
和
ONBUILD RUN composer update
on Dockerfile with the same results.
在 Dockerfile 上具有相同的结果。
If I go back to the previous step 4 of the test and I manually run composer updateon the bash prompt of the docker container I get:
如果我回到测试的前一个步骤 4 并在 docker 容器的 bash 提示符下手动运行composer update,我会得到:
root@bff14367658b:/var/www/html# composer update
Loading composer repositories with package information
Updating dependencies (including require-dev)
- Installing johnpbloch/wordpress-core-installer (0.2.1)
Downloading: 100%
- Installing johnpbloch/wordpress (4.4.1)
Downloading: 100%
Writing lock file
Generating autoload files
root@bff14367658b:/var/www/html# ls -al
total 24
drwxr-xr-x 4 www-data www-data 4096 Jan 19 11:12 .
drwxr-xr-x 6 root root 4096 Jan 19 11:12 ..
-rw-r--r-- 1 root root 138 Jan 15 09:18 composer.json
-rw-r--r-- 1 root root 3718 Jan 19 11:12 composer.lock
drwxr-xr-x 4 root root 4096 Jan 19 11:12 vendor
drwxr-xr-x 5 root root 4096 Jan 19 11:12 wordpress
root@bff14367658b:/var/www/html#
wich is exactly the output I was expecting on step 4
这正是我在第 4 步中期望的输出
I would love some advice. Thanks.
我希望得到一些建议。谢谢。
github links to the full files
github 链接到完整文件
回答by Brian Wood
Installing composer like this will avoid this problem:
像这样安装 composer 可以避免这个问题:
RUN curl -o /tmp/composer-setup.php https://getcomposer.org/installer \
&& curl -o /tmp/composer-setup.sig https://composer.github.io/installer.sig \
# Make sure we're installing what we think we're installing!
&& php -r "if (hash('SHA384', file_get_contents('/tmp/composer-setup.php')) !== trim(file_get_contents('/tmp/composer-setup.sig'))) { unlink('/tmp/composer-setup.php'); echo 'Invalid installer' . PHP_EOL; exit(1); }" \
&& php /tmp/composer-setup.php --no-ansi --install-dir=/usr/local/bin --filename=composer --snapshot \
&& rm -f /tmp/composer-setup.*
回答by bmagg
I ran into this problem today.
我今天遇到了这个问题。
What solved it for me was to use a different directory than the one that was defined in the image.
为我解决的是使用与图像中定义的目录不同的目录。
It seems like changes that are made to the directory during build process are discarded if the directory is defined as a volume.
如果将目录定义为卷,则在构建过程中对目录所做的更改似乎会被丢弃。
Here's an example of my working Dockerfile
这是我的工作 Dockerfile 的示例
FROM richarvey/nginx-php-fpm
# Install dependencies
RUN apt-get update && \
apt-get install curl nano && \
curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# Add update nginx config
COPY conf/nginx-site.conf /etc/nginx/sites-available/default.conf
# Bundle app source
COPY app/ /app
# Install app dependencies
RUN cd /app && \
composer install --no-interaction
EXPOSE 80
And then in conf/nginx-site.conf
I updated the root for my application (shortened for brevity)
然后在conf/nginx-site.conf
我更新了我的应用程序的根目录(为了简洁而缩短)
server {
# ... the rest of your nginx config
root /app/public;
# ... the rest of your nginx config
}