2

I have two Docker containers. One is based off of the standard MariaDB image. The container is named "mariadb", with one database inside of it called "fi". The fi db has a few tables inside of it, each with a couple rows of data. Using DataGrip or any other database viewing software, I can successfully access and query this database calling it on the localhost using port 3306.

My second Docker container runs a Spring Boot application, which needs to access the data in the MariaDB. If I run the MariaDB in the docker, and the Spring Boot application on the host machine, I can successfully access the MariaDB in the docker using the connection string: "jdbc:mariadb://localhost:3306/?user=myUsername&password=myPassword". My issue is, when I try to run them both in separate Dockers, the Spring Boot application cannot access the database. I have found a lot of people saying to "Link" the two dockers, but this does not seem to of solved the problem. When I run the Spring Boot docker, I execute the command "docker run -i -p 8080:8080 --link mariadb:db javaImage /bin/bash". I am publishing the 8080 port for reasons un-related to this issue. From what I have understood, their should now be an environment variable inside of my Spring Boot docker accessible with the key word "db", which would be replaced by an environment variable that is the actual ip of the MariaDB docker.

As of now, in my Java code making the connection (or well trying) is using the connection string "jdbc:mariadb://db:3306/fi?user=myUsername&password=myPassword". This returns the error "Could not connect: Unknown database 'fi'", which is odd, since if this connection was truly connecting to the MariaDB docker, the fi database would be found.

Any help is appreciated. Thanks!

Edit: Code Snippet

        Connection connection = DriverManager.getConnection("jdbc:mariadb://mariadb:3306/fi?user=myUsername&password=myPassword");
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT * FROM ...;");

Made change to connection string as recommended below, as shown above. Now running the container with the command "run -i -p 8080:8080 --link mariadb javaImage /bin/bash". Same result, receiving this error when trying to make the connection "Could not connect: Unknown database 'fi'".

dFrancisco
  • 896
  • 1
  • 11
  • 21
  • In the mean time I recommend formatting your questions according to guidelines, especially quoting the code snippets. – techtabu Jun 28 '17 at 14:50

1 Answers1

4

If the container is named mariadb, you should use it in the url, not db.

jdbc:mariadb://mariadb:3306/fi?user=myUsername&password=myPassword

On your link also you should use mariadb,

docker run -i -p 8080:8080 --link mariadb javaImage /bin/bash
techtabu
  • 23,241
  • 3
  • 25
  • 34
  • Please see the Edit: and below component of question. Does not seem to of fixed the problem, made both the change to the connection string and the run command. – dFrancisco Jun 28 '17 at 15:04
  • 1
    Can you post your result what you get when you do "docker ps". Also, is there any chance a local mysql instance running in your computer, you might be connecting to it when you use localhost? – techtabu Jun 28 '17 at 15:30
  • Yup I figured out the problem, you are right. There was some other local MySQL db on the host machine (that I was unaware of) that when I thought I was inserting the tables into the Docker MariaDB, I was inserting them into the host db. I have now properly inserted the tables into the actual Docker MariaDB and everything works. Thank you – dFrancisco Jun 28 '17 at 15:47
  • Hi, please see this answer in link below, I think that help you with this problem. https://stackoverflow.com/a/53431294/1939983 Thanks, – LandiLeite Aug 03 '20 at 17:27