まめぞうの技術メモ

IT関連で勉強したことをメモします

Docker で nginx を起動する方法【ポート番号指定】

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

参考にした資料