当前位置: 首页 > 知识库问答 >
问题:

Docker撰写错误上mysql db连接为Spring启动应用程序引起的:java.net.未知的主机异常:[重复]

苗征
2023-03-14

我有Spring靴应用

我的文件是

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

我的docker网络工作正常

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

应用程序正在使用java-jar-target\demo-0.0.1-SNAPSHOT成功运行。罐子

但当我做docker作曲的时候

我犯了错误

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

我可以访问mysql数据库和表,但从docker compose无法访问

任何建议都会很有帮助

共有1个答案

文自怡
2023-03-14

您需要向服务提供容器名称,并在相互引用它们时使用它们。在您的应用服务器环境部分中,数据库的url指向127.0.0.1但数据库与应用服务器不在同一个容器上运行,因此这将失败。要使此工作正常,请向服务提供容器名称,例如:my_mysql和my_app服务器,并在环境url中使用它作为jdbc: mysql://my_mysql: 3306。请参阅下面的修改文件:

# 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:
 类似资料:
  • 您好,我正在尝试获取rss源,代码如下: 它由和AsyncTask包装,我得到了这个日志: 有人知道吗?我也试过google news rss feed(https://news.google.com/news/rss/headlines/section/topic/WORLD?ned=us 方法parseFeed():

  • 我在JMeter上运行测试脚本时遇到过这种错误? java.net.unknownHostException:h2ebiz-testat java.net.inet6addressimpl.lookupAllHostAddr(本机方法)位于java.net.inetAddress.2.lookupAllHostAddr(未知源)位于java.net.inetAddress.getAddress.g

  • 在AWS EC2 T2上部署停靠的Spring Boot应用程序时,我遇到了这个“java.net.UnknownHostException:host.docker.internal:Name或service not known”问题。微实例。由于此错误,spring启动应用程序无法启动。 但奇怪的是,我没有在应用程序的任何地方使用变量“host.docker.internal”:不在代码中,不在

  • 我使用maven spring启动插件创建了一个jar,并尝试使用。主文件是用SpringBoot配置的,当我尝试打包它时,启动jar,我得到的错误是- 这表明打包的jar中缺少依赖项jar。我试着在网上寻找各种解决方案,但失败的是,没有一个对我有效。我检查了NoClassDefinitionFound和PackageDependencyJAR的答案,但它不起作用,每次都会出现相同的错误。下面是我

  • 以下是错误: 下面是我的pom.xml文件: 这是我的主课:

  • 我创建了一个JavaFX项目,运行时出现错误,我怀疑错误在以下主类中: 以下是错误: 经过一些研究,我发现错误最有可能存在于对FXML加载器的调用中,但是在尝试了几个不同的修复后,我一直无法找到解决方案。创建FXML对象是否错误?谢啦