[Nevermind] Is it possible to move a service to a new network and stack within the same compose file

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


<span style="color:#323232;">networks
</span><span style="color:#323232;">  - new network name
</span><span style="color:#323232;">stacks
</span><span style="color:#323232;">  - new stacks name
</span>

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

lemmyvore,

it’s also limiting when I wanna use postgres:// to access a database rather than exposing a port.

What does this mean? I don’t understand what you’re trying to change, or what you want to achieve.

It would also help to explain how the services are networked now. Post a sanitized compose file for example.

sabreW4K3,
@sabreW4K3@lazysoci.al avatar

I’m an idiot, I apparently already did what I was trying to do


<span style="color:#323232;">  miniflux:
</span><span style="color:#323232;">    container_name: miniflux
</span><span style="color:#323232;">    image: miniflux/miniflux:latest
</span><span style="color:#323232;">    ports:
</span><span style="color:#323232;">      - <redacted>:8080
</span><span style="color:#323232;">    depends_on:
</span><span style="color:#323232;">      minifluxdb:
</span><span style="color:#323232;">        condition: service_healthy
</span><span style="color:#323232;">    environment:
</span><span style="color:#323232;">      DATABASE_URL: postgres://<redacted>:<redacted>@<redacted>:<redacted>/miniflux?sslmode=disable
</span><span style="color:#323232;">      RUN_MIGRATIONS: 1
</span><span style="color:#323232;">      CREATE_ADMIN: 1
</span><span style="color:#323232;">      ADMIN_USERNAME: <redacted>
</span><span style="color:#323232;">      ADMIN_PASSWORD: <redacted>
</span><span style="color:#323232;">  minifluxdb:
</span><span style="color:#323232;">    container_name: minifluxdb
</span><span style="color:#323232;">    image: postgres:15
</span><span style="color:#323232;">    environment:
</span><span style="color:#323232;">      POSTGRES_USER: <redacted>
</span><span style="color:#323232;">      POSTGRES_PASSWORD: <redacted>
</span><span style="color:#323232;">      POSTGRES_DB: miniflux
</span><span style="color:#323232;">    volumes:
</span><span style="color:#323232;">      - /opt/miniflux/postgres:/var/lib/postgresql/data
</span><span style="color:#323232;">    ports:
</span><span style="color:#323232;">      - <redacted>:5432
</span><span style="color:#323232;">    healthcheck:
</span><span style="color:#323232;">      test: ["CMD", "pg_isready", "-U", "miniflux"]
</span><span style="color:#323232;">      interval: 10s
</span><span style="color:#323232;">      start_period: 30s
</span>
lemmyvore,

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.

sabreW4K3,
@sabreW4K3@lazysoci.al avatar

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.

lemmyvore,

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).

sabreW4K3,
@sabreW4K3@lazysoci.al avatar

I’ll give it a go. Thank you.

  • All
  • Subscribed
  • Moderated
  • Favorites
  • docker@programming.dev
  • khanakhh
  • magazineikmin
  • osvaldo12
  • GTA5RPClips
  • mdbf
  • Youngstown
  • tacticalgear
  • slotface
  • rosin
  • kavyap
  • ethstaker
  • everett
  • thenastyranch
  • DreamBathrooms
  • provamag3
  • InstantRegret
  • normalnudes
  • cubers
  • Leos
  • ngwrru68w68
  • cisconetworking
  • modclub
  • Durango
  • megavids
  • anitta
  • tester
  • JUstTest
  • lostlight
  • All magazines