Accessing a MariaDB in one Docker from another Docker

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.

  • Docker PHP and FreeTDS -cannot find freetds in know installation directories
  • How to deploy jar/war to nexus in the docker build?
  • Using Docker with Sensu
  • Does docker support command aliases similar to Git aliases?
  • Error when loading a docker imager
  • Rename docker machine
  • 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'”.

  • Restarting Docker Container after reboot
  • Docker is installed but Docker Compose is not ? why?
  • rolling deployment for docker containers behind load balancer
  • How can I convert a Docker image into a (vagrant) VirtualBox box?
  • Liberty Admin Center shows Docker server as running while it is not
  • Set http_proxy for Docker parent image build
  • One Solution collect form web for “Accessing a MariaDB in one Docker from another Docker”

    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
    
    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.