简体   繁体   中英

How run composer install from dockerfile in php container

Good day I have php container which seting up in Dockerfile

I am trying to run composer install with this command

WORKDIR '/app'
RUN curl -sS https://getcomposer.org/installer | \
            php -- --install-dir=/usr/bin/ --filename=composer
        CMD bash -c "composer install && php artisan serve --host 0.0.0.0 --port 80"
    EXPOSE 80
    EXPOSE 22
    CMD ["php-fpm"]

But I didnt get the error, and can not run application because of the composer install didnt worked.Just white screen of laravel which mean composer was not installed.

If I run after docker compose up

docker run -it --rm -v $(pwd):/app composer/composer install

I receive an error Composer could not find a composer.json file in /app To initialize a project, please create a composer.json file as described in the https://getcomposer.org/ "Getting Started" section

If I trying to COPY project dirrectory I receive an error

COPY /var/www/docker/project /app

ERROR: Service 'php' failed to build: COPY failed: stat /var/lib/docker/tmp/docker-builder091462376/var/www/docker/project: no such file or directory While I have the volumes in php container in docker-compose.yml

volumes:
            - ${APP_PATH_HOST}:${APP_PATH_CONTAINER}
            - /app/vendor/

Here is my docker-compose.yml

version: '2'
services:
    nginx:
     image: nginx:latest
     container_name: "${PROJECT_NAME}_nginx"
     environment:
       NGINX_STATIC_OPEN_FILE_CACHE: "off"
       NGINX_ERROR_LOG_LEVEL: debug
       NGINX_BACKEND_HOST: php
       NGINX_VHOST_PRESET: php
       NGINX_SERVER_ROOT: /app/public
     ports:
        - "80:80"
        - "443:443"
     volumes:
        - ./hosts:/etc/nginx/conf.d
        - ${APP_PATH_HOST}:${APP_PATH_CONTAINER}
        - ./logs:/var/log/nginx
     links:
        - php

     labels:
       - 'traefik.backend=nginx'
       - 'traefik.port=80'
       - 'traefik.frontend.rule=Host:${PROJECT_BASE_URL}'  
    php:
        build: ./images/php
        links:
            - mariadb
        #command: bash -c 'cd /app composer install'    
        volumes:
            - ${APP_PATH_HOST}:${APP_PATH_CONTAINER}
            - /app/vendor/

    mariadb:
     image: wodby/mariadb:$MARIADB_TAG
     container_name: "${PROJECT_NAME}_mariadb"
     stop_grace_period: 30s
     environment:
      MYSQL_ROOT_PASSWORD: $DB_ROOT_PASSWORD
      MYSQL_DATABASE: $DB_NAME
      MYSQL_USER: $DB_USER
      MYSQL_PASSWORD: $DB_PASSWORD

    pma:
     image: phpmyadmin/phpmyadmin
     container_name: "${PROJECT_NAME}_pma"
     ports:
        - "9000:80"
     environment:
       PMA_HOST: $DB_HOST
       PMA_USER: $DB_USER
       PMA_PASSWORD: $DB_PASSWORD
       PHP_UPLOAD_MAX_FILESIZE: 1G
       PHP_MAX_INPUT_VARS: 1G
     labels:
      - 'traefik.backend=pma'
      - 'traefik.port=80'
      - 'traefik.frontend.rule=Host:pma.${PROJECT_BASE_URL}'

Here is completely Dockerfile

FROM php:7.2-fpm-alpine3.6
WORKDIR '/app'
RUN apk update && apk upgrade\
    && apk add --no-cache curl libbz2 php7-bz2 php7-pdo php7-pgsql php7-bcmath php7-zmq php7-curl bash php7-pear php7-imagick openssh imap-dev\
    libtool \
    postgresql-dev \
    libpng-dev \
    imagemagick-c++ \
    imagemagick-dev \
    libmcrypt-dev \
    libxml2-dev \
    yaml-dev \
    bzip2 \
    aspell-dev \
    autoconf \
    build-base \
    linux-headers \
    libaio-dev \
    zlib-dev \
    git \
    subversion \
    freetype-dev \
    libjpeg-turbo-dev \
    libmcrypt-dev \
    bzip2-dev \
    libstdc++ \
    libxslt-dev \
    openldap-dev \
    hiredis-dev \
    make \
    unzip \
    ffmpeg \
    wget
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN docker-php-ext-configure imap --with-imap --with-imap-ssl \
  && docker-php-ext-install -j 4 imap 


RUN docker-php-ext-install gd bcmath zip bz2 pdo pdo_mysql simplexml opcache sockets mbstring pcntl xsl pspell
RUN docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ 
RUN pecl install imagick
RUN docker-php-ext-enable imagick 
RUN pecl install xdebug
RUN wget http://xdebug.org/files/xdebug-2.6.1.tgz
RUN tar -xvzf xdebug-2.6.1.tgz
RUN cd xdebug-2.6.1 \
    && phpize \
    && ./configure --enable-xdebug \
    && make \
    && make install \
    && cp modules/xdebug.so /usr/local/lib/php/extensions/no-debug-non-zts-20170718 \
    && echo 'zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so' >> /usr/local/etc/php/php.ini \
    && echo 'zend_extension = /usr/local/lib/php/extensions/no-debug-non-zts-20170718/xdebug.so' >> /etc/php7/php.ini \
    && echo 'xdebug.remote_enable=true' >> /etc/php7/php.ini \
    && echo 'xdebug.remote_host=127.0.0.1' >> /etc/php7/php.ini \
    && echo 'xdebug.remote_port=9000' >> /etc/php7/php.ini \
    && echo 'xdebug.remote_handler=dbgp' >> /etc/php7/php.ini \
    && echo 'xdebug.max_nesting_level=512' >> /etc/php7/php.ini

ENV COMPOSER_ALLOW_SUPERUSER 1

RUN echo "Install ZeroMQ library and PHP extension"
RUN apk add --update autoconf gcc libzmq zeromq-dev zeromq coreutils build-base
RUN pecl install zmq-beta \
  && docker-php-ext-enable zmq
#COPY ./project /app
RUN curl -sS https://getcomposer.org/installer | \
    php -- --install-dir=/usr/bin/ --filename=composer
#RUN "composer install"


EXPOSE 80

EXPOSE 22
#CMD php artisan serve --host=0.0.0.0 --port=80
#CMD php artisan migrate --seed

CMD ["php-fpm"]
USER root
copy run.sh /run.sh
RUN chmod +x /run.sh  
RUN ls
#RUN ["chmod", "+x", "/run.sh"]
COPY ./././project app/
RUN composer install --no-dev --no-interaction -o

CMD ["/run.sh"]

It seems you are mixing PHP-FPM and a standalone PHP server. Rather move the artisan serve to the CMD. Also, you can only have one CMD , which is the command that needs to execute when the container starts. In order to execute other commands when building the container, you need to use the RUN command. So your Dockerfile can look like this:

WORKDIR '/app'
RUN curl -sS https://getcomposer.org/installer | \
            php -- --install-dir=/usr/bin/ --filename=composer

RUN "composer install"
EXPOSE 80
EXPOSE 22
CMD "php artisan serve --host 0.0.0.0 --port 80"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM