我正在尝试在我的 Dockerfile 中安装 Composer。图像构建成功,服务器运行,但是当我启动容器 bash 并运行composer --version 时,我收到“命令未找到”消息。我做错了什么?我很绝望。我检查了更多如何添加作曲家的可能性,不幸的是,没有什么对我真正有用..
这是 dockerfile
# stage 1: build stage
FROM php:8.3-fpm-alpine as build
# installing system dependencies and php extensions
RUN apk add --no-cache \
zip \
libzip-dev \
freetype \
libjpeg-turbo \
libpng \
freetype-dev \
libjpeg-turbo-dev \
libpng-dev \
nodejs \
npm \
&& docker-php-ext-configure zip \
&& docker-php-ext-install zip pdo pdo_mysql \
&& docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-enable gd
# install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/bin --filename=composer
# Verify Composer installation
RUN composer --version
WORKDIR /var/www/html
# copy necessary files and change permissions
COPY . .
RUN chown -R www-data:www-data /var/www/html \
&& chmod -R 775 /var/www/html/storage \
&& chmod -R 775 /var/www/html/bootstrap/cache
# install php and node.js dependencies
RUN composer install --no-dev --prefer-dist \
&& npm install \
&& npm run build
RUN chown -R www-data:www-data /var/www/html/vendor \
&& chmod -R 775 /var/www/html/vendor
# stage 2: production stage
FROM php:8.3-fpm-alpine
# install nginx
RUN apk add --no-cache \
zip \
libzip-dev \
freetype \
libjpeg-turbo \
libpng \
freetype-dev \
libjpeg-turbo-dev \
libpng-dev \
oniguruma-dev \
gettext-dev \
freetype-dev \
nginx \
&& docker-php-ext-configure zip \
&& docker-php-ext-install zip pdo pdo_mysql \
&& docker-php-ext-configure gd --with-freetype=/usr/include/ --with-jpeg=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-enable gd \
&& docker-php-ext-install bcmath \
&& docker-php-ext-enable bcmath \
&& docker-php-ext-install exif \
&& docker-php-ext-enable exif \
&& docker-php-ext-install gettext \
&& docker-php-ext-enable gettext \
&& docker-php-ext-install opcache \
&& docker-php-ext-enable opcache \
&& rm -rf /var/cache/apk/*
# copy files from the build stage
COPY --from=build /var/www/html /var/www/html
COPY ./deploy/nginx.conf /etc/nginx/http.d/default.conf
COPY ./deploy/php.ini "$PHP_INI_DIR/conf.d/app.ini"
WORKDIR /var/www/html
# add all folders where files are being stored that require persistence. if needed, otherwise remove this line.
VOLUME ["/var/www/html/storage/app"]
CMD ["sh", "-c", "nginx && php-fpm"]
问题
/var/www/html # composer --version
sh: composer: not found
您正在
composer
阶段安装 build
,但未复制到您的 stage 2: production stage
。
考虑以下形式的内容 after
FROM php:8.3-fpm-alpine
COPY --from=build /usr/bin/composer /usr/local/bin/composer
你的
RUN
陈述......不寻常,但我假设它是正确的:
CMD ["sh", "-c", "nginx && php-fpm"]