Docker Compose Example Yml File

Docker Compose Example yml File

In this following steps, I will show you how to run a python flask application with Redis database by using docker-compose yml example file. In this simple python flask application Whenever you refresh the browser, the number counting will be increased.

In the below image you can see 9 number.

docker compose yml example

To store that number we need one database so that's why we are using Redis database.

Before going to start make sure that docker and docker compose installed in your system.

in the following steps, we are going to create four files

  • app.py
  • requirements.txt
  • Dockerfile
  • Docker-compose.yml

So here app.py and requirement.txt files are application files.

By using docker file we will convert above two application files into customized python app image.

In  docker-compose.yml file, we will link this customized python image with redis image

Creating App.py File

create one empty file app.py

copy and paste below code in app.py and save it

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('hits')
        except redis.exceptions.ConnectionError as exc:
            if retries == 0:
                raise exc
            retries -= 1
            time.sleep(0.5)


@app.route('/')
def hello():
    count = get_hit_count()
    return 'Hello World! I have been seen {} times.\n'.format(count)

if __name__ == "__main__":
    app.run(host="0.0.0.0", debug=True)

Creating Requirements.txt

this application needs flask and Redis libraries so we will create one file called requirements.txt in this file we will mention flask and redis.

create empty file called requirements.txt and paste below code

flask

redis

Creating Dockerfile

FROM python:3.4-alpine
ADD . /code
WORKDIR /code
RUN pip install -r requirements.txt
CMD ["python", "app.py"]

By using this dockerfile we will create customized python image.

FROM python:3.4-alpine

We are using python:3.4-alpine base image.

In this image, python is already preinstalled.

ADD . /code

ADD command will create the directory called code in base image python:3.4-alpine.

ADD command will add our app.py and requirements.txt files to /code directory.

By using ADD command our application files are added into base image.

WORKDIR /code

Now we have to install or add flask and Redis libraries for this we have to run the requirements.txt file.

But this file is located in /code directory.

So to execute further commands we have to enter into /code directory that’s why we use command.

WORKDIR /code

so all next commands will be executed in this directory only

RUN pip install -r requirements.txt

So now install Redis and flask libraries by using RUN command.

This command will install whatever libraries mentioned in requirements.txt.

CMD [“python”, “app.py”]

Now run application with cmd command.

So this image will create a python application but this application need database to store values.

For that, we are going to use Redis image from docker hub.

We will link this two images in docker compose.yml example file.

Creating docker-compose.yml File example

Create one empty file called docker-compose.yml, copy and paste below code in that file.

version: '3'
services:
  web:
    build: .
    ports:
     - "5000:5000"
  redis:
    image: "redis:alpine"

version: '3'

here version 3 is docker compose version in every docker compose file the starting line will be the version we are using version 3

services:

services are nothing but containers you can see in docker compose file we are having two containers called web and redis.

Generally, we will mention our containers in the services section

build: .

this build command will build the image from our dockerfile

here . (dot) is the location of docker file, dockerfile, and docker compose file both are in same directory thats why I mentioned. (dot)

if your dockerfile is in another directory mention the path of dockerfile

ports:

ports:

- "5000:5000"

In the ports section, we will expose our python application container to the outer world.

We are mapping our machine port 5000 to container port 5000.

You can mention any number in first 5000 place , because this is a machine port.

Whatever port you have available in your machine you can map to container.

Ex : if you have 3000 port is available in your machine write.

“3000:5000”

image: “redis:alpine”

For redis container, we are using redis:alpine image from docker hub.

But for web conatiner, we used build command to create a custom image from our docker file.

Run python flask application with docker compose yml file example

Run your application with command

docker-compose  up

Open you browser enter your hostmachine ip: 5000

Ex  35.171.19.234:5000

here 35.171.19.234 is my host machine ip

You can see hello world you have been seen 1 times.

If you refresh the page the count will increase.

i refreshed 9 times thats why i got 9 times

docker compose yml file example

this is the docker compose yml example for simple python flask application with redis database.

  • docker compose file example
  • docker compose yml file example
  • docker compose example

Leave a Reply

Your email address will not be published. Required fields are marked *