LoginSignup
26
21

More than 1 year has passed since last update.

Dockerのexposeとpublishの違い

Last updated at Posted at 2019-03-07

:gear:dockerのexposeとpublishの違いを簡単にまとめます。

publish

publishは、コンテナのportをホストマシンに公開するために利用します。

#docker run --publish ホストマシンのport:コンテナのport image

docker run nignx_image --publish=80:3000

これで、ホストマシンのport80と、containerのport3000をバインドします。

expose

Exposeははポートの公開をするわけではなくドキュメンテーションの役割を果たします。

The EXPOSE instruction informs Docker that the container listens on the >specified network ports at runtime. EXPOSE does not make the ports of >the container accessible to the host.
解説(https://docs.docker.com/engine/reference/builder/#expose)

コンテナ内部で利用されているportのうち、ホストマシンにバインドされる意図のあるportをexposeに記述しておくことで、どのportをpublishすれば良いかが分かりやすくなります。

exposeはコンテナ間でポートを公開すると理解され記述されているケースが多いですが、exposeの指定をせずとも他のコンテナからアクセスは可能です。

Exposeが機能するケース

-Pオプションを利用すると、exposeした全てのportを公開することができます。

docker run nginx_image --expose=80 --expose=443 -P

=> port80と443をdockerのホストマシンに公開

26
21
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
26
21