Talk is cheap

Files:

1
2
3
4
5
> tree
.
├── app.py
├── docker-compose.yml
└── web.Dockerfile
  • docker-compose.yml
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
27
28
29
version: "3"

services:

redis:
image: redis
restart: always

web:
build:
context: .
dockerfile: web.Dockerfile
depends_on:
- redis
environment:
REDIS_HOST: redis
restart: always

lb:
image: dockercloud/haproxy
links:
- web
ports:
- 80:80
depends_on:
- web
volumes:
- /var/run/docker.sock:/var/run/docker.sock
restart: always
  • app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from flask import Flask
from redis import Redis
import os
import socket

app = Flask(__name__)
redis = Redis(host=os.environ.get('REDIS_HOST', '127.0.0.1'), port=6379)


@app.route('/')
def hello():
redis.incr('hits')
return 'Hello Container World! I have been seen %s times and my hostname is %s.\n' % (redis.get('hits'),socket.gethostname())


if __name__ == "__main__":
app.run(host="0.0.0.0", port=8000, debug=True)
  • web.Dockerfile
1
2
3
4
5
6
7
FROM python:2.7
LABEL maintaner="simon.xie@codewalker.me"
COPY . /app
WORKDIR /app
RUN python -m pip install --upgrade pip && pip install flask redis
EXPOSE 8000
CMD [ "python", "app.py" ]

How to run

1
docker-compose up -d

then you should see something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> docker-compose up
Starting scale_redis_1 ... done
Starting scale_web_1 ... done
Starting scale_lb_1 ... done
...
...
...
lb_1 | maxconn 4096
lb_1 | default_backend default_service
lb_1 | backend default_service
lb_1 | server scale_web_1 scale_web_1:80 check inter 2000 rise 2 fall 3
lb_1 | INFO:haproxy:Launching HAProxy
lb_1 | INFO:haproxy:HAProxy has been launched(PID: 9)
lb_1 | INFO:haproxy:===========END===========

How to see

Open 127.0.0.1 in your browser whatever you like.

EOF
Enjoy
I, simon love this world ❤️