Report this

What is the reason for this report?

Setup Adminer, Postgres and Traefik on a single VPS

Posted on January 11, 2022

I am not quite sure how to setup networks in docker-compose.yml for Postgres and Adminer. All examples I can find are about Postgres and Adminer in same docker-compose.yml meaning separate Adminer for each app, and I dont want that, I want single Adminer for entire VPS instance.

I have another docker-compose.yml with Traefik and Portainer, and I want to add Adminer in that file but I am not sure how to set up networks.

I am aware that with Adminer I can connect to any database with IP, but since this is same VPS I guess Adminer can read hostname trough Docker network.

I have proxy external network that Traefik uses to discover containers. I assume I can’t have Adminer and Postgres on same internal network since they aren’t defined in same container.

Note that I need Postgres to be available for connections from external servers as I will use it’s connection string in Github actions for building the app container, Next.js app needs to read data to prerender pages.

Do you have advice or example how to setup networks? Here is my incomplete setup so far:

docker-compose.yml for reverse proxy in which I want to add Adminer

services:
  traefik:
	image: "traefik:v2.5.6"
	networks:
	  - proxy
  ...

  adminer:
	image: adminer:4.8.1-standalone
	networks:
	  - proxy
	labels:
	  - "traefik.http.routers.adminer.rule=Host(`adminer.${DOMAIN}`)"
	  - "traefik.http.services.adminer.loadbalancer.server.port=8080"

networks:
  proxy:
	external: true

docker-compose.yml for my app, Adminer should connect to this Postgres container

 app:
	depends_on:
	  - postgres-db-prod
  ...
	networks:
	  - proxy
	  - internal

  postgres-db-prod:
	image: postgres:14-alpine
	container_name: postgres-db-prod
	restart: unless-stopped
	ports:
	  - 5432:5432
	volumes:
	  - ./prisma/pg-data:/var/lib/postgresql/data
	env_file:
	  - .env.local
	labels:
	  - 'traefik.enable=false'
	networks:
	  - proxy
	  - internal

networks:
  proxy:
	external: true
  internal:
	external: false




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!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

Setting up Adminer, PostgreSQL, and Traefik on a single VPS using Docker and docker-compose is a common scenario. Your goal of having a single instance of Adminer for the entire VPS is practical, and it can be achieved by correctly configuring your Docker networks.

The key here is to ensure that Adminer is on the same network as your PostgreSQL database so it can communicate with it, and also on the network used by Traefik for routing.

Based on your provided docker-compose files, here’s a suggested setup:

  1. Traefik and Adminer Setup: In your reverse proxy docker-compose file where you have Traefik and want to add Adminer, you’re on the right track. You’ll need to make sure that Adminer is on the proxy network so that Traefik can route requests to it. Your configuration for this part seems correct.

  2. PostgreSQL Setup: In your application’s docker-compose file, where you have your PostgreSQL database, you should ensure that the database is also on the proxy network. This allows Traefik to manage requests to the database if needed (for external connections). However, for Adminer to access PostgreSQL, they don’t necessarily need to be on the same network since Adminer can connect to PostgreSQL using the internal Docker DNS system.

  3. Internal Network: You mentioned an internal network, which is great for internal communication between your app and the database. But since Adminer will be connecting from a different docker-compose setup, it won’t have access to this internal network. You can either add Adminer to this internal network or use the Docker DNS system for Adminer to connect to the PostgreSQL database.

  4. Connecting Adminer to PostgreSQL: Adminer will need the hostname of your PostgreSQL container to connect to it. In Docker, the default network mode provides a DNS resolver that allows containers to communicate by container names. So, in Adminer, you should be able to use the name of your PostgreSQL service (e.g., postgres-db-prod) as the host to connect to.

  5. External Access to PostgreSQL: If you need PostgreSQL to be accessible from external servers (as mentioned for GitHub actions), the ports configuration in your PostgreSQL service is necessary. Just ensure that your VPS and firewalls are configured to allow traffic on the relevant port (5432 in this case).

Here’s a revision of your setup with these considerations:

# Docker-compose for Traefik and Adminer
services:
  traefik:
    # ... your traefik configuration
    networks:
      - proxy

  adminer:
    image: adminer:4.8.1-standalone
    networks:
      - proxy
    labels:
      - "traefik.http.routers.adminer.rule=Host(`adminer.${DOMAIN}`)"
      - "traefik.http.services.adminer.loadbalancer.server.port=8080"

networks:
  proxy:
    external: true

# Docker-compose for your app and PostgreSQL
services:
  app:
    # ... your app configuration
    networks:
      - internal

  postgres-db-prod:
    image: postgres:14-alpine
    container_name: postgres-db-prod
    restart: unless-stopped
    ports:
      - "5432:5432"
    volumes:
      - ./prisma/pg-data:/var/lib/postgresql/data
    env_file:
      - .env.local
    networks:
      - internal

networks:
  internal:
    name: internal_network


    driver: bridge

# Note: No need to attach 'postgres-db-prod' to the 'proxy' network
# as it's not directly served by Traefik. Adminer accesses it over Docker's internal DNS.

In this configuration:

  • Both Adminer and PostgreSQL are on their respective networks. Adminer uses the proxy network to be reachable via Traefik, and PostgreSQL is on the internal network for communication with your app.
  • Adminer can connect to PostgreSQL using the PostgreSQL container’s name (postgres-db-prod) as the host in the connection settings.
  • The internal network is defined to facilitate communication between your app and the PostgreSQL database.
  • PostgreSQL is exposed on port 5432 for external connections, as needed for your GitHub actions.

This setup should allow you to have a single Adminer instance on your VPS that can access your PostgreSQL database, while also maintaining the necessary network separations for security and organization. Remember to replace internal_network with a unique name specific to your setup to avoid conflicts with other networks.

Best,

Bobby

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.