0

I have spring boot app

My Dockerfile is

FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/demo-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} demo.jar
ENTRYPOINT ["java","-jar","/demo.jar"]


My docker compose file
# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)

version: '3.7'

# Define services
services:
  # App backend service
  app-server:
    # Configuration for building the docker image for the backend service
    build:
      context: . # Use an image built from the specified dockerfile in the `polling-app-server` directory.
      dockerfile: ./Dockerfile
    container_name: empserver
    ports:
      - "3000:3000" # Forward the exposed port 8080 on the container to port 8080 on the host machine
    restart: always
    depends_on: 
      - db # This service depends on mysql. Start that first.
    environment: # Pass environment variables to the service
      SPRING_DATASOURCE_URL: jdbc:mysql://db:3306/employee_entries?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root     

  # Database Service (Mysql)
  db:
    image: mysql:5.7
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_DATABASE: employee_entries
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root

My docker net works

NETWORK ID          NAME                DRIVER              SCOPE
b95e3d99b266        Default Switch      ics                 local
7fff4f9713f8        demo_default        nat                 local
fe8883b77d1d        emp-mysql           ics                 local
f464aab9064a        nat                 nat                 local
a5bd5e8efe61        none                null                local

App is successfully running using java -jar target\demo-0.0.1-SNAPSHOT.jar

but when I am doing docker-compose up

I got below error

app-server_1  | Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure
app-server_1  |
app-server_1  | The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
app-server_1  |         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_212]
app-server_1  |         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
app-server_1  |         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
app-server_1  |         at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
app-server_1  |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         ... 56 common frames omitted
app-server_1  | Caused by: java.net.UnknownHostException: db
app-server_1  |         at java.net.InetAddress.getAllByName0(InetAddress.java:1281) ~[na:1.8.0_212]
app-server_1  |         at java.net.InetAddress.getAllByName(InetAddress.java:1193) ~[na:1.8.0_212]
app-server_1  |         at java.net.InetAddress.getAllByName(InetAddress.java:1127) ~[na:1.8.0_212]
app-server_1  |         at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
app-server_1  |         ... 59 common frames omitted

I am able to access mysql database and tables but from docker compose it was not

any suggestion would really be helpful

Rajarshi Das
  • 11,778
  • 6
  • 46
  • 74
  • I am not completely sure, but don't you have to specify the used network for your services within your `docker-compose.yml`? – Jan Held Feb 07 '20 at 09:00
  • yes I also specified it but still getting same error – Rajarshi Das Feb 07 '20 at 09:06
  • 2
    Change `jdbc:mysql://127.0.0.1:3306/....` with `jdbc:mysql://db:3306/....`. On spring container nothing runs on `3306`. Docker-compose's service name is resolved to host names on a docker network. – leopal Feb 07 '20 at 09:13
  • Thank you leopal but now I am getting the error Caused by: java.net.UnknownHostException: db updated my question – Rajarshi Das Feb 09 '20 at 15:44

1 Answers1

2

You need to provide the container names to the services and use them when referring them from each other. In your environment section for app-server, the url for database points to 127.0.0.1 but the database is not running on same container as app-server so this will fail. To make this work, provide container names to services for eg : my_mysql and my_app-server and use it in environment url as jdbc:mysql://my_mysql:3306. Please see the modified file below:

# Docker Compose file Reference (https://docs.docker.com/compose/compose-file/)

version: '3.7'

# Define services
services:
  # App backend service
  app-server:
    # Configuration for building the docker image for the backend service
    build:
      context: . # Use an image built from the specified dockerfile in the `polling-app-server` directory.
      dockerfile: ./Dockerfile
    container_name: my_app-server
    ports:
      - "3000:3000" # Forward the exposed port 8080 on the container to port 8080 on the host machine
    restart: always
    depends_on: 
      - db # This service depends on mysql. Start that first.
    environment: # Pass environment variables to the service
      SPRING_DATASOURCE_URL: jdbc:mysql://my_mysql:3306/employee_entries?useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
      SPRING_DATASOURCE_USERNAME: root
      SPRING_DATASOURCE_PASSWORD: root     

  # Database Service (Mysql)
  db:
    image: mysql:5.7
    container_name: my_mysql
    ports:
      - "3306:3306"
    restart: always
    environment:
      MYSQL_DATABASE: employees
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
network:
  my-network:
blankCoder
  • 172
  • 1
  • 9
  • 1
    You can use `db` (the Compose service name block) as a host name. You don't need to set `container_name:`. – David Maze Feb 07 '20 at 10:07
  • 1
    I got this error now ` Caused by: java.net.UnknownHostException: my_mysql my_app-server | at java.net.InetAddress.getAllByName0(InetAddress.jav` – Rajarshi Das Feb 09 '20 at 15:36
  • also tried db Caused by: java.net.UnknownHostException: db – Rajarshi Das Feb 09 '20 at 15:41
  • @DavidMaze thank you for your suggestion and also I have update my question with error now – Rajarshi Das Feb 09 '20 at 16:01
  • @RajarshiDas I forgot to add network to the docker compose i mentioned in the answer above. Please check the updated answer. We need to add a network to compose so that all containers are able to communicate with each other. – blankCoder Feb 12 '20 at 04:46
  • @blankcoder problem is that while I am running docker-compose up in windows it got service already in use error so I stopped the service `ERROR: for demo_db_1 Cannot start service db: failed to create endpoint demo_db_1 on network demo_default: hnsCall failed in Win32: The process cannot access the file because it is being used by another process. (0x20)` now if I ran the docker-compose up it is showing the unknown host error https://stackoverflow.com/questions/60147337/docker-compose-up-gving-error-as-i-need-to-stop-mysql-service-error-for-demo-db – Rajarshi Das Feb 12 '20 at 07:29