The sample from Docker, a "hello world" with redis and flask, seems to build fine, but, as an exercise can it be broken into two components?

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.

What I have in mind is to use the default redis image as its own container, and then modify the flask app so that it connects to the redis image. This is reasonable?


Yes, it's absolutely reasonable and this is how real apps work. This is the microservice approach which allows to independently scale instances of a service and a DB.

What you have to do is to run your Flask and Redis services in a way so the first one can achieve the second one. If they are on the same machine, you just have to open Redis port used by your Flask app (probably 6379):

docker run -d -p your_redis_image

And specify IP address and port of the Redis container in your Flask app. This is better to be done with a config file like the following yaml:

  host: ''
  pw: 'YourPassWord'
  port: 6379
  db: 0

And use it in the code:

import yaml, redis

cfg_file open('config.yaml', 'r')
cfg = yaml.load(cfg_file)
redcfg = cfg['redis']

r = redis.Redis(
    host=redcfg['host'], port=redcfg['port'],
    db=redcfg['db'], password=redcfg['pw'],

Don't forget to copy this file into container to be accessible from the inside.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.