简体   繁体   English

java.net.UnknownHostException:host.docker.internal:AWS EC2 上的名称或服务未知

[英]java.net.UnknownHostException: host.docker.internal: Name or service not known on AWS EC2

I ran into this "java.net.UnknownHostException: host.docker.internal: Name or service not known" problem when deploying a dockerized spring boot application on an AWS EC2 T2.micro instance.在 AWS EC2 T2.micro 实例上部署 dockerized spring 启动应用程序时,我遇到了这个“java.net.UnknownHostException:host.docker.internal:名称或服务未知”问题。 The spring boot application failed to start because of this error.由于此错误,spring 引导应用程序无法启动。

But the weird part is, I did not use the variable "host.docker.internal" anywhere in my application: not in the code, not in the yaml file, not in the.env file:但奇怪的是,我没有在我的应用程序的任何地方使用变量“host.docker.internal”:不在代码中,不在 yaml 文件中,不在 .env 文件中:

$ sudo grep -Rl "host.docker.internal" ~
/home/ec2-user/.bash_history

And when I run the following command it shows nothing but previous command to search for it:当我运行以下命令时,它只显示前一个命令来搜索它:

$ cat /home/ec2-user/.bash_history | $ cat /home/ec2-user/.bash_history | grep "host.docker.internal" grep "host.docker.internal"

Locally I am using Windows 10 for development, and I can successfully bring up the stack with docker-compose.在本地我使用 Windows 10 进行开发,我可以使用 docker-compose 成功启动堆栈。

Here is the EC2 instance OS version info:这是 EC2 实例操作系统版本信息:

$ cat /etc/*release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
Amazon Linux release 2 (Karoo)

And here is the docker-compose file that I used on the EC2 instance:这是我在 EC2 实例上使用的 docker-compose 文件:

version: '2'
services:
  backend:
    container_name: backend
    image: 'dockerhubuser/backend:0.0.4'
    ports:
      - '8080:8080'
    volumes:
      - /var/log/backend/logs:/var/log/backend/logs
      - ./backend-ssl:/etc/ssh/backend
    env_file:
      - .env
    depends_on:
      - mysql
      - redis
  redis:
    container_name: redis
    image: 'redis:alpine'
    ports:
      - '6379:6379'
    volumes:
      - $PWD/redis/redis-data:/var/lib/redis
      - $PWD/redis/redis.conf:/usr/local/etc/redis/redis.conf
  mysql:
    container_name: mysql
    image: 'mysql:8.0.21'
    ports:
      - '3306:3306'
    environment:
      MYSQL_DATABASE: dbname
      MYSQL_USER: dbuser
      MYSQL_PASSWORD: dbpass
      MYSQL_ROOT_PASSWORD: dbrootpass
    volumes:
      - ./my_volume/mysql:/var/lib/mysql
volumes:
  my_volume:

And here is my.env file on the EC2 instance:这是 EC2 实例上的 my.env 文件:

SERVER_PORT=8080
KEY_STORE=/etc/ssh/backend/keystore.p12
KEY_STORE_PASSWORD=keystorepass
REDIS_HOST=redis
REDIS_PORT=6379
DB_HOST=mysql
DB_PORT=3306
DB_USERNAME=dbuser
DB_PASSWORD=dbpass

I am pretty sure that this.env file is being used when bringing up the stack with "docker-compose up" because I can see the SERVER_PORT in the log matches this file when I change it.我很确定在使用“docker-compose up”启动堆栈时正在使用 this.env 文件,因为当我更改它时,我可以看到日志中的 SERVER_PORT 与此文件匹配。

2021-01-02 20:55:44.870 [main] INFO osbwetomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (https) 2021-01-02 20:55:44.870 [main] INFO osbwetomcat.TomcatWebServer - Tomcat 初始化端口: 8080 (https)

But I keep getting the error complaining about "host.docker.internal".但我不断收到抱怨“host.docker.internal”的错误。

Here are things that I have tried but not working:以下是我尝试过但不起作用的事情:

  1. Hard-code the db host in property spring.datasource.url in application.yml在 application.yml 中的属性 spring.datasource.url 中硬编码数据库主机
  2. Add the following entry to /etc/hosts file (see https://stackoverflow.com/a/48547074/1852496 )将以下条目添加到 /etc/hosts 文件(请参阅https://stackoverflow.com/a/48547074/1852496

172.17.0.1 host.docker.internal 172.17.0.1 主机.docker.internal

  1. Add the following entry to /etc/hosts file, where "ip-172-31-33-56.us-east-2.compute.internal" is what I got when running command "echo $HOSTNAME"将以下条目添加到 /etc/hosts 文件,其中“ip-172-31-33-56.us-east-2.compute.internal”是我在运行命令“echo $HOSTNAME”时得到的

ip-172-31-33-56.us-east-2.compute.internal host.docker.internal ip-172-31-33-56.us-east-2.compute.internal host.docker.internal

  1. Terminate the instance and created another T2.micro instance, but got same result.终止实例并创建另一个 T2.micro 实例,但得到相同的结果。
  2. Edit inbound rules to allow TCP:3306 from anywhere.编辑入站规则以允许来自任何地方的 TCP:3306。

Can someone take a look?有人可以看看吗? Any help appreciated.任何帮助表示赞赏。

It works on Ubuntu 20.04 after adding "172.17.0.1 host.docker.internal" to /etc/hosts file.在将“172.17.0.1 host.docker.internal”添加到 /etc/hosts 文件后,它适用于 Ubuntu 20.04。 Make sure the docker engine version is 20.10-beta1 or newer.确保 docker 引擎版本为 20.10-beta1 或更高版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Docker RabbitMQ Spring java.net.UnknownHostException:rabbitmq:名称或服务未知 - Docker RabbitMQ Spring java.net.UnknownHostException: rabbitmq: Name or service not known java.net.UnknownHostException Spring 引导 AWS EC2 Apache 卡夫卡 - java.net.UnknownHostException Spring Boot AWS EC2 Apache Kafka 获取 java.net.UnknownHostException: hostname: Name or service not known while using spring-data-redis-starter - Getting java.net.UnknownHostException: hostname: Name or service not known while using spring-data-redis-starter Eureka 服务发现期间的 java.net.UnknownHostException - java.net.UnknownHostException during Eureka service discovery java.net.UnknownHostException - Zuul和Eureka在Docker中无法正常工作 - java.net.UnknownHostException - Zuul and Eureka not working inside Docker 引起:java.net.UnknownHostException:mysqldb - Caused by: java.net.UnknownHostException: mysqldb java.net.UnknownHostException:在 Spring Boot 和 Cloud 中进行 4 次查询后无法解析“inventory-service” - java.net.UnknownHostException: Failed to resolve 'inventory-service' after 4 queries in Spring Boot and Cloud spring 启动云 eurka windows 10 eurkea 在最新的 docker 升级后返回 host.docker.internal 作为客户端主机名 - spring boot cloud eurka windows 10 eurkea returns host.docker.internal for client host name after latest docker upgrade java.net.UnknownHostException来自spring boot应用程序的dockerized mysql - java.net.UnknownHostException dockerized mysql from spring boot application Docker - AWS EC2 上的 springboot - Docker - springboot on AWS EC2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM