P
Prisma2mo ago
elfensky

How do I connect to mysql on host from inside a docker container?

things I've done: - I changed the bind-address to 0.0.0.0 so mysql would allow connetions from the docker network instead of limiting itself to 127.0.0.1 - To my docker-compose.yml, I added
extra_hosts:
- "host.docker.internal:host-gateway"
extra_hosts:
- "host.docker.internal:host-gateway"
- I changed my db_url string to use internal string instead of localhost. DATABASE_URL="mysql://user:[email protected]/db_name" from what I know that should be enough, but I can't make a connection. telnet host.docker.internal 3306 doesn't seem to work either. What am I missing here? Container works fine if I use host networking, so it's not the password being wrong or prisma being misconfigured - seems to be a networking issue.
3 Replies
elfensky
elfenskyOP2mo ago
omg it was ufw being ass. now I need to figure out the rules, but disabling it seems to have fixes my issues
RaphaelEtim
RaphaelEtim2mo ago
I'm glad to hear you could figure this out.
elfensky
elfenskyOP2mo ago
I havent yet, but I at least know where to look. Still need to figure out how to configure docker and firewall to play nice with eachother. Okay so for any future people: 1. sudo ufw allow from 172.17.0.0/16 to any port 3306 to whitelist the default docker bridge network and allow it to access port 3306 (mysql) 2. docker compose creates a unique network unless you manually specify to use the default docker bridge network. This is how my docker-compose ended up looking like: Note the "network_mode" and "extra_hosts:" sections
services:
h1api:
image: elfensky/h1api:latest # Replace with the correct image name and tag
container_name: h1api # Optional: name your container
env_file:
- .env
ports:
- '52001:3000'
restart: unless-stopped # Automatically restart the container unless it is explicitly stopped
network_mode: bridge # Use the default, ufw-whitelisted Docker Network
extra_hosts:
- "host.docker.internal:host-gateway" # pass the host to container, required on linux
services:
h1api:
image: elfensky/h1api:latest # Replace with the correct image name and tag
container_name: h1api # Optional: name your container
env_file:
- .env
ports:
- '52001:3000'
restart: unless-stopped # Automatically restart the container unless it is explicitly stopped
network_mode: bridge # Use the default, ufw-whitelisted Docker Network
extra_hosts:
- "host.docker.internal:host-gateway" # pass the host to container, required on linux
can't mark as solution, app did not respond

Did you find this page helpful?