Docker Community Forums

Share and learn in the Docker community.

Troubles running composer install from a dockerfile

I got a symfony app running with nginx, php-fpm and postgres on docker.I use docker-compose to set the services and i got a dockerfile for my php service in which i install the composer with success but then i wanna do a composer install and it’s somehow tricky. I cannot simply put the composer install there because i do not have the app yet.Yes i can use COPY and then RUN composer install which will work, but since i got volumes on this service in docker-compose, it will just be overwritten.
So another solution would be to use ENTRYPOINT /docker-entrypoint.sh but i couldn’t make it work.
I copy the sh with the composer install command but i cannot point it to the app folder, it gives the error:

[RuntimeException] directory specified, =/var/www/app does not exist.

I can’t manage to copy the .sh file into the app folder and thats why i use the -d argument of the composer.My docker config files are inside a docker folder in my app folder. That’s why i use …/ at the context in docker-compose.

docker-compose.yml:

version: '3.7'
services:
  postgres:
    image: postgres:alpine
    restart: always
    environment:
      POSTGRES_PASSWORD: ${DB_PASSWORD}
      POSTGRES_USER: ${DB_USER}
      POSTGRES_DB: ${DB_NAME}
    ports:
      - ${DB_PORT}:5432
    volumes:
      - pgdata:/var/lib/postgresql/data
  php:
    build:
      context: ../
      dockerfile: ./docker/php/Dockerfile
    restart: always
    volumes:
      - ../:/var/www/app
    depends_on:
      - postgres
  nginx:
    image: nginx:1.15.3-alpine
    restart: always
    volumes:
      - ../:/var/www/app
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    ports:
      - ${NGINX_PORT}:80
    depends_on:
      - php
volumes:
  pgdata:

Dockerfile:

# ./docker/php/Dockerfile
FROM php:7-fpm-alpine

RUN apk update && apk add build-base

RUN apk add postgresql postgresql-dev \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \
    && docker-php-ext-install pdo pdo_pgsql pgsql \
    && docker-php-ext-install opcache \
    && docker-php-ext-enable opcache

RUN apk add libzip-dev zlib-dev git zip \
  && docker-php-ext-install zip

RUN apk add bash

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

COPY ./docker/php/opcache.ini /usr/local/etc/php/conf.d/opcache.ini
COPY ./docker/php/php.ini /usr/local/etc/php/conf.d/php.ini
EXPOSE 9000

COPY ./docker/php/docker-entrypoint.sh /docker-entrypoint.sh

ENTRYPOINT /docker-entrypoint.sh

docker-entrypoint.sh

composer install -d=/var/www/app

I’m not familiar with composer, so apply an appropriate filter.

Given this error

=/var/www/app does not exist.

I have to wonder if the syntax for the entrypoint might be

composer install -d /var/www/app

i tried them both… i also tried to simply run a sh from the root of the project by adding a command: sh something.sh on my php service, yet it doesn’t work, is like it doesn’t recognize the ‘composer install’ statement, but if i /bin/sh into the container and manually run it, everything works