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:
What is the best/recommended way to:
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!
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:
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
Full documentation for every DigitalOcean product.
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.