title: java.net.UnknownHostException: host.docker.internal: Name or service not known on AWS EC2
body: <p>I ran into this &quot;java.net.UnknownHostException: host.docker.internal: Name or service not known&quot; 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.</p>
  <p>But the weird part is, I did not use the variable &quot;host.docker.internal&quot; anywhere in my application: not in the code, not in the yaml file, not in the .env file:</p>
  <pre><code>$ sudo grep -Rl &quot;host.docker.internal&quot; ~
  /home/ec2-user/.bash_history
  </code></pre>
  <p>And when I run the following command it shows nothing but previous command to search for it:</p>
  <blockquote>
  <p>$ cat /home/ec2-user/.bash_history | grep &quot;host.docker.internal&quot;</p>
  </blockquote>
  <p>Locally I am using Windows 10 for development, and I can successfully bring up the stack with docker-compose.</p>
  <p>Here is the EC2 instance OS version info:</p>
  <pre><code>$ cat /etc/*release
  NAME=&quot;Amazon Linux&quot;
  VERSION=&quot;2&quot;
  ID=&quot;amzn&quot;
  ID_LIKE=&quot;centos rhel fedora&quot;
  VERSION_ID=&quot;2&quot;
  PRETTY_NAME=&quot;Amazon Linux 2&quot;
  ANSI_COLOR=&quot;0;33&quot;
  CPE_NAME=&quot;cpe:2.3:o:amazon:amazon_linux:2&quot;
  HOME_URL=&quot;https://amazonlinux.com/&quot;
  Amazon Linux release 2 (Karoo)
  </code></pre>
  <p>And here is the docker-compose file that I used on the EC2 instance:</p>
  <pre><code>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:
  </code></pre>
  <p>And here is my .env file on the EC2 instance:</p>
  <pre><code>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
  </code></pre>
  <p>I am pretty sure that this .env file is being used when bringing up the stack with &quot;docker-compose up&quot; because I can see the SERVER_PORT in the log matches this file when I change it.</p>
  <blockquote>
  <p>2021-01-02 20:55:44.870 [main] INFO  o.s.b.w.e.tomcat.TomcatWebServer - Tomcat initialized with port(s): <strong>8080</strong> (https)</p>
  </blockquote>
  <p>But I keep getting the error complaining about &quot;host.docker.internal&quot;.</p>
  <p>Here are things that I have tried but not working:</p>
  <ol>
  <li>Hard-code the db host in property spring.datasource.url in application.yml</li>
  <li>Add the following entry to /etc/hosts file (see <a href="https://stackoverflow.com/a/48547074/1852496">https://stackoverflow.com/a/48547074/1852496</a>)</li>
  </ol>
  <blockquote>
  <p>172.17.0.1    host.docker.internal</p>
  </blockquote>
  <ol start="3">
  <li>Add the following entry to /etc/hosts file, where &quot;ip-172-31-33-56.us-east-2.compute.internal&quot; is what I got when running command &quot;echo $HOSTNAME&quot;</li>
  </ol>
  <blockquote>
  <p>ip-172-31-33-56.us-east-2.compute.internal    host.docker.internal</p>
  </blockquote>
  <ol start="4">
  <li>Terminate the instance and created another T2.micro instance, but got same result.</li>
  <li>Edit inbound rules to allow TCP:3306 from anywhere.</li>
  </ol>
  <p>Can someone take a look? Any help appreciated.</p>