0
0

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 1 year has passed since last update.

2022-08-11 php のイメージで docker run する際に、環境変数を受け渡そうとするとエラーが出る

Last updated at Posted at 2022-08-12

環境

$ docker --version
Docker version 20.10.17, build 100c701
$ cat /etc/os-release | head -n 2
NAME="Ubuntu"
VERSION="20.04.4 LTS (Focal Fossa)"

発生した問題

PHP のコンテナに対し、 docker run php:8.0-cli --env-file .env コマンドで環境変数ファイルを引き渡して実行すると、以下のようにエラー出力がされる。

$ docker run php:8.0-cli --env-file .env
Error in argument 1, char 1: no argument for option -
Usage: php [options] [-f] <file> [--] [args...]
   php [options] -r <code> [--] [args...]
   php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
   php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
   php [options] -S <addr>:<port> [-t docroot] [router]
   php [options] -- [args...]
   php [options] -a

  -a           	Run as interactive shell
  -c <path>|<file> Look for php.ini file in this directory
  -n           	No configuration (ini) files will be used
  -d foo[=bar] 	Define INI entry foo with value 'bar'
  -e           	Generate extended information for debugger/profiler
  -f <file>    	Parse and execute <file>.
... (以下略)

原因

引数の位置に誤り。

docker run php:8.0-cli --env-file .env

ではなく

docker run --env-file .env php:8.0-cli 

と実行すべきだった。

$ docker run --env-file .env php:8.0-cli
Interactive shell

php >

詳細

--env-file 以降に引き渡した引数が、php コマンドの引数として処理されたために、今回の事象が発生した。

おそらく、 ENTRYPOINT として定義された php コマンドの引数として処理されている。

なお、今回の事象について php の Docker イメージ側に悪い点はなく、悪いのは使い方を誤った私である。

後書き

こういうのって、なかなか自分では気づけない。

昔うまくいったことがあったので、 history でどのように実行していたかを確認し、ようやくコマンドの誤りに気がついた。

余談

元々自作のアプリケーションをビルドし、ビルドしたイメージをコンテナとして実行しようとした際に発生。

ビルドするイメージのベースイメージを同様に実行してみて、事象が同様に発生するかを確認することは、切り分けに役立った。

$ docker build . -t app && docker run app:latest --env-file .env
Successfully built 9f475b0e52d3
Successfully tagged app:latest
Usage: php [-n] [-e] [-h] [-i] [-m] [-v] [-t] [-p <prefix>] [-g <pid>] [-c <file>] [-d foo[=bar]] [-y <file>] [-D] [-F [-O]]
  -c <path>|<file> Look for php.ini file in this directory
  -n           	No php.ini file will be used
  -d foo[=bar] 	Define INI entry foo with value 'bar'
  -e           	Generate extended information for debugger/profiler
  -h           	This help
  -i           	PHP information
  -m           	Show compiled in modules
  -v           	Version number
  -p, --prefix <dir>
               	Specify alternative prefix path to FastCGI process manager (default: /usr/local).
  -g, --pid <file>
               	Specify the PID file location.
  -y, --fpm-config <file>
               	Specify alternative path to FastCGI process manager config file.
  -t, --test   	Test FPM configuration and exit
  -D, --daemonize  force to run in background, and ignore daemonize option from config file
  -F, --nodaemonize
               	force to stay in foreground, and ignore daemonize option from config file
  -O, --force-stderr
               	force output to stderr in nodaemonize even if stderr is not a TTY
  -R, --allow-to-run-as-root
               	Allow pool to run as root (disabled by default)
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?