MicroServices using Docker Compose — a Tiny Demonstration

FROM python:2.7-slim
WORKDIR /app
ADD . /app
RUN pip install --trusted-host pypi.python.org Flask Redis
EXPOSE 80
CMD ["python", "app.py"]
from flask import Flask
from redis import Redis, RedisError
import os
import socket
redis = Redis(host="redis-server", db=0, socket_connect_timeout=2, socket_timeout=2)app = Flask(__name__)@app.route("/")
def hello():
try:
visits = redis.incr("counter")
except RedisError:
visits = "<i>cannot connect to Redis server to count</i>"
html = "<h3>Hello World!</h3>\n" \
"<b>Hostname:</b> {hostname}<br/>\n" \
"<b>Visits:</b> {visits}\n"
return html.format(hostname=socket.gethostname(), visits=visits)if __name__ == "__main__":
app.run(host='0.0.0.0', port=80)
version: "3"
services:
redis-server:
image: redis:alpine
ports:
- "6379"
networks:
- mynet
app:
build: app
ports:
- "8080:80"
networks:
- mynet
depends_on:
- redis
networks:
mynet:
.
├── app
│ ├── Dockerfile
│ └── app.py
└── docker-compose.yml
docker-compose up -d
curl localhost:8080
<h3>Hello World!</h3><b>Hostname:</b> b3f2f3a731a6<br/><b>Visits:</b> 1
curl localhost:8080
<h3>Hello World!</h3><b>Hostname:</b> b3f2f3a731a6<br/><b>Visits:</b> 2

Sreeprakash Neelakantan

Written by

AWS Certified DevOps Engineer & Solutions Architect Professional — Docker | Kubernetes | DevOps — Trainer | Running | Swimming | Cycling

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade