this post was submitted on 02 May 2024
8 points (100.0% liked)

Docker

1104 readers
1 users here now

founded 1 year ago
MODERATORS
 

I have my main compose file which has a bunch of services in and while it makes it easier to manage, it's also limiting when I wanna use postgres:// to access a database rather than exposing a port. I'm wondering if I can remedy this by moving it to a new network and(?) stack?

If so, is it just as simple as adding

networks
  - new network name
stacks
  - new stacks name

I'm still curious as to the answer, but it's not something I need.

you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 6 months ago (1 children)

I'm guessing you're exposing the postgres on the host and then referring the host IP from the postgres:// connection string?

Docker services configured in the same compose already have a bridge network set up among them, they get a random subnet, a gateway, random IPs in that subnet, and also name resolution.

So all you have to do is stop using ports: on minifluxdb to expose to host, and in the miniflux connect string just use "minifluxdb" (the container name) as the hostname. Docker DNS will resolve that name to whatever IP gets allocated on the private subnet.

If you want you can define a different hostname from the container name with the "hostname:" directive.

Have a look at docker network list too.

[–] [email protected] 1 points 6 months ago (1 children)

First off, apologies for the delay. But my brain just didn't wanna think about compose yesterday.

Okay, so the reason I've ended up with postgres://username:password@ipaddress:port was because I'm running a bunch of different postgres databases within the compose file and once I removed the port, compose kept trying to look at the wrong database. What I was essentially trying to do was get it to look for the right database by creating its own stack and network and then not having to open the port to the outside.

Does that make sense? I could totally be overthinking things.

[–] [email protected] 2 points 6 months ago (1 children)

Try using the postgres hostname instead of "ipaddress", it should work. Postgres doesn't have to be exposed on host for this to work, and all compose files will automatically create a bridge network for all services defined in it so you don't technically need to make an explicit network (unless you really need to define IPs or MACs or some other parameters).

[–] [email protected] 1 points 6 months ago

I'll give it a go. Thank you.