Docker app not able to communicate with database container

I have mysql DB running in a container and web app running in another container. My use case is once the DB container is up and running app container has to insert some initial data to DB using Liquibase and start the app. My docker yml looks like below.

db:
    build: kdb
    user: "1000:50"
    volumes:
      - /data/mysql:/var/lib/mysql
    container_name: kdb
    environment:  
      - MYSQL_ALLOW_EMPTY_PASSWORD=yes
    image:  kdb
    ports:
      - "3307:3306"



k-api:
    container_name: k_api
    hostname: k-api
    domainname: i.com
    image:  k_api
    volumes:
      - /Users/agu/work:/data
    build:
      context:  ./api
      args:
        KB_API_WAR: k-web-1.2.9.war 
        KB_API_URL: https://artifactory.b-aws.i.com
    ports:  
      - "8097:8080"
    depends_on:
      - db
    #command: [/usr/local/bin/wait-for-it.sh, "db:3306","-s","-t","0","--","/bin/sh" "wait_for_liquibase.sh"]
    links:
      - "db:kdb_docker_host"

And in my Dockerfile for api i have entry point for a shell script called "wait_for_liquibase.sh"

CMD ["wait_for_liquibase.sh"]

wait_for_liquibase.sh:

#!/bin/sh
set -e


#RUN liquibase
mvn clean install -X  -PdropAll -Dcontexts=test -Dliquibase.user=XX -Dliquibase.pass=XX -Dliquibase.host=db -Dliquibase.port=3306 -Dliquibase.schema=knowledgebasedb -DpromptOnNonLocalDatabase=false -Dcontexts=test -f k/k-liquibase

/usr/local/tomcat/bin/catalina.sh run

The issue is once the DB container is up and running app container is not able to reach the DB server to perform liquibase setup for database. I see the below error.

Communication failure: Unknown database host -Dliquibase.host=db.

2 answers

  • answered 2018-01-11 20:14 TLPNull

    I am assuming you are using version 1.

    You giving an alias to your "db" service, you will need to use that alias, kdb_docker_host

    Also, the ports are mapping to the host machine, to expose ports between containers yuo will need to use the expose property.

    expose:
      - 3306
    

  • answered 2018-01-11 20:24 Kilian

    I used this in Docker file RUN apt-get update RUN apt-get install netcat -y ADD wait-for-base.sh /wait-for-base.sh CMD ["/wait-for-base.sh"]

    and in wait-for-base.sh :

    #!/bin/bash
    
    while ! nc -z db 3306; do sleep 3; done
    [my command to run]
    

    In your case /usr/local/tomcat/bin/catalina.sh run