1

I'm having trouble accessing a database created from a docker-compose file.

Given the following compose file, I should be able to connect to it from java using something like:

jdbc:postgresql://eprase:eprase@database:7000/eprase

However, the connection is rejected. I can't even use PGAdmin to connect it using the same details to create a new server.

I've entered the database container and ran psql commands to verify that the eprase user and database have been created according to postgres Docker documentation, everything seems fine. I can't tell if the problem is within the database container or something I need to change in the compose network.

The client & server services can largely be ignored, the server is a java based web API and the client is an Angular app.

Compose file:

version: "3"
services:
  client:
    image: eprase/client:latest
    build: ./client/eprase-app
    networks:
      api:
    ports:
      - "5000:80"
    tty: true
    depends_on:
      - server
  server:
    image: eprase/server:latest
    build: ./server
    networks:
      api:
    ports:
      - "6000:8080"
    depends_on:
      - database
  database:
    image: postgres:9
    volumes:
      - "./database/data:/var/lib/postgresql/data"
    environment:
      - "POSTGRES_USER=eprase"
      - "POSTGRES_PASSWORD=eprase"
      - "POSTGRES_DB=eprase"
    networks:
      api:
    ports:
      - "7000:5432"
    restart: unless-stopped
  pgadmin:
    image: dpage/pgadmin4:latest
    environment: 
      - "[email protected]"
      - "PGADMIN_DEFAULT_PASSWORD=eprase"
    networks:
      api:
    ports:
      - "8000:80"
    depends_on:
      - database
networks:
  api:

1 Answer 1

5

The PostgreSQL database is listening on container port 5432. The 7000:5432 line is mapping host port 7000 to container port 5432. That allows you to connect to the database on port 7000. But, your services on a common network (api) should communicate with each other via the container ports.

So, from the perspective of the containers for the client and server services, the connection string should be:

jdbc:postgresql://eprase:eprase@database:5432/eprase
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.