notebook

都内でWEB系エンジニアやってます。

docker-composeを使ってみる

とりあえず使ってみる

環境

  • CentOS7

CentOS6だと色々制限あったりちょっと面倒になってきたのでCentOS7でやってみます

dockerのインストール

yum install docker
$ docker version
Client:
 Version:         1.9.1
 API version:     1.21
 Package version: docker-common-1.9.1-40.el7.centos.x86_64
 Go version:      go1.4.2
 Git commit:      ab77bde/1.9.1
 Built:
 OS/Arch:         linux/amd64
Cannot connect to the Docker daemon. Is the docker daemon running on this host?

docker-composeのインストール

docs.docker.com

curl -L https://github.com/docker/compose/releases/download/1.7.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
$ docker-compose --version
docker-compose version 1.7.1, build 0a9ab35

ためしに何か書いてみる

構成

  • backend2台
  • nginx

設定

  • docker-compose.yml
web1:
  build: ./web
web2:
  build: ./web

nginx:
  build: ./nginx
  links:
    - web1
    - web2
  ports:
    - "80:80"
    - "443:443"
  • web/Dockerfile
FROM centos:centos6

EXPOSE 8080

CMD python -m SimpleHTTPServer 8080
  • nginx/Dockerfile
FROM nginx
MAINTAINER swfz sawafuji.09@gmail.com

COPY ["./nginx.conf", "/etc/nginx/nginx.conf"]

CMD ["/usr/sbin/nginx", "-g", "daemon off;"]
  • nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    open_file_cache max=100 inactive=5s;
    upstream backend {
        least_conn;
        server web1:8080;
        server web2:8080;
    }

    include /etc/nginx/conf.d/*.conf;
    server {
        listen       80 default_server;

        location /nginx_status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            deny all;
        }
        location / {
            proxy_pass http://backend;
        }
    }
}

起動

docker-compose up -d
  • リバプロの確認
$ docker ps
....
....
$ docker logs -f 741f7cee73bd(backend側)
172.17.0.4 - - [21/Jun/2016 14:36:32] "GET / HTTP/1.0" 200 -
172.17.0.4 - - [21/Jun/2016 14:36:34] "GET / HTTP/1.0" 200 -

curlでnginxのコンテナへ何度かアクセスし、均等にアクセスがいったことを確認

おわり

links,ports,volumesなどdockerだけで複数コンテナ立ててごにょごにょするのは結構しんどいですがcomposeだと簡単な設定だけで実現できてしまうのでとても感動しました

version2の記法はdocker自体のバージョンが足りなかったので今回は見送り

次はversion2の記法で実用的なものを作ってみたいですね

最後にサンプルで使ったファイルたちを上げて終わり

github.com