来自Fargate集群上AWS ECS docker容器中运行的Java应用程序的UnknownHostException

我有一个大型 Java应用程序,我正在尝试在AWS中的fargate集群上运行.图像在我的本地计算机的docker上成功运行.当我在fargate中运行它时它成功启动,但最终遇到以下错误,之后应用程序卡住了:

! java.net.UnknownHostException: 690bd678bcf4: 690bd678bcf4: Name or service not known
! at java.net.InetAddress.getLocalHost(InetAddress.java:1505) ~[na:1.8.0_151]
! at tracelink.misc.SingletonTokenDBO$.<init>(SingletonTokenDBO.scala:34) ~[habari.jar:8.4-QUARTZ-SNAPSHOT]
! at tracelink.misc.SingletonTokenDBO$.<clinit>(SingletonTokenDBO.scala) ~[habari.jar:8.4-QUARTZ-SNAPSHOT]
!... 10 common frames omitted
Caused by: ! java.net.UnknownHostException: 690bd678bcf4: Name or service not known
! at java.net.Inet4AddressImpl.lookupAllHostAddr(Native Method) ~[na:1.8.0_151]
! at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) ~[na:1.8.0_151]
! at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) ~[na:1.8.0_151]
! at java.net.InetAddress.getLocalHost(InetAddress.java:1500) ~[na:1.8.0_151]
!... 12 common frames omitted

Scala代码的违规行是:

private val machineName = InetAddress.getLocalHost().getHostName()

一些初步研究表明错误与容器中/ etc / hosts文件的内容有关.所以我创建了一个小的测试程序,它表现出与我的真实应用程序相同的行为,并且还将/ etc / hosts的内容转储到stdout:

import java.net.*;
import java.io.*;

public class NetworkTest {
   public static void main(String[] args) throws InterruptedException, IOException, FileNotFoundException {
      while(true) {
         networkDump();
         Thread.sleep(10000);
      }
   }

   private static void networkDump() throws IOException, FileNotFoundException {
      System.out.println("/etc/hosts:");
      System.out.println("");

      FileReader f = new FileReader("/etc/hosts");
      BufferedReader reader = new BufferedReader(f);
      String line = null;
      while((line = reader.readLine()) != null) {
         System.out.println(line);
      }
      System.out.println("");

      dumpHostname();
   }

   private static void dumpHostname() {
      try {
         String hostname = InetAddress.getLocalHost().getHostName();
         System.out.printf("Hostname: %s\n\n", hostname);
      } catch(UnknownHostException e) {
         System.out.println(e.getMessage());
      }
   }
}

Dockerfile:

FROM openjdk:8

WORKDIR /site
ADD . /site

CMD ["java", "NetworkTest"]

我从AWS获得的输出如下:

/etc/hosts:
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

3a5a4271a6e3: 3a5a4271a6e3: Name or service not known

与在我的本地机器上的docker中运行的输出相比:

> docker run networktest

/etc/hosts:
127.0.0.1   localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.17.0.4  82691e2fb948

Hostname: 82691e2fb948

未获得异常的本地版本在/ etc / hosts中具有主机名的条目,而AWS主机文件没有主机名的条目.我已经尝试添加一个/etc/rc.local文件来手动将主机名添加到localhost行的末尾,只需在Dockerfile中添加一个RUN命令来执行相同的操作.两者都没有任何影响.

有没有人知道是否有办法配置图像或ECS任务定义以获得在AWS中正确配置的主机名?

通过以下方式将主机名指向127.0.0.1:

echo "127.0.0.1 $HOSTNAME" >> /etc/hosts

修复了我的问题.

我正在使用Docker Compose.所以我有一个像这样的docker-compose.yml文件:

version: '2'

services:
  myservice:
    command: ["/set-hostname.sh", "--", "/run-service.sh"]

然后set-hostname.sh文件如下所示:

#!/bin/bash

set -e

shift
cmd="$@"

echo "127.0.0.1 $HOSTNAME" >> /etc/hosts

exec $cmd
相关文章
相关标签/搜索