docker compose up gving error as I need to stop mysql service ERROR: for demo_db_1 Cannot start

一个人想着一个人 提交于 2020-03-25 19:21:30

问题


I have spring boot app with below Dockerfile

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"]

I can able to build docker image successfully

and this below is my docker-compose

# 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"
    environment:
      MYSQL_DATABASE: employee_entries
      MYSQL_HOST: 127.0.0.1
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_USER: root

Now I did

docker-compose up

Starting demo_db_1 ... error                                                                                                                                                
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)

ERROR: for db  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)
ERROR: Encountered errors while bringing up the project.

Now as per one SO solution they said they need to stop services so I stop mysql service.

Then it run the docker-compose up successfully.

but it is not able to send data into mysql from my spring app

it gave below error as service stopped

 The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
empserver     |         at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_212]
empserver     |         at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_212]
empserver     |         at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_212]
empserver     |         at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_212]
empserver     |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         at com.mysql.cj.NativeSession.connect(NativeSession.java:144) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         ... 56 common frames omitted
empserver     | Caused by: java.net.ConnectException: Connection refused (Connection refused)
empserver     |         at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_212]
empserver     |         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_212]
empserver     |         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_212]
empserver     |         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_212]
empserver     |         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_212]
empserver     |         at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_212]
empserver     |         at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.19.jar!/:8.0.19]
empserver     |         ... 59 common frames omitted

回答1:


It seems that application is booting up before the database server is available. You need to wait for db to be available and then start the application server. There is a utility available to just do this kind of synchronization. [https://github.com/ufoscout/docker-compose-wait]

Modify your dockerfile to include this script in it

FROM openjdk:8-jdk-alpine
EXPOSE 8080
ARG JAR_FILE=target/demo-0.0.1-SNAPSHOT.jar
ADD ${JAR_FILE} demo.jar
## Add the wait script to the image
ADD https://github.com/ufoscout/docker-compose-wait/releases/download/2.7.3/wait /wait
RUN chmod +x /wait
ENTRYPOINT ["java","-jar","/demo.jar"]

and with this you will have the wait script now part of your image and can be used as shown below: Please pay attention to command and environment vars added to app-server section. I am currently using this exact set up where i have 4 services and 2 database running in docker compose and all services need to wait for both db to be available and 2 of the 4 services need to wait for another service to be available.

# 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
    command: sh -c "/wait && java -jar /demo.jar"
    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     
      WAIT_HOSTS: db:3306
  # Database Service (Mysql)
  db:
    image: mysql:5.7
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: employee_entries
      MYSQL_HOST: 127.0.0.1
      MYSQL_USER: root
      MYSQL_PASSWORD: root
      MYSQL_ROOT_PASSWORD: root
      MYSQL_ROOT_USER: root
network:
  my-network:


来源:https://stackoverflow.com/questions/60147337/docker-compose-up-gving-error-as-i-need-to-stop-mysql-service-error-for-demo-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!