├── nginx ├── start ├── Dockerfile └── nginx.tmpl ├── app ├── Dockerfile └── app.py ├── README.md └── docker-compose.yml /nginx/start: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | j2 /nginx.tmpl > /etc/nginx/nginx.conf 4 | nginx 5 | -------------------------------------------------------------------------------- /app/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python 2 | 3 | RUN pip install flask redis 4 | 5 | WORKDIR / 6 | 7 | COPY app.py / 8 | 9 | EXPOSE 5000 10 | 11 | CMD python app.py 12 | -------------------------------------------------------------------------------- /nginx/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nginx:latest 2 | RUN apt-get update -y 3 | RUN apt-get install -y python-pip 4 | RUN pip install j2cli 5 | COPY start /start 6 | RUN chmod +x /start 7 | COPY nginx.tmpl /nginx.tmpl 8 | CMD /start 9 | EXPOSE 80 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # docker-compose example 2 | 3 | This is the example used in my [blog][1] to show how to construct an environment with loadbalancing with docker-compose. 4 | 5 | [1]: http://eyenx.ch/2015/04/18/loadbalancing-containers-with-docker-compose/ 6 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | app: 2 | build: app 3 | links: 4 | - redis 5 | haproxy: 6 | image: tutum/haproxy 7 | links: 8 | - app 9 | environment: 10 | - BACKEND_PORT=5000 11 | redis: 12 | image: redis 13 | nginx: 14 | build: nginx 15 | ports: 16 | - "80:80" 17 | links: 18 | - haproxy 19 | -------------------------------------------------------------------------------- /app/app.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | from flask import Flask 4 | import socket,redis,os 5 | 6 | r=redis.Redis(host=os.getenv("REDIS_PORT_6379_TCP_ADDR"),port=os.getenv("REDIS_PORT_6379_TCP_PORT")) 7 | r.set("count",0) 8 | 9 | app = Flask(__name__) 10 | 11 | @app.route("/") 12 | def index(): 13 | r.incr("count") 14 | return "Visit number %d\nHostname: %s\n" % (int(r.get("count")),socket.gethostname()) 15 | 16 | if __name__ == "__main__" : 17 | app.run(host="0.0.0.0") 18 | -------------------------------------------------------------------------------- /nginx/nginx.tmpl: -------------------------------------------------------------------------------- 1 | user nginx; 2 | daemon off; 3 | worker_processes 1; 4 | 5 | error_log /proc/self/fd/2 warn; 6 | pid /var/run/nginx.pid; 7 | 8 | 9 | events { 10 | worker_connections 1024; 11 | } 12 | 13 | 14 | http { 15 | resolver 8.8.8.8; 16 | include /etc/nginx/mime.types; 17 | default_type application/octet-stream; 18 | 19 | log_format main '$remote_addr - $remote_user [$time_local] "$request" ' 20 | '$status $body_bytes_sent "$http_referer" ' 21 | '"$http_user_agent" "$http_x_forwarded_for"'; 22 | 23 | access_log /var/log/nginx/access.log main; 24 | 25 | sendfile on; 26 | #tcp_nopush on; 27 | 28 | keepalive_timeout 65; 29 | 30 | #gzip on; 31 | 32 | #include /etc/nginx/conf.d/*.conf; 33 | 34 | server { 35 | listen 80; 36 | server_name localhost; 37 | access_log /proc/self/fd/1; 38 | error_log /proc/self/fd/2; 39 | location / { 40 | proxy_intercept_errors on; 41 | proxy_pass http://{{ HAPROXY_PORT_80_TCP_ADDR }}:{{ HAPROXY_PORT_80_TCP_PORT }}$request_uri?; 42 | proxy_set_header Host $server_name; 43 | } 44 | } 45 | } 46 | --------------------------------------------------------------------------------