1

I am currently trying to find a unique solution for running my project locally and remotely on a CI-pipeline. On git I got everything working by setting the bean dataSource url of the applicationContext.xml as shown in the following snipped:

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://mysql:3306/project" />
    ...
</bean>

However, I was not able to configure my docker-compose file in a way that I can use the same url as shown previously. Instead it still requires the localhost jdbc:mysql://localhost:3306/project

I guess I need to add some additional networking fields to my docker-compose file, so that I can use jdbc:mysql://mysql:3306/project locally as well, but I could not figure it out by now.

My current docker-compose.yml to be modified:

version: '3.3'
services:
  tomcat:
    image: tomcat:9-jre11
    volumes:
      - ./target:/usr/local/tomcat/webapps
    ports:
      - "8080:8080"
  mysql:
    image: mysql:latest
    environment:
      MYSQL_USER: mysql_user
      MYSQL_PASSWORD: mysql_password
      MYSQL_ROOT_PASSWORD: mysql_root_password
      MYSQL_DATABASE: mysql_database
    ports:
      - "3306:3306"

Running the project locally leads to the following error message if I use the mysql url instead of the local one:

Caused by: java.net.UnknownHostException: mysql
Harald
  • 526
  • 4
  • 26
  • Did you try host networking mode in compose? If your DB connects with the app outside docker, using host mode would be a good point to start troubleshooting. – Isuru Perera Jun 07 '22 at 18:37
  • The only thing you should need to do is set an environment variable for your Spring service, `SPRING_DATASOURCE_URL=jdbc:mysql://mysql`. Does [Externalising Spring Boot properties when deploying to Docker](https://stackoverflow.com/questions/46057625/externalising-spring-boot-properties-when-deploying-to-docker) describe your situation? – David Maze Jun 07 '22 at 19:33
  • Good idea, but I guess something like SPRING_DATASOURCE_URL would only work for my ci pipeline. In this case i would need to overwrite the `jdbc:mysql://localhost` url for the git ci pipeline to the mysql one. However, the app is run over maven. – Harald Jun 07 '22 at 20:24
  • You need to use docker networks. https://docs.docker.com/network/ – Amol Gharpure Jun 15 '22 at 18:58

2 Answers2

1

Sounds like you're starting your app locally outside of docker, is that correct?

In such case, not a perfect solution as you have to do it separately on each desktop, but you can add to your /etc/hosts, "mysql" host to localhost mapping:

127.0.0.1       mysql

Nevertheless, I recommend running your app over your tomcat docker image or having separate configuration for CI and local environments.

Marcin Szulc
  • 1,171
  • 1
  • 10
  • 21
0

This can happen sometimes,

Are you deploying in swarm mode?

Anyway the fix with in docker compose file for single node would look something like following.

version: '3.3'
services:
  tomcat:
    image: tomcat:9-jre11
    volumes:
      - ./target:/usr/local/tomcat/webapps
    ports:
      - "8080:8080"
    networks:
    - mynetwork

  mysql:
    image: mysql:latest
    environment:
      MYSQL_USER: mysql_user
      MYSQL_PASSWORD: mysql_password
      MYSQL_ROOT_PASSWORD: mysql_root_password
      MYSQL_DATABASE: mysql_database
    ports:
      - "3306:3306"
    networks:
    - mynetwork

networks:
  mynetwork:

Specifically in swarm across multiple nodes:

docker network create mynetwork --driver overlay --attachable

compose file:

version: '3.3'
services:
  tomcat:
    image: tomcat:9-jre11
    volumes:
      - ./target:/usr/local/tomcat/webapps
    ports:
      - "8080:8080"
    networks:
    - mynetwork

  mysql:
    image: mysql:latest
    environment:
      MYSQL_USER: mysql_user
      MYSQL_PASSWORD: mysql_password
      MYSQL_ROOT_PASSWORD: mysql_root_password
      MYSQL_DATABASE: mysql_database
    ports:
      - "3306:3306"
    networks:
    - mynetwork

networks:
  mynetwork:
    external: true
Raja Ravindra
  • 303
  • 2
  • 9