Unable to Connect MySQL container to Tomcat Container in docker

前端 未结 2 333
说谎
说谎 2020-12-17 00:22

The Plan

I want my tomcat server to be able to connect to my MySQL server both in separate containers.

The Problem

相关标签:
2条回答
  • 2020-12-17 01:05

    As you're linking db as "db", you cannot use localhost to join you database. you should "db"

    jdbc:mysql://db:3306/tracker?useSSL=false
    

    In your container, localhost design your tomcat container, not your host. MySQL container has his own network.

    Futhermore, if you don't like "db" name, you can name link it with different name

    For exemple:

     links:
       - db:container-mysql
    

    In this case, inside you tomcat container, you could use

    jdbc:mysql://container-mysql:3306/tracker?useSSL=false
    
    0 讨论(0)
  • 2020-12-17 01:24

    If you are using a container already running or run by using separate DockerFile, then you might want to consider using

    external_links:
      -  mysql-container:db
    

    Note that the name mysql-container is the name of your container. If you don't know the name or haven't specified one in the docker-compose.yml you can also find it by running

    docker ps
    

    The column with the NAMES is the one you'll need in the external links. The db after : is just an alias and it could be anything you want. It will be your hostname. For example if before dockerizing the database your host was localhost you have to now change it into db.

    Lastly, you must also be sure to have both of the containers running in the same network. You can do that by creating a new network as:

    docker network create --driver=bridge my-network
    

    Then inside the docker-compose.yml of both the containers, add the following network configuration at the same indentation level as that of service

    networks:
      default:
        external:
          name: my-network
    
    0 讨论(0)
提交回复
热议问题