[英]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:以下是我尝试过但不起作用的事情:
172.17.0.1 host.docker.internal
172.17.0.1 主机.docker.internal
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
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.