Composer not working in docker container: “https:/

2019-07-14 04:47发布

问题:

I have a docker container that's running a Symfony application (which works fine). Composer install/require commands (e.g. composer require annotations) inside the container often fail with the following error:

[Seld\JsonLint\ParsingException]                                        
  "https://packagist.org/packages.json" does not contain valid JSON       
  Parse error on line 1:                                                  
  \\\\\\u��n[7
                ���                                                    
  ^                                                                       
  Expected one of: 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '['

I haven't really been able to find a pattern on when the commands succeed or not, it seems completely random. I feel like it might be a cache/networking thing, as it usually works for a while and then stops working for a while, but I'm not sure. Composer commands work fine on the host system (Ubuntu 17.10 / 18.04 - thought the update might help but doesn't make a difference). All other things composer in the container work fine. The result is the same whether I open a shell in the container and run the command there or run the command via docker-compose exec.

Here's my Dockerfile:

FROM alpine:3.6

RUN apk add --update \
    php7-fpm \
    php7-apcu \
    php7-ctype \
    php7-curl \
    php7-dom \
    php7-gd \
    php7-iconv \
    php7-imagick \
    php7-json \
    php7-intl \
    php7-mcrypt \
    php7-mbstring \
    php7-opcache \
    php7-openssl \
    php7-pdo \
    php7-pdo_mysql \
    php7-mysqli \
    php7-xml \
    php7-zlib \
    php7-phar \
    php7-tokenizer \
    php7-session \
    php7-xmlwriter \
    php7-simplexml \
    make \
    curl

RUN rm -rf /var/cache/apk/* && rm -rf /tmp/*

RUN curl --insecure https://getcomposer.org/composer.phar -o /usr/bin/composer && chmod +x /usr/bin/composer

ADD symfony.ini /etc/php7/php-fpm.d/
ADD symfony.ini /etc/php7/cli/conf.d/

ADD symfony.pool.conf /etc/php7/php-fpm.d/

CMD ["php-fpm7", "-F"]

WORKDIR /var/www/symfony
EXPOSE 9000

I run it via docker-compose, which mounts a volume to /var/www/symfony and exposes the port:

version: '2.1'

services:
  php:
    build: .
    volumes:
      - ./symfony:/var/www/symfony
    ports:
      - 9000:9000

How do I get this to work? Any setting I don't know of that I need to activate for composer to be able to connect properly?

回答1:

You can add this to your DockerFile :

RUN composer config --global repo.packagist composer https://packagist.org


回答2:

You should add to the Dockerfile as well

RUN apk update && apk add ca-certificates && update-ca-certificates && apk add openssl

and after installing the composer

RUN composer self-update