首页 文章

让composer(php依赖管理器)在docker镜像构建上运行

提问于
浏览
28

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; DR你能指点一个使用composer处理PHP依赖关系的docker镜像的例子吗?我在这篇文章中的所有问题都是关于作曲家的php依赖工具而不是docker-composer无花果的继承者 .

我正在尝试构建自己的docker镜像来运行wordpress installed as a composer dependency .

我正在使用docker php image作为基础构建docker镜像,我需要做的是安装composer并在图像创建时或图像构建时运行composer update命令(不知道两者是否都是好) .

我可以通过手动执行所有步骤(运行docker镜像,攻击它,以及复制和粘贴每一步)来运行一切 .

但是,当我将所有步骤放在Dockerfile上时,我没有让作曲家编写文件 .

我一直试图在一段时间内得到一个最小的失败的例子,但我得到的那个并不是最小的 .

我的测试由以下内容组成(链接到下面相关的github repos)

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 <miquel@miqueladell.com>

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

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

  • 在包含上面粘贴的Dockerfile的目录中构建执行此命令的映像
docker build -t miqueladell/composed_wordpress_test .

(日志中没有错误)

  • 通过在包含上面粘贴的docker-compose.yml的目录中运行以下命令,使用该映像构建容器
docker-compose up

(日志中没有错误)

  • bash到正在运行的容器中,以便能够查看文件是否存在
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

您可以在步骤4中看到作曲家更新似乎根本没有运行 .

我试过用这两个

RUN composer update

ONBUILD RUN composer update

在Dockerfile上具有相同的结果 .

如果我回到测试的前一步骤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#

这正是我在第4步期待的输出

我会喜欢一些建议 . 谢谢 .

github links to the full files

2 回答

  • 16

    我今天遇到了这个问题 .

    为我解决的是使用与图像中定义的目录不同的目录 .

    如果将目录定义为卷,则似乎会丢弃在构建过程中对目录所做的更改 .

    这是我工作的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
    

    然后在 conf/nginx-site.conf 我更新了我的应用程序的根目录(为简洁而缩短)

    server {
        # ... the rest of your nginx config
    
        root /app/public;
    
        # ... the rest of your nginx config
    }
    
  • 16

    像这样安装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.*
    

相关问题