LoginSignup
20
24

More than 5 years have passed since last update.

Dockerコンテナ上にnginxを構築する

Last updated at Posted at 2016-02-23

この記事は「Dockerコンテナ上に開発環境一式を構築する」の一部です。

現在のステータス

方式

ホスト複数台で運用していた時は、nginxにロードバランスさせるつもりでした。今回一台しかありませんが備忘録代わりに設定します。nginxでネームバーチャルホストを設定して、リバースプロキシでGitBucketとJenkinsを外部公開します。この方式であればしておけば、他のWebサービスを立ち上げても外部ポートは80だけで済みます。

設定ファイル

まずnginxの設定ファイルを用意します。デフォルトだと1MBまでしかアップロードできないので50MBに拡張します。

nginx.conf
        # gitbucket file size max
        client_max_body_size 50m;

追加するのはこの行だけですが、イメージに焼きこむためにファイル全体を用意します。

nginx.conf
 user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 768;
        # multi_accept on;
}

http {

        ##
        # Basic Settings
        ##

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;
        # server_tokens off;

        # server_names_hash_bucket_size 64;
        # server_name_in_redirect off;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        ##
        # Logging Settings
        ##

        access_log /var/log/nginx/access.log;
        error_log /var/log/nginx/error.log;

        ##
        # Gzip Settings
        ##

        gzip on;
        gzip_disable "msie6";

        # gzip_vary on;
        # gzip_proxied any;
        # gzip_comp_level 6;
        # gzip_buffers 16 8k;
        # gzip_http_version 1.1;
        # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

        ##
        # nginx-naxsi config
        ##
        # Uncomment it if you installed nginx-naxsi
        ##

        #include /etc/nginx/naxsi_core.rules;

        ##
        # nginx-passenger config
        ##
        # Uncomment it if you installed nginx-passenger
        ##

        #passenger_root /usr;
        #passenger_ruby /usr/bin/ruby;

        ##
        # Virtual Host Configs
        ##

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;
}


#mail {
#       # See sample authentication script at:
#       # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#
#       # auth_http localhost/auth.php;
#       # pop3_capabilities "TOP" "USER";
#       # imap_capabilities "IMAP4rev1" "UIDPLUS";
#
#       server {
#               listen     localhost:110;
#               protocol   pop3;
#               proxy      on;
#       }
#
#       server {
#               listen     localhost:143;
#               protocol   imap;
#               proxy      on;
#       }
#}

daemon off;

次にsitesディレクトリを作り、バーチャルホストの設定ファイルを作成します。

$ mkdir sites
jenkins.org
upstream jenkins {
        ip_hash;
        server $IP:8081 max_fails=3 fail_timeout=30s ;
        #server 127.0.0.1:80  down;
}

server {
        server_name                             jenkins jenkins.lavans.jp;
        proxy_set_header Host                   $host;
        proxy_set_header X-Forwarded-For        $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host       $host;
        proxy_set_header X-Forwarded-Server     $host;
        location / {
                proxy_pass http://jenkins;
        }
}
gitbucket.org
upstream gitbucket {
        ip_hash;
        server $IP:8080 max_fails=3 fail_timeout=30s ;
        #server 127.0.0.1:80  down;
}

server {
        server_name                             gitbucket gitbucket.lavans.jp;
        proxy_set_header Host                   $host;
        proxy_set_header X-Forwarded-For        $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Host       $host;
        proxy_set_header X-Forwarded-Server     $host;
        location / {
                proxy_pass http://gitbucket;
        }
}

setenv.shを使ってIPを設定します。

. ../../setenv.sh
sed -e s/\$IP/$IP/g gitbucket.org > gitbucket
sed -e s/\$IP/$IP/g jenkins.org > jenkins

Dockerfile

これをDockerイメージに入れます。
※コメント頂いたので修正しました。他の記事も修正します。

Dockerfile
# Nginx Dockerfile
FROM ubuntu:14.04
ENV DEBIAN_FRONTEND noninteractive

# Install Nginx.
RUN apt-get update && apt-get -y upgrade \
 && apt-get install -y software-properties-common \
 && add-apt-repository -y ppa:nginx/stable \
 && apt-get install -y nginx \
 && rm -rf /var/lib/apt/lists/* \
RUN echo "\ndaemon off;" >> /etc/nginx/nginx.conf
RUN chown -R www-data:www-data /var/lib/nginx

# Setup virtual hosts.
ADD sites /etc/nginx/sites-available/
RUN ln -s /etc/nginx/sites-available/gitbucket /etc/nginx/sites-enabled/
RUN ln -s /etc/nginx/sites-available/jenkins /etc/nginx/sites-enabled/
ADD nginx.conf /etc/nginx/

# Default Command
CMD ["nginx"]

「DEBIAN_FRONTEND=noniteractive」って「apt-get -y」したら要らないんですね。でも記述を統一しておけばdockerのキャッシュが効くので残しておきます。

起動終了スクリプト

/home/docker/setenv.shのホストブリッジIPをdocker runの--dnsオプションで指定して起動します。

setenv.sh
DOCKER_HOME=/home/docker
IP=172.17.42.1
#HTTP_PROXY=http://host:port
#HTTPS_PROXY=https://host:port
run.sh
#/bin/sh

PROJECT=www
PORT=80

. ../setenv.sh

docker run --name $PROJECT -p $PORT:$PORT -d --dns=$IP lavans-$PROJECT
kill.sh
#/bin/sh

PROJECT=www

docker stop $PROJECT
docker rm $PROJECT

動作確認

実行したらlocalhostに接続して起動確認します。

$ ./run.sh
$ telnet localhsot 80
Trying ::1...
Connected to localhost.
Escape character is '^]'.
GET /
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
...

無事に動きました。いろいろ設定を試したいならimageに焼きこむよりdnsの時のように/etc/nginxにマウントした方がよさそうです。

20
24
2

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
20
24