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:

  • Run containers as other user
  • Don't have permission to access / on this server (apache2) in docker
  • Where to put folders for code in docker containers
  • connect robomongo to mongo instance running inside docker container
  • pm2: how to switch multiple app declaration json inside docker
  • Docker intermittently failing when building image
  • 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'
    ENV JAVA_OPTS=""
    ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -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:

    CREATE DATABASE IE;
    CREATE DATABASE IE_test;
    

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

    version: "3.0"
    
    services:
      database:
        environment:
          MYSQL_ROOT_PASSWORD: root
        build:
          context: *PATH_TO_DIR_WITH_DOCKERFILE*
          dockerfile: Dockerfile
        ports:
          - 3306:3306
        volumes:
          - db_data:/var/lib/mysql
    
      backend:
        build:
          context: *PATH_TO_DIR_WITH_DOCKERFILE*
          dockerfile: Dockerfile
        ports:
          - 3000:3000
        depends_on:
          - database
    
    volumes:
      db_data:
    

    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:

    app:
      data-base:
        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 (172.19.0.2): 56 data bytes
    64 bytes from 172.19.0.2: icmp_seq=0 ttl=64 time=0.086 ms
    64 bytes from 172.19.0.2: 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?

  • Docker service process zombie holding a Device Mapper managed device
  • maven-docker-plugin: how to get container ip address
  • When using a docker volume, are files up to date when the container is restarted?
  • Unable to correct problems, you have held broken packages
  • Install SCP in Docker container
  • Does docker stack deploy work with local images
  • 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.

    See: https://github.com/docker-library/healthcheck/tree/master/mysql

    Script from above link:

    #!/bin/bash
    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
    fi
    
    host="$(hostname --ip-address || echo '127.0.0.1')"
    user="${MYSQL_USER:-root}"
    export MYSQL_PWD="${MYSQL_PASSWORD:-$MYSQL_ROOT_PASSWORD}"
    
    args=(
        # force mysql to not use the local "mysqld.sock" (test "external" connectibility)
        -h"$host"
        -u"$user"
        --silent
    )
    
    if select="$(echo 'SELECT 1' | mysql "${args[@]}")" && [ "$select" = '1' ]; then
        exit 0
    fi
    
    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'
    ENV JAVA_OPTS=""
    ENTRYPOINT [ "sh", "-c", "java $JAVA_OPTS -Djava.security.egd=file:/dev/./urandom -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
    

    with

    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.