DockerでNginxしたい
2015/07/03
Dockerfileを用意する
1 2 3 4 5 6 7 8 9 10 |
FROM ubuntu:latest MAINTAINER nihontaro RUN apt-get update RUN apt-get install -y nginx EXPOSE 22 80 ENTRYPOINT ["/usr/sbin/nginx","-g","daemon off;"] |
やってることは単純でnginxをインストールして22,80ポートを開放して、run時にENTRYPOINTに書いてる処理を実行する。
なんでデーモンをオフ(フォアグラウンド)にして起動してるのか?
Dockerはrunするときに指定したコマンドがフォアグラウンドで実行されないとexitする。
で、Nginxはデフォルトでバックグラウンド起動する。
なので、明示的にフォアグラウンド起動にしてDockerのコンテナが生存するようにしている。
Dockerfileからイメージをビルド
1 |
docker build -t nihontaro/base:latest . |
.はDockerfileの場所を指定している。
イメージをrun
1 |
docker run -d -p 80:80 nihontaro/base:latest |
dockerをバックグラウンドでrunしてる。
先に書いたDockerfileでEXPOSE 80とあったのでコンテナの80ポートは開放されている。
で、ホスト側の80ポートをコンテナの80ポートに委譲したいので-p 80:80となる。
ホスト側の9999ポートをコンテナの80ポートに委譲したいなら-p 9999:80となる。
アクセスできるか確認
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
hogehuga@moxt:~/thick# curl localhost <!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> |
nginxと導通確認できた。