docker network not working without link

256 Views Asked by At

I have a mysql container and a service container that needs to connect to the mysql container.

I created a networking with docker network create chrisbolton

I am spinning up the mysql container with

docker run --name chrisbolton-mysql -v /Users/Bolton/chrisbolton-data:/var/lib/mysql --network chrisbolton -e MYSQL_ROOT_PASSWORD=password -e MYSQL_DATABASE=chrisbolton -d mysql:latest

I am spinning up the service container with

docker run -p 8080:8080 --name chrisbolton-service --network chrisbolton --link chrisbolton-mysql:mysql -d chrisbolton-service

However, I know that link is deprecated and I need to move to only using networks. But if I remove the link here the two containers cannot communicate.

I am connecting to mysql with the following config:

#disbale Spring banner
spring.main.banner-mode=off

spring.datasource.url=jdbc:mysql://mysql:3306/chrisbolton
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

I have done a docker network inspect chrisbolton to get the IPAddresses and have tried changing my config file to point to that directly. But still not sure why it won't connect.

1

There are 1 best solutions below

0
On BEST ANSWER

datasource.url is not correct, you should specify the name of the container to which you want to connect. There are better options than using the name of service. You can use aliases or hostname.

While in this case, as you indicated in comments, datasource.url needs to be jdbc:mysql://chrisbolton-mysql:3306/chrisbolton