0

I have a Postgres server running inside Docker. Inside this Postgres server, I have a database named 'aa'.

I also have a Docker image of a Spring Boot Application. When this image is executed in Docker, database tables should be created inside database 'aa'.

In order to achieve this, I executed the following steps:

  1. Run the Postgres Server inside Docker

    docker run --name PostgresServer --e POSTGRES_PASSWORD=*** -d Postgres

enter image description here

  1. Enter PostgresServer then Create database 'aa'

    sudo docker exec -it PostgresServer psql -U postgres

    CREATE DATABASE AA;

enter image description here 3. Run the Sprint Boot Docker Image (this is where the problem happens)

docker run -v /Users/juancesard.pineda/Desktop/brapi:/home/brapi/properties -d brapicoordinatorselby/brapi-java-server:v2

enter image description here

  1. I checked the logs: It says database 'aa' does not exist wherein clearly it exists in the Postgres Server

    org.postgresql.util.PSQLException: FATAL: database "aa" does not exist

enter image description here

Some additional info:

  • Docker listens at port 8080

  • Postgres server listens at port 5432

  • My application.properties file looks like this:

    server.port = 8080 server.servlet.context-path=/Users/juancesard.pineda/Desktop/brapi/application.properties/germplasm

    spring.datasource.url=jdbc:postgresql://host.docker.internal:5432/aa spring.datasource.username=**** spring.datasource.password=****

    spring.datasource.driver-class-name=org.postgresql.Driver

    spring.jpa.hibernate.ddl-auto=create-drop spring.jpa.show-sql=false spring.jpa.properties.hibernate.hbm2ddl.import_files=sql/crops.sql, sql/lists.sql, sql/locations.sql, sql/people.sql, sql/programs.sql, sql/trials.sql, sql/seasons.sql, sql/studies.sql, sql/breeding_methods.sql, sql/germplasm.sql, sql/attribute_defs.sql, sql/attribute_values.sql, sql/seed_lots.sql, sql/observation_units.sql, sql/crosses.sql, sql/pedigree.sql, sql/events.sql, sql/images.sql, sql/observation_variables.sql, sql/observations.sql, sql/samples.sql, sql/allele_calls.sql, sql/genome_maps.sql, sql/references.sql, sql/vendor.sql

    spring.mvc.dispatch-options-request=true

Am I missing some config in my application.properties file?

Thank you in advance

2
  • keep sql independent of spring boot, you can create docker image extending postgres and execute you sql on startup, otherwise if you are using flyway it will do it for you Commented Oct 15, 2020 at 11:24
  • I think the application.properties file is usually a text file; you've attached an image to your question instead. Can you rewrite the question listing the relevant source code and commands you've run as plain text, not screen shots? Commented Oct 15, 2020 at 11:29

1 Answer 1

2

The problem is in your application.properties file. The datasource url is pointing to host.docker.internal. This should only be used when connecting something from inside a docker container to something running outside docker on the host machine (documentation). You need a different solution because your Spring server and Postgres server are both running inside docker containers.

You need a Docker Network, specifically a bridge network, to connect your different containers. Docker Bridge Networks

Try something like this

  1. Run this command to create a network in docker called “brapi_net”
    $> docker network create --driver bridge network_name

  2. Run the postgres container and link it to the network. Create your database schema as normal.
    $> docker run --name PostgresServer --network=network_name --e POSTGRES_PASSWORD=*** -d Postgres
    $> sudo docker exec -it PostgresServer psql -U postgres
    $> CREATE DATABASE aa;

  3. With a Docker network, container names act as server names. Modify your application.properties to reflect this
    spring.datasource.url=jdbc:postgresql://PostgresServer:5432/aa

  4. Run the server container and link it to the network.
    $> docker run -v /Users/juancesard.pineda/Desktop/brapi:/home/brapi/properties --network=network_name -d brapicoordinatorselby/brapi-java-server:v2

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.