コード日進月歩

しんくうの技術的な小話、メモ、つれづれ、など

DockerfileのEXPOSEは実際に何かポートを開けているわけではない

盲目的に書いていたけどそうなんだ、と思ったのでメモ

環境

$ docker -v
Docker version 18.06.1-ce, build e68fc7a

出典

Docker Documentation のEXPOSEの項目に下記のような記載がある

The EXPOSE instruction does not actually publish the port. It functions as a type of documentation between the person who builds the image and the person who runs the container, about which ports are intended to be published. To actually publish the port when running the container, use the -p flag on docker run to publish and map one or more ports, or the -P flag to publish all exposed ports and map them to high-order ports.

Google翻訳駆使すると以下の感じ。

EXPOSE命令は実際にはポートを公開しません。それは、どのポートが公開されることを意図しているかについて、イメージを作成する人とコンテナを実行する人との間の一種のドキュメンテーションとして機能します。コンテナの実行時に実際にポートを公開するには、docker runで-pオプションを使用して1つ以上のポートを公開してマップするか、-Pオプションを使用してすべての公開ポートを公開してそれらを高位ポートにマップします。

-p-P を使えとのこと。 というか -P で全ポート公開は知らなかった。

関連リンク

Docker Documentation - Docker run reference