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 Python Console via docker-compose on Pycharm
  • Sass is not found for gulp-ruby-sass within Docker container
  • docker restart on cascade
  • Docker private registry issue
  • connecting APIMAN to an external ElasticSearch database for metrics
  • How to add a production config when deploying a docker container?
  • 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.

    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'”.

  • Multiple applications running simultaneously on a single Docker image
  • Elastic Beanstalk Docker container running Node application exits early
  • Is docker phasing out some sites and services or something?
  • Does Docker purge old/unused images?
  • Jenkins With Nginx Reverse Proxy And Resolver
  • Database created in Dockerfile does not appear in running container
  • 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.


    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.