Get started with Docker Compose | Docker-compose enter container
ThistutorialisdesignedtointroducethekeyconceptsofDockerComposewhilstbuildingasimplePythonwebapplication.TheapplicationusestheFlaskframeworkandmaintainsahitcounterinRedis.Theconceptsdemonstratedhereshouldbeunderstandableevenifyou’renotfamiliarwithPython.PrerequisitesYouneedtohaveDockerEngineandDockerComposeonyourmachine.Youcaneither:Youdon’tneedtoinstallPythonorRedis,asbothareprovidedbyDockerimages.Step1:DefinetheapplicationdependenciesCreateadirectoryfortheproject:$mkdircomposetest$cdcompose...
This tutorial is designed to introduce the key concepts of Docker Compose whilst building a simple Python web application. The application uses the Flask framework and maintains a hit counter in Redis.
The concepts demonstrated here should be understandable even if you’re not familiar with Python.
PrerequisitesYou need to have Docker Engine and Docker Compose on your machine. You can either:
You don’t need to install Python or Redis, as both are provided by Docker images.
Step 1: Define the application dependenciesCreate a directory for the project:
$ mkdir composetest $ cd composetestCreate a file called app.py in your project directory and paste the following code in:
import time import redis from flask import Flask app = Flask(__name__) cache = redis.Redis(host=redis, port=6379) def get_hit_count(): retries = 5 while True: try: return cache.incr...