N
N
nepster-web2017-07-04 13:16:35
PHP
nepster-web, 2017-07-04 13:16:35

How to do composer install inside a docker container?

I ran into a problem:
I need to prepare a ready-made environment during the build and, accordingly, I need the composer install command.
I am using docker compose:

...   
php-bundle:
        container_name: php_bundle
        command: top -b
        build: ./docker/php-bundle
        depends_on:
            - "php"
        working_dir: /www
        volumes:
            - ./www:/www
        networks:
            client:
                ipv4_address: 192.168.110.119
...

And the Dockerfile looks like this:
# https://hub.docker.com/_/php/
# PHP7-CLI
FROM php:7.1-cli

WORKDIR /www


RUN apt-get update -qq \
    && apt-get install -qy --no-install-recommends \
        git \
        openssl \
        librecode0 \
        uuid-dev \
        libmagickwand-dev \
        libsasl2-dev \
        imagemagick \
        libmagickwand-dev \
        libmagickcore-dev \
        libsqlite3-0 \
        libxml2


RUN apt-get update -qq \
    && apt-get install -qy --no-install-recommends \
        autoconf \
        file \
        g++ \
        gcc \
        libc-dev \
        make \
        cmake \
        curl \
        pkg-config \
        libtool \
        tar \
        libmcrypt-dev \
        libpng-dev \
        zip \
        unzip \
        wget


RUN mkdir /var/log/php


RUN apt-get install -y libpq-dev \
    && docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql


RUN docker-php-ext-install \
        gd \
        mcrypt \
        mysqli \
        pdo \
        pdo_pgsql \
        pgsql \
        pdo_mysql \
        mbstring \
        tokenizer \
        opcache \
        exif \
        zip

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- \
        --filename=composer \
        --install-dir=/usr/local/bin && \
        echo "alias composer='composer'" >> /root/.bashrc && \
        composer


# Install phpunit
RUN wget https://phar.phpunit.de/phpunit-6.0.phar && \
        chmod +x phpunit-6.0.phar && \
        mv phpunit-6.0.phar /usr/local/bin/phpunit


# Install codecept
RUN wget http://codeception.com/codecept.phar && \
        chmod +x codecept.phar && \
        mv codecept.phar /usr/local/bin/codecept

RUN composer install --prefer-source --no-interaction

When building, it crashes with an error:
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Composer could not find a composer.json file in /www
To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section

As I understand it, the volume is thrown after the build, and at the time of the composer installation, the www folder is empty.
Tell me, please, how to solve this problem?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexey Ukolov, 2017-07-04
@nepster-web

COPY ./%относительный путь от Dockerfile%/composer.json /www/composer.json
RUN composer install --prefer-source --no-interaction

S
samizdam, 2018-01-17
@samizdam

You can see working examples of using composer in docker in real projects. on github.
Mine for example:
https://github.com/FreeElephants/rest-auth/blob/ma...
https://github.com/FreeElephants/thruway-in-docker...

P
PQR, 2018-01-17
@PQR

To prevent composer from swearing at root/super user, you need to set the environment variable
https://getcomposer.org/doc/03-cli.md#composer-all...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question