16
7

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.

docker-compose.yml で $ を使いたい場合は $$ にすればいいというはなし

Posted at

タイトルで全部言い切っちゃってますけど、 docker-compose.ymlcommand のところなんかで $ を使いたいケースってままあると思うんですよ。

例えば以下のようにみたいに環境変数渡したりとか。

command: '$HOGE=hoge something'

ただこれ動かなくて、たぶんコマンド実行時には =hoge something って解釈されちゃうんですね。
で、何となく $$ にしたら動いたのですが、これが正しいソリューションなのかよく分かってなかったんですが、ちゃんとドキュメントに書いてあったので、以下のように $$ にするが正解でよさそうでした :ok_woman:

command: '$$HOGE=hoge something'

ドキュメント

Compose File Reference - Variable substitution
https://docs.docker.com/compose/compose-file/#variable-substitution

(日本語は拙訳)
Both $VARIABLE and ${VARIABLE} syntax are supported. Extended shell-style features, > such as ${VARIABLE-default} and ${VARIABLE/foo/bar}, are not supported.
$VARIABLE${VARIABLE} の両方がサポートされています。
${VARIABLE-default} や ${VARIABLE/foo/bar} といった拡張シェルスタイル機能はサポートされていません。

You can use a $$ (double-dollar sign) when your configuration needs a literal dollar sign. This also prevents Compose from interpolating a value, so a $$ allows you to refer to environment variables that you don’t want processed by Compose.
$リテラルをコンフィグに記載したい場合、$$ (ダブルダラーサイン)を利用することができます。
$$ を利用することで、docker-compose による値の補完から防ぐことができ、あなたが利用したい環境変数の値を指し示すことができるようになります。

web:
  build: .
  command: "$$VAR_NOT_INTERPOLATED_BY_COMPOSE"

If you forget and use a single dollar sign ($), Compose interprets the value as an environment variable and will warn you:
もし誤って $ を利用した場合、 docker-compose はをれを環境変数だと判断し、次のようなメッセージを表示するでしょう。

The VAR_NOT_INTERPOLATED_BY_COMPOSE is not set. Substituting an empty string.

とのことですので、気兼ねなく $$ していきましょう。

16
7
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
16
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?