I have spring boot application which connects to mysql database. If I run it locally in a classic way it works fine. But when I try do dockerise it - database starts successfully but the application doesn't see the DB. 

My Dockerfile:

```
FROM openjdk:11-jdk-slim
ADD start-local.sh /start-local.sh
RUN chmod +x start-local.sh
ENTRYPOINT [ "sh", "-c", "./start-local.sh" ]

```

My start-local.sh (I added it in order my application waited for database start):

```
#!/bin/sh

echo "Waiting for the database server to start"
echo "********************************************************"
apt-get update && apt-get install -y netcat
while ! nc -z mysql 3306; do sleep 3; done
echo "******** Database Server has started"
echo "Starting account service"
java -jar /mnt/service-2.4.0.jar

```

And finally my docker-compose.yml file:

```
version: '2'
services:
  mysql:
    image: mysql:5.7.23
    environment:
      - MYSQL_ROOT_PASSWORD=root
    entrypoint:
      sh -c "echo 'CREATE DATABASE IF NOT EXISTS books;  CREATE DATABASE IF NOT EXISTS notifications' > /docker-entrypoint-initdb.d/init.sql;/usr/local/bin/docker-entrypoint.sh --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci --sql_mode="""
    ports:
      - "3306:3306"

  account_app:
    image: service_img
    environment:
      - "SPRING_PROFILES_ACTIVE=local"
    ports:
      - "8083:8083"
    volumes:
      - "/build/libs/:/mnt"

```

What I execute: 

* first of all, I build my service\_img
* then I execute docker-compose up.
In logs, I see database starting logs, after my application starting logs and then error:

> 
> | 2019-09-23 09:51:14.006 DEBUG [account-service,,,] 286 --- [  
> 
>  main] c.m.l.c.c.d.p.DatasourceConfiguration : Creating datasource  
> 
>  account\_app\_1 | 2019-09-23 09:51:14.014 INFO [account-service,,,]
>  286 --- [ main] com.zaxxer.hikari.HikariDataSource :
>  HikariPool-1 - Starting... account\_app\_1 | 2019-09-23 09:51:15.222
>  ERROR [account-service,,,] 286 --- [ main]
>  com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Exception
>  during pool initialization. account\_app\_1 | account\_app\_1 |
>  com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications
>  link failure account\_app\_1 | account\_app\_1 | The last packet sent
>  successfully to the server was 0 milliseconds ago. The driver has not
>  received any packets from the server. account\_app\_1 | at
>  com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] The last packet sent
>  successfully to the server was 0 milliseconds ago. The driver has not
>  received any packets from the server. account\_app\_1 | at
>  com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:835)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:455)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:240)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.zaxxer.hikari.util.DriverDataSource.getConnection(DriverDataSource.java:136)
>  ~[HikariCP-3.2.0.jar!/:na] account\_app\_1 | at
>  com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:369)
>  ~[HikariCP-3.2.0.jar!/:na] account\_app\_1 | at
>  com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:198)
>  ~[HikariCP-3.2.0.jar!/:na] account\_app\_1 | at
>  com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:467)
>  ~[HikariCP-3.2.0.jar!/:na] account\_app\_1 | at
>  com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:541)
>  ~[HikariCP-3.2.0.jar!/:na] account\_app\_1 | ... 206 common
>  frames omitted account\_app\_1 | Caused by:
>  com.mysql.cj.exceptions.CJCommunicationsException: Communications link
>  failure account\_app\_1 | account\_app\_1 | The last packet sent
>  successfully to the server was 0 milliseconds ago. The driver has not
>  received any packets from the server. account\_app\_1 | at
>  java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native
>  Method) ~[na:na] account\_app\_1 | at
>  java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
>  ~[na:na] account\_app\_1 | at
>  java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
>  ~[na:na] account\_app\_1 | at
>  java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
>  ~[na:na] account\_app\_1 | at
>  com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.NativeSession.connect(NativeSession.java:152)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:955)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:825)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 |  
> 
>  ... 214 common frames omitted account\_app\_1 | Caused by:
>  java.net.ConnectException: Connection refused (Connection refused)
>  account\_app\_1 | at
>  java.base/java.net.PlainSocketImpl.socketConnect(Native Method)
>  ~[na:na] account\_app\_1 | at
>  java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399)
>  ~[na:na] account\_app\_1 | at
>  java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:242)
>  ~[na:na] account\_app\_1 | at
>  java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:224)
>  ~[na:na] account\_app\_1 | at
>  java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:403)
>  ~[na:na] account\_app\_1 | at
>  java.base/java.net.Socket.connect(Socket.java:591) ~[na:na]
>  account\_app\_1 | at
>  com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 | at
>  com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)
>  ~[mysql-connector-java-8.0.15.jar!/:8.0.15] account\_app\_1 |  
> 
>  ... 217 common frames omitted account\_app\_1 |
> 
> 
> 

Any suggestions?