Search code examples
postgresqlscalaplayframeworkdockerfiledev-to-production

Play Framework: use h2 database for development and postgresql in production mode and how to connect to the postgresql via conf-file


I have the following prod.conf, containing the relevant information on how to connect to my PostgresSql DB.

slick.dbs.default {
  profile = "slick.jdbc.PostgresProfile$"
  db.dataSourceClass = "slick.jdbc.DatabaseUrlDataSource"
  db.numThreads = 8
  maxConnections = 8
  driver="slick.jdbc.PostgresProfile$"
  db.driver="org.postgresql.Driver"
  url="jdbc:postgresql://db:5432/db"
  user="user"
  password="password"
}

(excerpt)

My Dockerfile looks like this

#...
RUN sbt stage

RUN cd target/universal/stage/bin

ENTRYPOINT ["target/universal/stage/bin/newsapi", "-Dplay.http.secret.key=KEY -Dconfig.resource=prod.conf"]

(excerpt)

With the -Dconfig.resource=prod.conf the application should start using the prod.conf and not the application.conf, right?

But when the application gets deployed it still uses a h2 database which is not part of the prod.conf and configured in the application.conf.

I dont get any warnings that the prod.conf could not be found/loaded or that the connection to the db could not be established.

What is the right way to solve this issue and is my db-url correct?

As a bit of context the postgresql db runs as a docker image as well as the play-applications. Bot are in the same network.


Solution

  • The -Dconfig.resource flag needs to be passed to the JVM, but you are passing it to your application instead.

    If you're using sbt-native-packager (which Play uses by default), you should be able to pass the flag to the JVM by prefixing it with -J. So you need to pass -J-Dconfig.resource=prod.conf.

    Here is the relevant documentation: https://www.scala-sbt.org/sbt-native-packager/archetypes/java_app/customize.html#via-build-sbt

    By the way, there is also a Docker plugin for sbt-native-packager. https://www.scala-sbt.org/sbt-native-packager/formats/docker.html I recommend you use it instead of writing Dockerfiles manually.