I have been struggling to get connection to postgresql container from both localhost and from outside.
Here is the very nice demo which works as such very well as it is demonstrated at https://linuxhint.com/postgresql_docker/. And below is the docker-compose.yml by which the postgres:12.2 and pgadmin are containerized and run. And indeed after running the docker-compose.yml file (by docker-compose up -d) they work.
You can then go in your browser to http://localhost:8080/ and have the direct access (with given crendentials, i.e., [email protected], secret) to pgadmin and start operating on your postgres:12.2 installation on docker.
However my difficulties start when I try to find get connection to this postgres:12.2 -database directly from localhost or outside/some application/ip (like Java Spring Boot).
Admittedly I am not an expert with docker, just the student - so, I asking for help. Hopefully someone can show me what should be done to make connection possible to (this) containerized postgres database from outside/app. I am using Win 10.
UPDATE:
Can anyone say why a web-app (added below to docker.compose.yml, see below) is shutting down persistently after its launching?
docker logs gives the following error:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.postgresql.util.PSQLException: Connection to localhost:5433 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
here is Dockerfile for the app:
FROM maven:3-jdk-11
VOLUME /tmp
EXPOSE 8095
ADD /target/spring-boot-demo-0.0.1-SNAPSHOT.jar app.jar
RUN ls -ls
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]
and here is connection details in the application.properties file:
spring.datasource.url=jdbc:postgresql://db:5432/postgres
spring.datasource.username=admin
spring.datasource.password=secret
two other servies (db and pgadmin starts normally)
docker-compose.yml
version: "3.7"
services:
# ========== ADDED APP ===============
web:
#restart: on-failure
image: app-springboot-postgresql
build: ./
ports:
- "8095:8095"
environment:
WAIT_HOSTS: postgres:5432
depends_on:
- db
# ========== ADDED APP ===============
db:
image: postgres:12.2
restart: always
environment:
POSTGRES_DB: postgres
POSTGRES_USER: admin
POSTGRES_PASSWORD: secret
PGDATA: /var/lib/postgresql/data
volumes:
- db-data:/var/lib/postgresql/data
ports:
- "5432:5432"
pgadmin:
image: dpage/pgadmin4:4.18
restart: always
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: secret
PGADMIN_LISTEN_PORT: 80
ports:
- "8080:80"
volumes:
- pgadmin-data:/var/lib/pgadmin
links:
- "db:pgsql-server"
volumes:
db-data:
pgadmin-data:
Connection; nested exception is org.postgresql.util.PSQLException: Connection to localhost:5433 refused.you have the postgresql configured on 5433 and from yaml you start that on 5432