Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker Compose doesn't load .env file and incorrect DATABASE_URL configuration #588

Open
2 tasks
SureshPradhana opened this issue Aug 16, 2024 · 7 comments
Open
2 tasks
Labels
complexity: moderate Time needed to do this ticket will be moderate e.g. 1-2 days docker Concerns Docker containers question / suggestion Further information is requested state: approved Ready to go. Not blocked or pending.

Comments

@SureshPradhana
Copy link

Overview:

There are two main issues in the current Docker setup:

  1. The .env file is not being loaded, causing missing environment variables during runtime.
  2. The DATABASE_URL uses host.docker.internal:35000, which leads to connection errors within the Docker network.

These issues prevent the backend service from properly connecting to the PostgreSQL database when using Docker Compose.

Environment:

  • OS: Arch Linux
  • Docker Version: 27.0.3
  • Docker Compose Version: 2.28.1

Actions Needed:

  • Load .env file: Update the docker-compose.yml to include the env_file directive.
  • Update DATABASE_URL: Modify the DATABASE_URL to use the correct hostname within the Docker network (db:5432).

Proposed Fix:

  1. Load .env file:
    Add the following to the api service in docker-compose.yml:

    env_file:
      - .env
  2. Correct DATABASE_URL:
    Update the DATABASE_URL in docker-compose.yml:

    DATABASE_URL=postgres://postgres:postgres@db:5432/bloom
@SureshPradhana
Copy link
Author

Hi, can you confirm if the issue I reported is correct? If so, I'd like to work on it.

@kyleecodes kyleecodes added the question / suggestion Further information is requested label Aug 26, 2024
@kyleecodes
Copy link
Member

kyleecodes commented Aug 28, 2024

  1. The .env file is not being loaded, causing missing environment variables during runtime.
  2. The DATABASE_URL uses host.docker.internal:35000, which leads to connection errors within the Docker network.

Hi @SureshPradhana, thank you for your suggestion, and patience while we were out of office. Can you share the exact connection errors you experienced? They might be specific to your set-up. But we aim to support all environments, so it's important we learn what is going on here.

As for loading env vars, I agree they should be mounted to the volume / loaded at runtime, as you demonstrated. Our current set-up isn't best practice, this would be more secure and flexible.

@SureshPradhana
Copy link
Author

Hi @kyleecodes,

Thanks for the feedback. Here are the connection errors I faced:

  • Issue: Connection refused when using host.docker.internal:35000 for PostgreSQL.
    bloom-backend   | [Nest] 39  - 08/29/2024, 11:20:24 AM   ERROR [TypeOrmModule] Unable to connect to the database. Retrying (1)...
    bloom-backend   | Error: getaddrinfo ENOTFOUND host.docker.internal
    bloom-backend   |     at GetAddrInfoReqWrap.onlookupall [as oncomplete] (node:dns:120:26)
    bloom-backend   | [Nest] 39  - 08/29/2024, 11:20:27 AM   ERROR [TypeOrmModule] Unable to connect to the database. 
    

i use linux found out that On Linux, host.docker.internal is not available out-of-the-box. Instead, you might need to use the host’s IP address or configure Docker networking to access the host machine.

In a Docker Compose setup, host.docker.internal isn't suitable because PostgreSQL runs in a Docker container. The recommended approach is to use postgres://postgres:postgres@db:5432/bloom to connect to the PostgreSQL service within Docker.

Copy link

Thank you @SureshPradhana you have been assigned this issue!
Please follow the directions in our Contributing Guide. We look forward to reviewing your pull request shortly ✨


Support Chayn's mission? ⭐ Please star this repo to help us find more contributors like you!
Learn more about Chayn here and explore our projects. 🌸

@kyleecodes
Copy link
Member

Great points @SureshPradhana. Thanks for your patience while we were out of office.
This might be a Linux specific suggestion? But let's try it! We are currently making updates to our Docker set-up.
I've assigned you the issue if you would like to implement these changes. Please tag me if you have any questions or if you'd like to be unassigned.

@kyleecodes kyleecodes added state: approved Ready to go. Not blocked or pending. complexity: moderate Time needed to do this ticket will be moderate e.g. 1-2 days docker Concerns Docker containers labels Sep 17, 2024
@github-actions github-actions bot added the stale This issue or PR is inactive. label Oct 21, 2024
@kyleecodes kyleecodes removed the stale This issue or PR is inactive. label Oct 23, 2024
@eleanorreem
Copy link
Contributor

eleanorreem commented Nov 20, 2024

@leoseg I'm going to assign you to this issue as you have done some work on it. You might need to comment on it before I assign you.

@leoseg
Copy link
Contributor

leoseg commented Nov 20, 2024

Okay, I include the fix for this issue in another pull request and linked it there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complexity: moderate Time needed to do this ticket will be moderate e.g. 1-2 days docker Concerns Docker containers question / suggestion Further information is requested state: approved Ready to go. Not blocked or pending.
Projects
None yet
Development

No branches or pull requests

4 participants