-1

I'm trying to create docker containers on mi local enviroment. We have a Micronaut (Java) application and a MySQL database.

I created a container for my database with:

docker run -p 3307:3306 --name mysql-example -e MYSQL_ROOT_PASSWORD=password -d mysql

When I connect from my app in and embbebed server outside of docker we can connect without problems. Mi application.yml have the next configuration:

datasources:
  default:
    url: jdbc:mysql://localhost:3307/example?allowPublicKeyRetrieval=true&generateSimpleParameterMetadata=true&zeroDateTimeBehavior=convertToNull&verifyServerCertificate=false&useSSL=false
    driverClassName: com.mysql.cj.jdbc.Driver
    username: root
    password: password
    schema-generate: CREATE_DROP
    dialect: MYSQL

Then I tried to create the Micronaut app image and run it on a container. My Dockerfile contains:

FROM maven:3.6.1-jdk-8
COPY target/example-0.1.jar example.jar
EXPOSE 8080
CMD java -Dcom.sun.management.jmxremote -Xmx128m -jar example.jar

I first generate JAR:

mvnw package

And then I generate docker image:

docker build -t example .

And when I test my app deployed in a container I have the next exception:

Caused by: java.net.ConnectException: Connection refused (Connection refused)

Could you help me with this issue? Thanks :-)

Shadow
  • 33,525
  • 10
  • 51
  • 64
  • Questions like [Communications link failure , Spring Boot + MySql +Docker + Hibernate](https://stackoverflow.com/questions/58880998/communications-link-failure-spring-boot-mysql-docker-hibernate) discuss this, but they are almost all focused on a Docker Compose setup, which provides some of the required networking setup for you. Also see the newer answers to [How to communicate between Docker containers via "hostname"](https://stackoverflow.com/questions/30545023/how-to-communicate-between-docker-containers-via-hostname). – David Maze Feb 01 '22 at 10:35

1 Answers1

1

Since you run your MySQL container with --name mysql-example then you should connect it as

jdbc:mysql://mysql-example:3307

not

jdbc:mysql://localhost:3307
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42
  • You need to use the standard MySQL port 3306 (connections between containers ignore `docker run -p` port mappings), and both containers need to be on the same `docker run --net` network. – David Maze Feb 01 '22 at 10:32
  • I tried to create containers using: `docker run --network=host --name mysql-example -e MYSQL_ROOT_PASSWORD=password -d mysql` `docker run --network=host --name example-container example` But I received: `java.net.UnknownHostException: mysql-example: Name or service not known` – alvaromorala Feb 01 '22 at 12:06