(Docker) How to install dependencies, using separate Composer container, in WordPress container?

Dockerfile

FROM wordpress

ENV REFRESHED_AT 2015-08-12

ADD 
  composer.json /var/www/html
ADD 
  composer.lock /var/www/html

# install the PHP extensions
RUN 
  apt-get -qq update && 
  apt-get -y upgrade && 
  apt-get install -y vim wget && 
  rm -rf /var/lib/apt/lists/*

# Symlink User's "wp-content" folder into the newly installed WordPress
RUN 
  rm -rf /usr/src/wordpress/wp-content/plugins/* && 
  rm -rf /usr/src/wordpress/wp-content/themes/* && 
  cp -fr /usr/src/wordpress/* /var/www/html/ && 
  chown -R www-data:www-data /var/www/html/

# volume for mysql database and wordpress install
VOLUME ["/var/www/html/wp-content/plugins", "/var/www/html/wp-content/themes"]

# Define working directory.
WORKDIR /var/www/html/

EXPOSE 80 3306

CMD ["apache2-foreground"]

Docker Compose File

wordpress:
  build: .
  links:
    - mysql
    - composer
  volumes:
    - wp-content/plugins/:/var/www/html/wp-content/plugins
    - wp-content/themes/:/var/www/html/wp-content/themes
  environment:
    - WORDPRESS_DB_PASSWORD=__WORDPRESS_DB_PASSWORD__
    - WORDPRESS_DB_NAME=__WORDPRESS_DB_NAME__
    # - WORDPRESS_DB_USER=__WORDPRESS_DB_USER__

  ports:
    - "9888:80"

mysql:
  image: mysql:5.7
  environment:
    - MYSQL_ROOT_PASSWORD=__WORDPRESS_DB_PASSWORD__
    - MYSQL_DATABASE=__WORDPRESS_DB_NAME__

composer:
  image: composer/composer

Question details

I’m able to ADD the composer.json and composer.lock files to the working directory. I can confirm that these two files are in the working directory.

What I need is for the Dockerfile (or wherever) to also automatically install the dependencies into the working directory.

Read More

According to Docker Hub, https://hub.docker.com/r/composer/composer/,
I should be able to docker run -v $(pwd):/app composer/composer install to install the dependencies but how do I do this in Dockerfile?

Also I’m confused because the -v flag, https://docs.docker.com/engine/userguide/dockervolumes/, has to do with mounting the specified host directory into the a container but I’ve already ADDed the necessary files to the working directory. All I want to do is install the dependencies.

Thank you for your help.

Related posts

1 comment

  1. You just need to mount the current directory to /app when running your composer container. I’ve put together a simple example to illustrate this working at https://gist.github.com/andyshinn/e2c428f2cd234b718239.

    The key parts here are the volumes for the composer part of the application and the restart: 'yes' on the primary PHP application (the application likely will not run until Composer has run so you will want it to restart).

Comments are closed.