3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

composerのバージョンが変わっても対応可能なDockerfileの書き方

Last updated at Posted at 2019-09-04

問題点

DockerでPHPの環境を構築するときに、Dockerfileにcomposerのインストールコマンドを記載することがあるが、
そのときにhttps://getcomposer.org/download/ に書いてあるインストールコマンドをコピペすると、

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
      && php -r "if (hash_file('sha384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
      && php composer-setup.php \
      && php -r "unlink('composer-setup.php');"

最新バージョンのハッシュ値が変わった時、以下のようなエラーが出て怒られる。

Could not open input file: composer-setup.php
ERROR: Service 'php' failed to build: The command '/bin/sh -c php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"   && php -r "if (hash_file('SHA384', 'composer-setup.php') === '48e3236262b34d30969dca3c37281b3b4bbe3221bda826ac6a9a62d6444cdb0dcd0615698a5cbe587c3f0fe57a54d8f5') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"   && php composer-setup.php   && php -r "unlink('composer-setup.php');"   && mv composer.phar /usr/local/bin/composer' returned a non-zero code: 1

解決策

常に最新のバージョンを取得したい場合

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
      && php -r "if (hash_file('SHA384', 'composer-setup.php') === trim(file_get_contents('https://composer.github.io/installer.sig'))) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
      && php composer-setup.php\
      && php -r "unlink('composer-setup.php');" \
      && mv composer.phar /usr/sbin/composer

常に特定のバージョンを指定したい場合

一つのDockerfileをチームで共有し、共通のローカル環境で作業するために、バージョンを固定したい場合など……
--version=1.8.3の部分にインストールするバージョンを当てはめてください。

RUN php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" \
      && php -r "if (hash_file('SHA384', 'composer-setup.php') === trim(file_get_contents('https://composer.github.io/installer.sig'))) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" \
      && php composer-setup.php --version=1.8.3 \
      && php -r "unlink('composer-setup.php');" \
      && mv composer.phar /usr/sbin/composer
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?