skip to Main Content

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

2

Answers


  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.

    Login or Signup to reply.
  2. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search