Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get composer (php dependency manager) to run on a docker image build

TL;DR Can 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 tool not docker-composer the successor of fig.

I'm trying to build my own docker image to run WordPress installed as a composer dependency.

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).

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).

But when I put all that steps on a Dockerfile I don't get composer to write the files.

I've been trying to get a minimum failing example for some time but the one I've got is quite not minimum.

My test is composed of the following (links to the relevant github repos below)

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

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

  1. 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)

  2. 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)

  3. bash into the running container to be able to see if the files are there

     docker exec -i -t miqueladellv2_wordpress_1 bash
    
  4. 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
    

You can see in step 4 that composer update seems to not have run at all.

I've tried using both

RUN composer update

and

ONBUILD RUN composer update

on Dockerfile with the same results.

If I go back to the previous step 4 of the test and I manually run composer update on the bash prompt of the docker container I get:

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#

which is exactly the output I was expecting on step 4

github links to the full files

  • Dockerfile and its dependencies
  • docker-composer
like image 692
Miquel Adell Avatar asked Jan 19 '16 11:01

Miquel Adell


People also ask

Can I run PHP in docker?

You can use docker run to create a container and execute PHP. You just need to add some volumes to the container. These volumes should include the paths to your code.

What is Composer PHP dependency manager?

Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.


2 Answers

Installing composer like this will avoid this problem:

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.*
like image 178
Brian Wood Avatar answered Oct 09 '22 03:10

Brian Wood


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

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)

server {
    # ... the rest of your nginx config

    root /app/public;

    # ... the rest of your nginx config
}
like image 20
bmagg Avatar answered Oct 09 '22 03:10

bmagg