Can't connect to db docker container

currently working on moving our application to start using docker. It’s a typical app with backend and frontend. I don’t have any troubles with front, while still can’t launch back.

I have Docker file for backend:

  • Tag latest not found in repository<package>
  • How can I get the IP address of a unit started on a machine and use it in another unit with fleet?
  • Docker stats from java
  • How do I know whether “init” or “systemd” controls the docker?
  • How to resolve a PHP-FPM Primary script unknown with a PHP-FPM and an Nginx Docker container?
  • Some RUNs won't work on docker but will when inside a container
  • FROM williamyeh/java8
    RUN apt-get -y update && apt-get install -y maven
    WORKDIR /explorerbackend
    ADD settings.xml /root/.m2/settings.xml
    ADD pom.xml /explorerbackend
    ADD src /explorerbackend/src
    RUN ["mvn", "clean", "install"]
    ADD target/explorer-backend-1.0.jar /explorerbackend/app.jar
    RUN sh -c 'touch /explorerbackend/app.jar'
    ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /explorerbackend/app.jar" ]

    and Docker file for mysql:

    FROM mysql
    ADD createDB.sql /docker-entrypoint-initdb.d

    The reason i’m using a separate Docker file for mysql instead of just using image in docker-compose is necessity to create 2 databases on start (otherwise backend will not launch)

    createDB.sql file looks as:


    Now i have docker-compose.yml file which is supposed to start 2 containers and make backend connect to database:

    version: "3.0"
          MYSQL_ROOT_PASSWORD: root
          context: *PATH_TO_DIR_WITH_DOCKERFILE*
          dockerfile: Dockerfile
          - 3306:3306
          - db_data:/var/lib/mysql
          context: *PATH_TO_DIR_WITH_DOCKERFILE*
          dockerfile: Dockerfile
          - 3000:3000
          - database

    When I run the command docker-compose up database container is up and running while backend is failing:

    backend_1   | java.sql.SQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

    However I’m able to log in to database container and I do see databases created:

    mysql> show databases;
    | Database           |
    | information_schema |
    | IE                 |
    | IE_test            |
    | mysql              |
    | performance_schema |
    | sys                |
    6 rows in set (0.00 sec)

    The only reason I see might be related to yml property file of backend:

        name: IE
        link: database
        port: 3306

    From the frontend container I’m able to ping database (but am I allowed to put into property file just link:database):

    root@897b187f9042:/frontend# ping database
    PING database ( 56 data bytes
    64 bytes from icmp_seq=0 ttl=64 time=0.086 ms
    64 bytes from icmp_seq=1 ttl=64 time=0.088 ms

    So, I assume it’s pingable from backend container as well, but why it’s not able to connect to db server?

  • Running Sidekiq inside Docker - Handle termination of Docker container?
  • Why does docker login hang on Travis CI?
  • Should I add the reverse proxy in the same container as the application on docker?
  • Install Docker on OpenVZ VPS
  • Cannot login to Nexus 3 docker registry
  • what triggers Elastic Beanstalk to pull in an updated Docker image
  • 2 Solutions collect form web for “Can't connect to db docker container”

    MySQL takes a few seconds to start-up. In-order to confirm this is a race-condition, try the following:

    $ docker-compose up -d database && sleep 5 && docker-compose up

    When/if this confirms the race-condition, you can alleviate that with a HEALTHCHECK on your database image.


    Script from above link:

    set -eo pipefail
    if [ "$MYSQL_RANDOM_ROOT_PASSWORD" ] && [ -z "$MYSQL_USER" ] && [ -z "$MYSQL_PASSWORD" ]; then
        # there's no way we can guess what the random MySQL password was
        echo >&2 'healthcheck error: cannot determine random root password (and MYSQL_USER and MYSQL_PASSWORD were not set)'
        exit 0
    host="$(hostname --ip-address || echo '')"
        # force mysql to not use the local "mysqld.sock" (test "external" connectibility)
    if select="$(echo 'SELECT 1' | mysql "${args[@]}")" && [ "$select" = '1' ]; then
        exit 0
    exit 1

    Eventually, we found the problem which is a kind of oversight.
    The root cause was backend dockerfile:

    FROM williamyeh/java8
    RUN apt-get -y update && apt-get install -y maven
    WORKDIR /explorerbackend
    ADD settings.xml /root/.m2/settings.xml
    ADD pom.xml /explorerbackend
    ADD src /explorerbackend/src
    RUN ["mvn", "clean", "install"]
    ADD target/explorer-backend-1.0.jar /explorerbackend/app.jar
    RUN sh -c 'touch /explorerbackend/app.jar'
    ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -jar /explorerbackend/app.jar" ]

    The idea is pretty simple:
    1. Take java image
    2. install maven
    3. copy src folder of my project from host
    4. install with maven in container
    5. move jar to workdir inside container
    6. launch it

    However, option 5. doesn’t look correct, as instead of copying jar file what was just created by maven inside container i was copying it from my host.

    Issue was resolved simply replacing

    ADD target/explorer-backend-1.0.jar /explorerbackend/app.jar


    RUN cp /explorerbackend/target/explorer-backend-1.0.jar /explorerbackend/app.jar

    Thanks Rawcode for looking into it!

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.