{"id":26018885,"url":"https://github.com/nachtfeuer/docker-compose-demo","last_synced_at":"2026-04-17T08:05:13.407Z","repository":{"id":94143578,"uuid":"150884212","full_name":"Nachtfeuer/docker-compose-demo","owner":"Nachtfeuer","description":"Docker and docker-compose","archived":false,"fork":false,"pushed_at":"2018-10-03T11:50:27.000Z","size":13,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-06T06:46:44.439Z","etag":null,"topics":["demo","docker","docker-compose","scale"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Nachtfeuer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-09-29T16:47:30.000Z","updated_at":"2018-10-03T11:50:29.000Z","dependencies_parsed_at":"2023-03-16T06:30:34.342Z","dependency_job_id":null,"html_url":"https://github.com/Nachtfeuer/docker-compose-demo","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Nachtfeuer/docker-compose-demo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nachtfeuer%2Fdocker-compose-demo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nachtfeuer%2Fdocker-compose-demo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nachtfeuer%2Fdocker-compose-demo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nachtfeuer%2Fdocker-compose-demo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Nachtfeuer","download_url":"https://codeload.github.com/Nachtfeuer/docker-compose-demo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Nachtfeuer%2Fdocker-compose-demo/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":31920520,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-16T18:22:33.417Z","status":"online","status_checked_at":"2026-04-17T02:00:06.879Z","response_time":62,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["demo","docker","docker-compose","scale"],"created_at":"2025-03-06T06:38:49.312Z","updated_at":"2026-04-17T08:05:13.395Z","avatar_url":"https://github.com/Nachtfeuer.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Welcome to the docker-compose demo\n\n**Table Of Contents**\n\n - [Thoughts](#thoughts)\n - [Requirements](#requirements)\n - [Starting the primes server directly](#starting-the-primes-server-directly)\n - [Building the image for the primes server](#building-the-image-for-the-primes-server)\n - [Running the primes server as Docker container](#running-the-primes-server-as-docker-container)\n - [Using docker-compose for starting the primes server](#using-dockercompose-for-starting-the-primes-server)\n - [Running multiple prime servers with a loadbalancer](#running-multiple-prime-servers-with-a-loadbalancer)\n - [Scaling the primes server](#scaling-the-primes-server)\n - [Health check](#health-check)\n - [Using Mongo database](#using-mongo-database)\n - [Links](#links)\n\n## Thoughts\n\n - The images can be built locally but the docker-compose also would work if the images are already uploaded\n   to a Docker registry; this idea is essential if you want to run such an environment either in Jenkins or\n   Travis CI like system as part of a CI/CD (coded pipeline) or via Kubernetes.\n - The good thing is: if it works for those systems it also works locally which is usually not the case\n   if you use such tools (Docker and docker-compose) relying on a concrete setup on your machine.\n - The setup and teardown should **always** be **easy** and **fast**.\n - The example server (written in Python) is very short (below 50 lines of code)\n\n## Requirements\n\n - Please ensure you have at least **Docker 18.06**.\n - You should have at least **docker-compose 1.22.0**.\n - You always should be in the root path of this repository when you execute shown commands.\n\n## Starting the primes server directly\n\n```bash\n$ HOSTNAME=demo FLASK_APP=src/primes_server.py flask run\n * Serving Flask app \"src/primes_server.py\"\n * Environment: production\n   WARNING: Do not use the development server in a production environment.\n   Use a production WSGI server instead.\n * Debug mode: off\n * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)\n```\n\nOpen another terminal and then:\n\n```bash\n$ curl http://127.0.0.1:5000/primes/check/3\n{\"isPrime\": true, \"hostname\": \"demo\", \"number\": 3}\n$ curl http://127.0.0.1:5000/primes/check/4\n{\"isPrime\": false, \"hostname\": \"demo\", \"number\": 4}\n```\n\n## Building the image for the primes server\n\nIt's basically about installing the required Python module **Flask**, adding the python\nserver to the image and defining the command on how to run the server when the Docker container\nfor this image is started.\n\n```bash\ndocker build -t demo/primes-server:latest . -f images/Dockerfile.primes_server\n```\n\n## Running the primes server as Docker container\n\n```bash\n$ docker run --rm --name=primes-server -p 5000:5000 -d demo/primes-server:latest\n$ curl http://localhost:5000/primes/check/3\n{\"isPrime\": true, \"hostname\": \"09d6af743a3f\", \"number\": 3}\n$ curl http://localhost:5000/primes/check/4\n{\"isPrime\": false, \"hostname\": \"09d6af743a3f\", \"number\": 4}\n```\n\nWhen stopping the server (`docker stop primes-server`) the container automatically\ngoes away because of the `--rm` option.\n\n## Using docker-compose for starting the primes server\n\n```bash\n$ docker-compose -f compose/docker-compose-server.yml up -d primes-server\n$ docker ps\nCONTAINER ID        IMAGE                       COMMAND                  CREATED              STATUS              PORTS                    NAMES\n29177c0471cf        demo/primes-server:latest   \"flask run --host=0.…\"   About a minute ago   Up About a minute   0.0.0.0:5000-\u003e5000/tcp   docker-compose-demo_primes-server_1\n$ curl http://localhost:5000/primes/check/3\n{\"isPrime\": true, \"hostname\": \"09d6af743a3f\", \"number\": 3}\n$ curl http://localhost:5000/primes/check/4\n{\"isPrime\": false, \"hostname\": \"09d6af743a3f\", \"number\": 4}\n```\n\nThe `-d` is that docker compose starts the process into background.\nAvoiding that more services are started the service **prime-service** is specified.\nYou can shutdown with `docker-compose -f compose/docker-compose-server.yml down`.\n\n\n## Running multiple prime servers with a loadbalancer\n\n```bash\n$ docker-compose --compatibility -f compose/docker-compose-loadbalancer.yml up -d\nStarting compose_primes-server_1 ... done\nStarting compose_primes-server_2 ... done\nStarting compose_primes-server_3 ... done\nStarting compose_proxy_1         ... done\n$ for n in $(seq 1 12); do echo \"$(curl -s http://localhost/primes/check/$n)\"; done\n{\"isPrime\": false, \"hostname\": \"99a5669c6fa2\", \"number\": 1}\n{\"isPrime\": true, \"hostname\": \"35eeb727e120\", \"number\": 2}\n{\"isPrime\": true, \"hostname\": \"ffda7cf2c91f\", \"number\": 3}\n{\"isPrime\": false, \"hostname\": \"99a5669c6fa2\", \"number\": 4}\n{\"isPrime\": true, \"hostname\": \"35eeb727e120\", \"number\": 5}\n{\"isPrime\": false, \"hostname\": \"ffda7cf2c91f\", \"number\": 6}\n{\"isPrime\": true, \"hostname\": \"99a5669c6fa2\", \"number\": 7}\n{\"isPrime\": false, \"hostname\": \"35eeb727e120\", \"number\": 8}\n{\"isPrime\": false, \"hostname\": \"ffda7cf2c91f\", \"number\": 9}\n{\"isPrime\": false, \"hostname\": \"99a5669c6fa2\", \"number\": 10}\n{\"isPrime\": true, \"hostname\": \"35eeb727e120\", \"number\": 11}\n{\"isPrime\": false, \"hostname\": \"ffda7cf2c91f\", \"number\": 12}\n```\n\n- You can see that each response comes from another prime server (hostname).\n  Docker usually adjusts the hostname with the id of the container.\n- You also can see that the mechanism used here is round robin.\n- The `--compatibility` parameter is **required** otherwise the scale feature\n  in the yaml configuration (name: deploy) works for Docker swarm only.\n- It's also to mention that the concrete Docker image for the haproxy\n  seems no longer maintained which does the automatic re-configuration\n  depending on current scale. There are other solution with Docker compose\n  but I have not yet investigated.\n- You shutdown with `docker-compose --compatibility -f compose/docker-compose-loadbalancer.yml down`.\n\n## Scaling the primes server\n\n```bash\ndocker-compose --compatibility -f compose/docker-compose-loadbalancer.yml up -d --scale=primes-server=10\nStarting compose_primes-server_1 ... done\nStarting compose_primes-server_2 ... done\nStarting compose_primes-server_3 ... done\nCreating compose_primes-server_4  ... done\nCreating compose_primes-server_5  ... done\nCreating compose_primes-server_6  ... done\nCreating compose_primes-server_7  ... done\nCreating compose_primes-server_8  ... done\nCreating compose_primes-server_9  ... done\nCreating compose_primes-server_10 ... done\ncompose_proxy_1 is up-to-date\n```\n\n## Health check\n\nThe health check can be implemented as REST call of the server.\nThe server has to tell whether all is fine.\n\n```bash\n$ docker ps --format=\"{{.Names}} {{.Status}}\"\ncompose_proxy_1 Up 43 seconds\ncompose_primes-server_3 Up 45 seconds (healthy)\ncompose_primes-server_2 Up 45 seconds (healthy)\ncompose_primes-server_1 Up 46 seconds (healthy)\n```\n\nIn the logs of each container you see a record each\n10 seconds:\n\n```\nhealthcheck:\n  test: [\"CMD\", \"curl\", \"-f\", \"http://localhost:5000/status\"]\n  interval: 10s\n  timeout: 2s\n```\n\n## Using Mongo database\n\nStarting a Mongo database via Docker:\n\n```bash\n$ docker run --rm --name=mongo -p 27017:27017 -d mongo:latest\n```\n\nUsing the previously started Docker container:\n\n```bash\n$ MONGODB_HOST=localhost HOSTNAME=demo FLASK_APP=src/primes_server_mongo.py flask run\n```\n\nThis prime server stores each calculated primes for a given maximum number into\nMongo DB and next time when same list is requested the list is queried in the database\nand returnd instead of the calculation.\n\nThe docker image can be built with\n\n```bash\n$ docker build -t demo/primes-server-mongo:latest . -f images/Dockerfile.primes_server_mongo\n```\n\nWith this you can also run an environment with Mongo DB, three primes server and\na loadbalance in front of this:\n\n```bash\n$ docker-compose --compatibility -f compose/docker-compose-mongo.yml up -d\n```\n\nUsing **curl** then only difference now is that (currently) the port is 80 (loadbalancer).\n\n```bash\n$ curl http://127.0.0.1:80/primes/list/100\n{\"max_n\": 100, \"primes\": [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97], \"hostname\": \"383ac2648957\"}\n```\n\nYou also can verify it changing into the Mongo DB Docker container:\n\n```bash\n$ docker exec -it compose_mongodb_1 bash\n$ mongo\n$ use primes\nswitched to db primes\n$ db.Primes.find({max_n: 100})\n{ \"_id\" : ObjectId(\"5bb4aa1a667cd800012a95d0\"), \"max_n\" : 100, \"values\" : [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67,71, 73, 79, 83, 89, 97 ] }\n```\n\n## Links\n\n - \u003chttps://docs.docker.com/compose/install/\u003e\n - \u003chttps://docs.docker.com/compose/compose-file/\u003e\n - \u003chttps://docs.docker.com/compose/startup-order/\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnachtfeuer%2Fdocker-compose-demo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnachtfeuer%2Fdocker-compose-demo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnachtfeuer%2Fdocker-compose-demo/lists"}