3

I am trying to run a webserver container with nginx and a php-fpm server running a Laravel application. The directory houses a frontend and backend directory so my paths are not off in the configuration files.

I setup a docker-compose.yml file

version: '2'
services:
  webserver:
    build:
      context: ./
      dockerfile: webserver.docker
    volumes:
      - /home/colesam/Documents/code/todo/backend:/var/www
    ports:
      - "8080:80"
    links:
      - backend
  backend:
    build:
      context: ./
      dockerfile: backend.docker
    volumes:
      - ./home/colesam/Documents/code/todo/backend:/var/www

And php-fpm Dockerfile (backend.docker)

FROM php:7.2-fpm

RUN apt-get update -y && apt-get install -y libmcrypt-dev openssl
RUN pecl install mcrypt-1.0.2
RUN docker-php-ext-enable mcrypt
RUN docker-php-ext-install pdo mbstring
RUN apt-get install -y apt-transport-https
RUN apt-get install -y curl
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN chown -R www-data:www-data /var/www
VOLUME ["/var/www"]
RUN ls -al /var/www
WORKDIR /var/www
RUN composer install

And the nginx webserver Dockerfile (webserver.docker)

FROM nginx:1.10

ADD ./vhost.conf /etc/nginx/conf.d/default.conf
WORKDIR /var/www

For some reason when I run docker-compose up -d --build I always fail at this step of the build process:

Step 13/13 : RUN composer install
 ---> Running in 65bb97f03004
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer could not find a composer.json file in /var/www
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section
ERROR: Service 'backend' failed to build: The command '/bin/sh -c composer install' returned a non-zero code: 1

From the last line of the output it looks like it failed because /var/www/was empty and didn't have the files copied over like composer.json. I looked around a lot and this stackoverflow issue seemed to be the most related to what was happening but I think I'm already following everything the accepted answer suggests.

Do I need to include a COPY or ADD command even though I'm mounting the folder through a volume?

Samuel Cole
  • 521
  • 6
  • 24

1 Answers1

1

The build phase does not see any mounted volumes. The only thing that is available to "build" are the things defined in the backend.docker and in the compose section of build:

    build:
      context: ./
      dockerfile: backend.docker

Do I need to include a COPY or ADD command even though I'm mounting the folder through a volume?

Yes, modify your backend.docker similar to this

...
WORKDIR /var/www
ADD composer.json .
RUN composer install
Artem Titkov
  • 559
  • 3
  • 12