0

I have a two services in docker compose 1. an application and 2. db service (can be Mysql or Postgres). Depending upon environment variables set in compose file for db services, I need create DATABASE_URI for sqlalchemy engine. How do I access these ENV in app docker container?

I am trying to access env set inside docker-compose file and not Dockerfile.

Below is how my Docker-compose file looks

version: "3.7"
services:
  myapp:
    image: ${TAG:-myapp}
    build:
      context: .
    ports:
      - "5000:5000"


  docker_postgres:
    image: "postgres:latest"
    environment:
      - POSTGRES_PASSWORD=postgres
    ports:
      - "5432:5432"
Pravin
  • 677
  • 2
  • 7
  • 22
  • Have you tried accessing it in the same way you'd access any other environment variable from Python? – jonrsharpe Mar 12 '19 at 10:09
  • Yes, but gets nothing. – Pravin Mar 12 '19 at 10:11
  • Then please give a [mcve]. For what it's worth, the project I'm currently working on passes env vars from my environment, through the compose file, into the container, and into the code (mostly JS, but we do have some Python) just fine. – jonrsharpe Mar 12 '19 at 10:12
  • Possible duplicate of [How do I pass environment variables to Docker containers?](https://stackoverflow.com/questions/30494050/how-do-i-pass-environment-variables-to-docker-containers) – Mudits Mar 12 '19 at 10:13
  • I want to excess env vars set for db (for.eg postgres db) in compose file inside my app container to construct database_uri. – Pravin Mar 12 '19 at 10:14
  • Yes, that part is clear. What's unclear is how you're currently attempting to *actually implement that*. [This](https://docs.docker.com/compose/compose-file/#environment) is working fine for me. – jonrsharpe Mar 12 '19 at 10:16
  • @jonrsharpe This is how I have implemented too. – Pravin Mar 12 '19 at 10:18
  • Is it possible to show the docker-compose file. At least then we can see what you have done. – Olufemi Adesina Mar 12 '19 at 10:19
  • So **give a [mcve]**. `docker-compose`, `Dockerfile`, `some.py` - how is this actually set up? – jonrsharpe Mar 12 '19 at 10:19
  • You appear to not actually be setting `DATABASE_URI`, or any other environment variable, for `myapp`. So why did you think it *would* be set? – jonrsharpe Mar 12 '19 at 10:25
  • I am planning to construct database_uri using all env set under compose file. For eg `postgresql://{POSTGRES_USER}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}:5432/{POSTGRES_DB}` – Pravin Mar 12 '19 at 10:26
  • But **you haven't set any of those**. You clearly know how to set them, because you're doing it for the `docker_postgres` service, so why don't you do the same thing for `myapp`? I don't understand why you think there's a *problem* here, you just haven't done it yet. – jonrsharpe Mar 12 '19 at 10:30

2 Answers2

0

You need to set environment variables for your postgres database if you want to build your own database_uri.

environment:
POSTGRES_DB: dev
POSTGRES_USER: username
POSTGRES_PASSWORD: pw
0

@jonrsharpe You mean to say I can do something like this?

services:
  myapp:
    image: ${TAG:-myapp}
    build:
      context: .
    environment:
      - DB_USER=postgres
      - DB_PASSWORD=postgres
      - DB_HOST=docker_postgres
    ports:
      - "5000:5000"


  docker_postgres:
    image: "postgres:latest"
    environment:
      - POSTGRES_PASSWORD=postgres
      - POSTGRES_USER=postgres
    ports:
      - "5432:5432"```
Pravin
  • 677
  • 2
  • 7
  • 22