Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

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. The spring boot application failed to start because of this error.

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:

$ 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 | grep "host.docker.internal"

Locally I am using Windows 10 for development, and I can successfully bring up the stack with docker-compose.

Here is the EC2 instance OS version info:

$ 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:

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:

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.

2021-01-02 20:55:44.870 [main] INFO o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): 8080 (https)

But I keep getting the error complaining about "host.docker.internal".

Here are things that I have tried but not working:

  • Hard-code the db host in property spring.datasource.url in application.yml
  • Add the following entry to /etc/hosts file (see https://stackoverflow.com/a/48547074/1852496)
  • 172.17.0.1 host.docker.internal

  • 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"
  • ip-172-31-33-56.us-east-2.compute.internal host.docker.internal

  • Terminate the instance and created another T2.micro instance, but got same result.
  • Edit inbound rules to allow TCP:3306 from anywhere.
  • 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. Make sure the docker engine version is 20.10-beta1 or newer.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.