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.

  • Run a database migration command when deploying a Docker container to AWS
  • Docker container - how to configure so it gets a viable IP address when running in vagrant?
  • Can not ping docker in macOS
  • Getting “broken pipe” when building Docker image using boot2docker Mac
  • docker expose ports only on host
  • Cannot SSH using private key while building Docker image
  • 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'”.

  • port isolation with bazel inside docker
  • Docker architecture for many PHP containers
  • Rails engine apps deployed with docker
  • is it possible to speed up writes inside a docker container?
  • Managing dev/test/prod environments with Docker
  • Docker image already pushed issue?
  • 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.