1

I have a host that runs a native mysql installation (not a container).

From a docker container I now want to connect from a java spring-boot application to that port (3306 by default).

But it does not work:

docker-compose.yml:

version: '3.7'
services:
  customer-app:
    ports:
      - "3306:3306"
    environment:
      SPRING_DATASOURCE_URL: jdbc:mysql://localhost:3306/db         
  

Result from docker-compose up:

Cannot start service customer-app: driver failed programming external connectivity on endpoint:
Error starting userland proxy: listen tcp4 0.0.0.0:3306: bind: address already in use

This is probably not a question directly to a java application, but more general:

How can I access a port on the host system from inside a docker container?

Gyro Gearless
  • 5,131
  • 3
  • 18
  • 14
membersound
  • 81,582
  • 193
  • 585
  • 1,120
  • localhost inside a container acts the same way as the loopback on a host machine. I.e. your container tries to connect to itself. – tbjorch Apr 20 '22 at 08:43
  • maybe try `network_mode: host` for the container – Tobias S. Apr 20 '22 at 08:45
  • Here you are telling docker to create your customer-app on port 3306 which is already used for your local mysql installation. @tbjorch gave you the good process – Reynadan Apr 20 '22 at 09:10

1 Answers1

1

I added the following to docker-compose.yml:

extra_hosts:
  - "host.docker.internal:host-gateway"
environment:
  SPRING_DATASOURCE_URL: jdbc:mysql://host.docker.internal:3306/db  
membersound
  • 81,582
  • 193
  • 585
  • 1,120