Connecting machines on a private network

To keep things isolated we’re trying to use private networks. I’m trying to connect our phoenix app to a database within the network and have been unable to create a connection. Here are some relevant files/snippets

fly.database.toml

app = 'db-staging'
primary_region = 'lax'

[build]
dockerfile = 'Dockerfile.database'

[env]
POSTGRES_USER = 'postgres'
POSTGRES_DB = 'staging'

[mounts]
source = 'pg_data_staging'
destination = '/var/lib/postgresql/data'

[[services]]
internal_port = 5432
protocol = 'tcp'
auto_stop_machines = false
auto_start_machines = true

[[vm]]
memory = '1gb'
cpu_kind = 'shared'
cpus = 1

Dockerfile.database

FROM postgres:16-alpine

# Set default environment variables
ENV POSTGRES_USER=postgres
ENV POSTGRES_DB=staging
ENV PGDATA=/var/lib/postgresql/data/pgdata

# Create directory for custom initialization scripts
RUN mkdir -p /docker-entrypoint-initdb.d

# Add custom PostgreSQL configuration for production
RUN echo "listen_addresses = '*'" >> /usr/local/share/postgresql/postgresql.conf.sample && \
    echo "max_connections = 100" >> /usr/local/share/postgresql/postgresql.conf.sample && \
    echo "shared_buffers = 128MB" >> /usr/local/share/postgresql/postgresql.conf.sample && \
    echo "effective_cache_size = 256MB" >> /usr/local/share/postgresql/postgresql.conf.sample && \
    echo "work_mem = 4MB" >> /usr/local/share/postgresql/postgresql.conf.sample && \
    echo "maintenance_work_mem = 64MB" >> /usr/local/share/postgresql/postgresql.conf.sample

# Expose PostgreSQL port
EXPOSE 5432

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
    CMD pg_isready -U $POSTGRES_USER -d $POSTGRES_DB || exit 1

# Use the default entrypoint from the postgres image
CMD ["postgres"]

fly configuration

fly apps create "$DB_APP_NAME" --network "$NETWORK_NAME"
<create db volume>
<set db secrets>
fly deploy -c fly.database.toml

fly apps create "$APP_NAME" --network "$NETWORK_NAME"
<set app secrets>
fly deploy -c fly.staging.toml

db connection url

postgres://postgres:<password>@db-staging.internal:5432/staging

resulting error

[error] Postgrex.Protocol (#PID<0.151.0>) failed to connect: ** (DBConnection.ConnectionError) tcp connect (fdaa:3a:2b1b:0:1::2:5432): non-existing domain - :nxdomain

I’ve tried setting up a flycast ip like so

fly ips allocate-v6 --private --network <network name> -a db-staging

and then used .flycast rather than .internal. I’ve also tried using the ip directly to no avail.

What is the recommended configuration to use private networks with an app and database?

The error sounds like your app did successfully resolve the .internal domain, but it does not support IPv6 (at least by default) and is treating the IPv6 address as a domain. Is there an option somewhere to enable IPv6? Without IPv6, .flycast wouldn’t work either.

This was it! I hadn’t enabled ipv6 for ecto.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.