Francisco Albert
Francisco Albert

Reputation: 1721

How to pass the dynamic exposed port in container to my application?

I have this configuration for database in my akka project:

mydb = {
  driver = "com.mysql.jdbc.Driver"
  profile = "slick.jdbc.MySQLProfile$"
  jdbcUrl = "jdbc:mysql://127.0.0.1:3306/play_db?useSSL=false"
  username = "root"
  password = ""
}

I'm trying to build mysql in a container by using docker-compose. However, I need the port to be dynamic instead of always 3306 After running "docker-compose up" how can I pass this exposed port to my AKKA app?.

Exists any standard way?

Upvotes: 0

Views: 206

Answers (1)

atline
atline

Reputation: 31664

One solution is next:

  1. If you not care get the port of mysql, by pass next & go step 2.

    Otherwise, define db container's name in docker-compose.yml, e.g. db_container; then after docker-compose up, you could use:

    docker inspect --format='{{(index (index .NetworkSettings.Ports "80/tcp") 0).HostPort}}' db_container

    to get the dynamic changed host port which map for container's 3306 port.

  2. Let's suppose the port you dynamic get is 32768, then in the shell, which you launch your akka project, do next:

    export MYSQL_PORT=32768
    

    Modify application.conf of your akka project as next:

    jdbcUrl = "jdbc:mysql://127.0.0.1:"${MYSQL_PORT}"/play_db?useSSL=false"
    

    Then, when akka project starts, it will get the port from environment variable MYSQL_PORT.

Upvotes: 1

Related Questions