Another - Lab - Get Started With Docker Compose
Another - Lab - Get Started With Docker Compose
On this page you build a simple Python web application running on Docker Compose. The application uses
the Flask framework and maintains a hit counter in Redis. While the sample uses Python, the concepts
demonstrated here should be understandable even if you’re not familiar with it.
Prerequisites
Make sure you have already installed both Docker Engine and Docker Compose. You don’t need to install
Python or Redis, as both are provided by Docker images.
Step 1: Setup
Define the application dependencies.
4. Create a file called app.py in your project directory and paste this in:
5. import time
6.
7. import redis
8. from flask import Flask
9.
10. app = Flask(__name__)
11. cache = redis.Redis(host='redis', port=6379)
12.
13. def get_hit_count():
14. retries = 5
15. while True:
16. try:
17. return cache.incr('hits')
18. except redis.exceptions.ConnectionError as exc:
19. if retries == 0:
20. raise exc
21. retries -= 1
22. time.sleep(0.5)
23.
24. @app.route('/')
25. def hello():
26. count = get_hit_count()
27. return 'Hello World! I have been seen {} times.\n'.format(count)
In this example, redis is the hostname of the redis container on the application’s network. We use
the default port for Redis, 6379.
Handling transient errors
Note the way the get_hit_count function is written. This basic retry loop lets us attempt our
request multiple times if the redis service is not available. This is useful at startup while the
application comes online, but also makes our application more resilient if the Redis service needs to
be restarted anytime during the app’s lifetime. In a cluster, this also helps handling momentary
connection drops between nodes.
28. Create another file called requirements.txt in your project directory and paste this in:
29. flask
30. redis
In your project directory, create a file named Dockerfile and paste the following:
FROM python:3.7-alpine
WORKDIR /code
ENV FLASK_APP=app.py
ENV FLASK_RUN_HOST=0.0.0.0
RUN apk add --no-cache gcc musl-dev linux-headers
COPY requirements.txt requirements.txt
RUN pip install -r requirements.txt
EXPOSE 5000
COPY . .
CMD ["flask", "run"]
Web service
The web service uses an image that’s built from the Dockerfile in the current directory. It then binds the
container and the host machine to the exposed port, 5000. This example service uses the default port for the
Flask web server, 5000.
Redis service
The redis service uses a public Redis image pulled from the Docker Hub registry.
Compose pulls a Redis image, builds an image for your code, and starts the services you defined. In
this case, the code is statically copied into the image at build time.
If you’re using Docker natively on Linux, Docker Desktop for Mac, or Docker Desktop for Windows,
then the web app should now be listening on port 5000 on your Docker daemon host. Point your
web browser to http://localhost:5000 to find the Hello World message. If this doesn’t resolve, you
can also try http://127.0.0.1:5000.
If you’re using Docker Machine on a Mac or Windows, use docker-machine ip MACHINE_VM to get
the IP address of your Docker host. Then, open http://MACHINE_VM_IP:5000 in a browser.
You should see a message in your browser saying:
Hello World! I have been seen 1 times.
23. Refresh the page.