Report this

What is the reason for this report?

How to connect GitHub repository to a Dockerized database?

Posted on December 2, 2025
Jackpot Junk Removal Las Vegas

By Jackpot Junk Removal Las Vegas

Junk Removal Henderson NV

Hi everyone,

I’m trying to figure out the correct way to connect my application (hosted/pulled from GitHub) to a database running inside Docker (e.g., PostgreSQL, MySQL, MongoDB, etc.).

Common problems I’m facing:

  • Connection refused / timeout when the app starts
  • Hardcoding database credentials or host IP doesn’t work reliably
  • Database container and app container can’t communicate properly
  • Environment variables not being read correctly

What is the best/recommended way to:

  1. Run the DB in Docker
  2. Pull and run my code from GitHub (via GitHub Actions, Dockerfile, docker-compose, etc.)
  3. Make the app successfully connect to the Docker database

Any working docker-compose.yml examples or step-by-step guides would be really helpful!

Thanks in advance!



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hello, @jackpotjunkremovallasvegas

You can put your app and your DB in the same docker-compose.yml, let Docker handle networking, and connect using the service name as the host, with credentials coming from environment variables (or a .env file), not hard-coded values or IPs.

From there, most of the “connection refused / timeout / env not read” problems disappear.

Imagine your app lives in this repo and has a Dockerfile at the root. The app expects DB settings from environment variables like DB_HOST, DB_USER, etc.

Here’s an example docker-compose.yml:

version: "3.9"

services:
  db:
    image: postgres:16
    container_name: myapp-db
    environment:
      POSTGRES_USER: myuser
      POSTGRES_PASSWORD: mypassword
      POSTGRES_DB: myapp
    volumes:
      - db_data:/var/lib/postgresql/data
    ports:
      - "5432:5432"
    healthcheck:
      test: ["CMD-SHELL", "pg_isready -U myuser -d myapp"]
      interval: 5s
      timeout: 5s
      retries: 10

  app:
    build: .
    container_name: myapp-app
    depends_on:
      db:
        condition: service_healthy
    environment:
      DB_HOST: db         # IMPORTANT: use service name, not localhost or IP
      DB_PORT: 5432
      DB_USER: myuser
      DB_PASSWORD: mypassword
      DB_NAME: myapp
      # any other env your app needs...
    ports:
      - "8000:8000"
    # command: ["your", "start", "command"]  # if not defined in Dockerfile

volumes:
  db_data:

Regards

Hi,

The easiest and most reliable setup is to run your app and your database inside the same docker-compose network. Instead of connecting via IPs, you simply use the service name as the hostname and Docker handles the DNS resolution for you.

Here’s a small example:

services:
  db:
    image: postgres:16
    environment:
      POSTGRES_PASSWORD: example
    ports:
      - 5432:5432

  app:
    build: .
    env_file: .env
    depends_on:
      - db
    environment:
      DATABASE_URL: postgres://postgres:example@db:5432/mydb

With this setup, your app talks to the database at ‘db:5432’ rather than an IP. Credentials should go into environment variables or an env file so you don’t have to hardcode anything. Compose automatically creates a shared network, so the containers can communicate cleanly without extra config. If you’re pulling the code from GitHub, let GitHub Actions build and push your application image, then run it with compose on your server.

https://devops-daily.com/posts/rebuild-docker-container-compose

If you want to avoid managing the database yourself, you can also look into DigitalOcean Managed Databases which make this part much easier:

https://docs.digitalocean.com/products/databases/

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.