Docker でポート番号を指定して nginx を起動する方法を忘れがちなので自分でメモを作ります。
以下のコマンドです。
docker run -p 8080:80 nginx
正直これだけなんですね。
-p の左側は外部に公開するポート番号、右側はコンテナのポート番号です。
docker run -p 外部に公開するポート番号:コンテナのポート番号 イメージ名
アクセスしてみます。ちゃんと表示されました。
>curl http://localhost:8080/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
body {
width: 35em;
margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
また、-dをつけないとフォアグラウンドで起動してしまうので、バックグラウンドで起動する場合は-dを付与してください。
docker run -d -p 8080:80 nginx
参考にした資料
- Docker 公式ドキュメント:ポートの公開と露出(-p、--expose) https://docs.docker.jp/engine/reference/commandline/run.html#p-expose