gpt4 book ai didi

php - 如何从php容器中的dockerfile运行composer安装

转载 作者:行者123 更新时间:2023-12-02 20:01:39 25 4
gpt4 key购买 nike

好的,我有在Dockerfile中设置的php容器

我正在尝试使用此命令运行composer install

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"]

但是我没有得到错误,并且由于composer的安装没有起作用而无法运行应用程序.laravel的白屏意味着没有安装composer。

如果我在Docker组成之后运行
docker run -it --rm -v $(pwd):/app composer/composer install

我收到一个错误
Composer 在/ app中找不到composer.json文件
要初始化项目,请按照 https://getcomposer.org/“入门”部分中的描述创建composer.json文件。

如果我尝试复制项目目录,则会收到错误消息
COPY /var/www/docker/project /app

错误:服务'php'构建失败:复制失败:stat / var / lib / docker / tmp / docker-builder091462376 / var / www / docker / project:没有这样的文件或目录
虽然我在docker-compose.yml中的php容器中有卷
volumes:
- ${APP_PATH_HOST}:${APP_PATH_CONTAINER}
- /app/vendor/

这是我的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}'

这是完全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"]

最佳答案

看来您正在混合使用PHP-FPM和独立的PHP服务器。
而是将artisan serve移动到CMD。
另外,您只能有一个CMD,这是在容器启动时需要执行的命令。为了在构建容器时执行其他命令,您需要使用RUN命令。因此,您的Dockerfile可能如下所示:

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"

关于php - 如何从php容器中的dockerfile运行composer安装,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53079103/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com