<p>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.</p>
<p>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):</p>
<pre><code>docker run -d -p 192.168.10.20:6379:6379 your_redis_image
</code></pre>
<p>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 <code>yaml</code>:</p>
<pre class="lang-yaml prettyprint-override"><code>redis:
  host: '172.16.10.20'
  pw: 'YourPassWord'
  port: 6379
  db: 0
</code></pre>
<p>And use it in the code:</p>
<pre class="lang-py prettyprint-override"><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'],
)
</code></pre>
<p>Don't forget to copy this file into container to be accessible from the inside.</p>

CC BY-SA 4.0
2020-09-25T08:58:12.443Z
2020-09-25T08:58:12.443Z
AivanF.
<p>The <a href="https://devops.stackexchange.com/q/12482/23443">sample</a> from Docker, a &quot;hello world&quot; with <code>redis</code> and <code>flask</code>, seems to build fine, but, as an exercise can it be broken into two components?</p>
<blockquote>
<p>The redis service uses a public Redis image pulled from the Docker Hub
registry.</p>
</blockquote>
<p>and</p>
<blockquote>
<p>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.</p>
</blockquote>
<p>What I have in mind is to use the default <code>redis</code> image as its own container, and then modify the <code>flask</code> app so that it <a href="https://devops.stackexchange.com/q/12467/23443">connects</a> to the <code>redis</code> image.  This is reasonable?</p>

<docker><docker-compose><containers><flask><redis>
hello world with flask and redis using docker networking
Nicholas Saunders
# hello world with flask and redis using docker networking

(Asked by: username_0 on 2020-09-24)

The [sample](https://devops.stackexchange.com/q/12482/23443) 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.

and

> 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](https://devops.stackexchange.com/q/12467/23443) to the `redis` image. This is reasonable?

**Tags:** docker, docker-compose, containers, flask, redis


---

## 1 answers

### Answer by username_1 on 2020-09-25. Score: 2

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 192.168.10.20:6379:6379 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:
redis:
  host: '172.16.10.20'
  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.


---

