【问题标题】:How To Install PHP Composer via Docker And Use As If It Was Installed Locally?如何通过 Docker 安装 PHP Composer 并像在本地安装一样使用?
【发布时间】:2019-01-18 21:13:07
【问题描述】:

我正在尝试通过 Docker 安装 PHP Composer,以便能够像本地安装在我的主机 (MacOS) 上一样运行 composer。

FROM php:7.2.13-apache

# install supporting packages
RUN apt-get update && apt-get install -y --fix-missing \
    xz-utils \
    build-essential \
    pkg-config \
    git-core \
    autoconf \
    libjpeg62-turbo-dev \
    libsodium-dev \
    libpng-dev \
    libcurl4-openssl-dev \
    libpq-dev \
    libpspell-dev \
    libsqlite3-dev \
    libmagickwand-dev \
    libzip-dev \
    imagemagick \
    subversion \
    python \
    g++ \
    curl \
    vim \
    wget \
    netcat \
    chrpath

# install officially supported php extensions
RUN docker-php-ext-install \
    iconv \
    sodium \
    opcache \
    curl \
    gd \
    mysqli \
    exif \
    mbstring \
    pdo \
    pdo_pgsql \
    pdo_mysql \
    pdo_sqlite \
    pspell \
    pgsql \
    soap \
    zip \
    && docker-php-ext-configure zip --with-libzip \
    && docker-php-ext-install zip

# PECL modules
RUN pecl install imagick \
    && pecl install xdebug-2.6.0

COPY ./xdebug/xdebug.ini /usr/local/etc/php/conf.d/ 

# Enable PECL modules
RUN docker-php-ext-enable imagick xdebug

# cleanup apt
RUN apt-get clean
RUN apt-get autoremove -y

# enable apache modules
RUN a2enmod rewrite headers cache cache_disk expires vhost_alias userdir autoindex
RUN service apache2 restart
RUN service apache-htcacheclean start

RUN usermod -u 1000 www-data
RUN usermod -G staff www-data
RUN chown -R www-data:www-data /var/www

RUN echo "memory_limit=-1" > "$PHP_INI_DIR/conf.d/memory-limit.ini" \
 && echo "date.timezone=${PHP_TIMEZONE:-UTC}" > "$PHP_INI_DIR/conf.d/date_timezone.ini"

RUN apk add --no-cache --virtual .build-deps zlib-dev libzip-dev \
 && docker-php-ext-configure zip --with-libzip \
 && docker-php-ext-install zip \
 && runDeps="$( \
    scanelf --needed --nobanner --format '%n#p' --recursive /usr/local/lib/php/extensions \
    | tr ',' '\n' \
    | sort -u \
    | awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
    )" \
 && apk add --virtual .composer-phpext-rundeps $runDeps \
 && apk del .build-deps

ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /tmp
ENV COMPOSER_VERSION 1.8.0

RUN curl --silent --fail --location --retry 3 --output /tmp/installer.php --url https://raw.githubusercontent.com/composer/getcomposer.org/b107d959a5924af895807021fcef4ffec5a76aa9/web/installer \
 && php -r " \
    \$signature = '544e09ee996cdf60ece3804abc52599c22b1f40f4323403c44d44fdfdd586475ca9813a858088ffbc1f233e9b180f061'; \
    \$hash = hash('SHA384', file_get_contents('/tmp/installer.php')); \
    if (!hash_equals(\$signature, \$hash)) { \
        unlink('/tmp/installer.php'); \
        echo 'Integrity check failed, installer is either corrupt or worse.' . PHP_EOL; \
        exit(1); \
    }" \
 && php /tmp/installer.php --no-ansi --install-dir=/usr/bin --filename=composer --version=${COMPOSER_VERSION} \
 && composer --ansi --version --no-interaction \
 && rm -rf /tmp/* /tmp/.htaccess

RUN chmod 700 /usr/bin/composer

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

WORKDIR /var/www

CMD ["apache2-foreground"]

EXPOSE 80

docker-entrypoint.sh 具有以下代码 (from official composer docker page):

#!/usr/bin/env bash

isCommand() {
  for cmd in \
    "about" \
    "archive" \
    "browse" \
    "check-platform-reqs" \
    "clear-cache" \
    "clearcache" \
    "config" \
    "create-project" \
    "depends" \
    "diagnose" \
    "dump-autoload" \
    "dumpautoload" \
    "exec" \
    "global" \
    "help" \
    "home" \
    "info" \
    "init" \
    "install" \
    "licenses" \
    "list" \
    "outdated" \
    "prohibits" \
    "remove" \
    "require" \
    "run-script" \
    "search" \
    "self-update" \
    "selfupdate" \
    "show" \
    "status" \
    "suggests" \
    "update" \
    "upgrade" \
    "validate" \
    "why" \
    "why-not"
  do
    if [ -z "${cmd#"$1"}" ]; then
      return 0
    fi
  done

  return 1
}

# check if the first argument passed in looks like a flag
if [ "$(printf %c "$1")" = '-' ]; then
  set -- /sbin/tini -- composer "$@"
# check if the first argument passed in is composer
elif [ "$1" = 'composer' ]; then
  set -- /sbin/tini -- "$@"
# check if the first argument passed in matches a known command
elif isCommand "$1"; then
  set -- /sbin/tini -- composer "$@"
fi

exec "$@"

docker-compose.yml 的代码如下:

version: '3'
services:

    php:
       container_name: local_php
       build: .
       image: php:7.2.13-apache
       ports:
         - "80:80"
         - "443:443"
         - "9001:9001"
       volumes:
         - ../:/var/www/html/

我希望能够从终端运行 composer 命令(例如composer install),但每次我在构建 Dockerfile 后尝试运行任何 composer 命令时都会收到错误 -bash: composer: command not found

【问题讨论】:

  • 你能解释一下为什么你想这样做吗?我看不到任何用例
  • 这样我就不必在我的主机上安装 composer 了
  • 所以,你不想在本地安装composer,但是PHP?为什么不将两者都放入容器中?

标签: php docker composer-php dockerfile


【解决方案1】:

我终于想通了;如果有人试图达到同样的效果,您基本上有两种选择:

  1. 访问 php 容器 bash(通过使用 docker exec -it <container name> /bin/bash 并导航到您希望使用 composer 的文件夹,然后运行 ​​composer 命令。注意 docker-compose run <container name> ls 将显示容器的已安装卷(如果您想检查/确保)。
  2. 在 macOS 终端中运行以下命令,就可以像在本地安装一样使用 composer:
#!/bin/bash
mkdir ~/.functions
echo '#!/bin/bash
tty=
tty -s && tty=--tty
docker run \
    $tty \
    --interactive \
    --rm \
    --user $(id -u):$(id -g) \
    --workdir /var/www/html/${PWD##*/} \
    --volume /etc/passwd:/etc/passwd:ro \
    --volume /etc/group:/etc/group:ro \
    --volume $(pwd):/var/www/html/${PWD##*/} \
    composer "$@"' > ~/.functions/composer
echo 'alias composer="sh ~/.functions/composer"' >> ~/.bash_profile
source ~/.bash_profile

然后在终端尝试composer version 看看它是否工作;如果没有,请尝试再次执行source ~/.bash_profile,然后重试。

希望有帮助!

【讨论】:

    猜你喜欢
    • 2021-08-15
    • 2018-12-02
    • 2017-09-30
    • 1970-01-01
    • 2018-12-16
    • 2020-11-25
    • 2018-08-20
    • 2018-12-05
    • 2019-08-19
    相关资源
    最近更新 更多