3

I'm pretty new with docker, I try to automatically execute composer install within my Dockerfile but it seems that I can't cd into my application while installing, what's wrong? Or maybe there is another better way to do that?

my docker-compose.yml

version: "3.1"
services:

    app:
      image: nginx:alpine
      container_name: app
      working_dir: /application
      volumes:
          - ./Projects/app:/application/app
          - ./Docker/nginx/app.conf:/etc/nginx/conf.d/app.conf
      ports:
          - "8080:8080"

    php-fpm-app:
      build: Docker/php-fpm-app
      container_name: php-fpm-app
      working_dir: /application
      volumes:
          - ./Projects:/application
          - ./Docker/php-fpm-app/php-ini-overrides.ini:/etc/php/7.2/fpm/conf.d/99-overrides.ini

my Dockerfile

FROM phpdockerio/php72-fpm:latest
WORKDIR "/application"

# Fix debconf warnings upon build
ARG DEBIAN_FRONTEND=noninteractive

RUN mkdir -p /var/www/.composer \
    && chown -R www-data:www-data /var/www/.composer

USER www-data

RUN cd /application/app; composer install

The output after I run this command:

docker-compose up -d

Step 6/6 : RUN cd /application/app; composer install
 ---> Running in ac53e653af46
/bin/sh: 1: cd: can't cd to /application/app
Composer could not find a composer.json file in /application
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section
ERROR: Service 'php-fpm-app' failed to build: The command '/bin/sh -c cd /application/app; composer install' returned a non-zero code: 1

If I try to remove the last line from my Dockerfile, once it's up and running if I run this command:

docker-compose exec --user www-data php-fpm-app bash -c 'cd /application/app && composer install'

It works, I don't understand why I can't do this with my Dockerfile.

=========================== Finally I find a way to execute a script but I don't see the output, so if the script last for many secondes/minutes I won't know when it's done.

ADD ./setup.sh /setup.sh
RUN chmod +x /setup.sh
CMD ["sh", "/setup.sh"]

I decided to execute this script manually once all it's up and running

sh ./setup.sh
RaKoDev
  • 613
  • 2
  • 7
  • 22
  • omit the "composer install" part for once, and log in to the container (once its up and running), and manually try to issue composer install inside the container. – cooshal Sep 05 '18 at 13:35
  • Yes, it works if I do it once its up and running – RaKoDev Sep 05 '18 at 13:40
  • the question is not about the existence of composer.json. it is there. but your error is that it cannot cd into /application/app, and since your WORKDIR is /application, it fall backs to this directory, and thus if you look into your error, it says that it cannot find composer.json in /application `/bin/sh: 1: cd: can't cd to /application/app Composer could not find a composer.json file in /application` – cooshal Sep 05 '18 at 13:48
  • and instead of WORKDIR /application, may be try setting it to /application/app, and remove `RUN cd /application/app` and just do `RUN composer install` – cooshal Sep 05 '18 at 13:51
  • I already tried this solution but it seems that it didn't find the files before it's up and running – RaKoDev Sep 05 '18 at 14:04
  • Possible duplicate of [How to mount host volumes into docker containers in Dockerfile during build](https://stackoverflow.com/questions/26050899/how-to-mount-host-volumes-into-docker-containers-in-dockerfile-during-build) – David Maze Sep 05 '18 at 14:08
  • The “volume” directives in the `docker-compose.yml` file have no effect on the image build; you need to COPY your application code in. (At which point mounting the volume over it is kind of redundant, if you don’t mind a workflow where you need to test-build-run after making changes.) – David Maze Sep 05 '18 at 14:10
  • Maybe there is a way of running some command or script once it's up? – RaKoDev Sep 05 '18 at 14:47

3 Answers3

5

You should not run composer install in Dockerfile. It will fail because you have not created/synced volume from the local to container yet. Thus all files including composer.json is not placed under /var/www/html.

What you can do is to add 1 line (command) into your docker-compose.yaml file inside the service eg.

services:
  api-php-fpm:
    build: ./docker/php-fpm
    command: sh -c "composer install"
İsmail Atkurt
  • 1,360
  • 1
  • 12
  • 17
0

This is because the volumes aren't mounted until the build is complete. Just use COPY to copy only the composer.json into the folder, and run the composer install as normal.

Pobtastic
  • 262
  • 2
  • 9
-3

Referencing the Documentation for Dockerfiles you need to edit your Dockerfile.

it should be:

WORKDIR /application/app
RUN composer install

Does this fix your problem?

Markus Mauksch
  • 397
  • 1
  • 9
  • Step 6/6 : RUN composer install ---> Running in 9821f69e3ce0 Composer could not find a composer.json file in /application/app But there is a composer.json file – RaKoDev Sep 05 '18 at 13:15