1

I have a Spring Boot application running inside of a Dockers container. The application offers REST endpoints, which can successfully be called from the host machine using http://localhost:8080/endpoint. Previously, when this application ran on the host machine and not within Dockers, I could call the local MariaDB using jdbc:mariadb://localhost:3308/fi?user=userName&password=thePassword from within the application. Now that the application is running inside of Dockers, the connection returns the error: "Could not connect to address=(host=localhost)(port=3308)(type=master) : Connection refused (Connection refused)"

The code snippet making the connection is as follows:

Connection connection = DriverManager.getConnection("jdbc:mariadb://localhost:3308/fi?user=username&password=thePassword");
        Statement stmt = connection.createStatement();
        String query = "";
        ResultSet rs = stmt.executeQuery("SELECT * ....;");

I already have the 8080 port published to accept the REST calls from the host machine, and have tried to publish or expose the 3308 port to allow for the database call to be made, with neither helping.

Thank you in advance!

2
  • I suggest you to run your db in another container - it is suggested way from docker. Commented Jun 27, 2017 at 19:22
  • Other than creating the other container and re-setting up the db, is there in other special changes that need to be made to allow the app in one docker to call the db in another? Commented Jun 27, 2017 at 19:28

2 Answers 2

1

Suggested way of using database with docker is to run it in separated container. Docker containers are in same (docker) network so you need to just link them. Here you have MariaDB image and guidelines how to use it. You can dump your existing database to your image (so you don't need to do it all again).

To do that you have to:

  1. Create new database image (dump database in it)
  2. Run database container (set name!)
  3. Run your REST container with link do database container
Sign up to request clarification or add additional context in comments.

2 Comments

The link pattern is an old pattern. Instead, it is recommended to use network service discovery. Basically, as long as your containers are all connected to the same docker network create type network, they will be able to resolve each other by their --name.
You are right - I wrote it from my memory and I always used --link :)
1

If you intented to do just development you can link containers or better you can use docker-compose. But be careful : database in containers are not for production!

From the container you can retrieve the docker host IP with ip route show

# ip route show
default via 172.17.0.1 dev eth0 
172.17.0.0/16 dev eth0  proto kernel  scope link  src 172.17.0.2

Then the default route is your host IP (here 172.17.0.1)

At this point, pay attention to the host's firewall which can block the connection

In your entrypoint script you can retrieve the host IP with ip route show | awk '/default/ {print $3}'. Then you can pass this IP through an environment variable and retrieve it in your application.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.