1

I was trying to dockerize my java application to get data from youtube and storing to local file. The app works on my local, but I am unable to get it to work in a docker container. The exception occurs when I try to connect to youtube.com via http. I am using the following dockerfile:

FROM maven:3.8 AS build
RUN mkdir /usr/src/project
COPY . /usr/src/project
WORKDIR /usr/src/project
RUN mvn clean install -DskipTests

FROM adoptopenjdk/openjdk11:alpine-jre
RUN mkdir /project
WORKDIR /project
RUN mkdir sharedZone
COPY --from=build /usr/src/project/target/YouTubePlaylistPuller-1.0-SNAPSHOT-jar-with-dependencies.jar /project/
COPY ./entrypoint.sh /project/
RUN echo 'hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4' >> /etc/nsswitch.conf
RUN chmod +x ./entrypoint.sh
ENTRYPOINT ["/project/entrypoint.sh"] // Entrypoint is java -jar with $@

I am getting the following exception:

java.lang.RuntimeException: java.net.UnknownHostException: www.youtube.comtrue
    at impl.MyApiMultithreaded$MyRunnableDataPuller.run(MyApiMultithreaded.java:77)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.net.UnknownHostException: www.youtube.comtrue
    at java.base/java.net.InetAddress$CachedAddresses.get(InetAddress.java:797)
    at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:883)
    at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1519)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1378)
    at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1306)
    at org.apache.http.impl.conn.SystemDefaultDnsResolver.resolve(SystemDefaultDnsResolver.java:45)
    at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:112)
    at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:376)
    at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:393)
    at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
    at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:186)
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:89)
    at org.apache.http.impl.execchain.RedirectExec.execute(RedirectExec.java:110)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:83)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:108)
    at impl.MyApi.pullData(MyApi.java:23)
    at impl.MyApiMultithreaded$MyRunnableDataPuller.run(MyApiMultithreaded.java:75)
    ... 3 more

My docker run is: docker run -it --network=host --add-host='myMachineHostName':127.0.0.1 -u 1000 -v /root/sharedFolder/:/project/sharedZone/ youtube-test-23 /project/sharedZone/inputs/1.properties

Things that I tried:

  1. DNS correction - Docker openjdk:8 UnknownHostException

    • exec adoptopenjdk/openjdk11:alpine-jre, and I got these entries for /etc/hosts

      /project # cat /etc/hosts
      127.0.0.1   localhost
      ...
      

      and these entries for ```/etc/resolv.conf nameserver 8.8.8.8 nameserver 8.8.4.4 search lan example.com

  2. Sending hostname and add-host path - https://medium.com/yohan-liyanage/docker-machine-moby-name-or-service-not-known-bd2a7c74abc2

    • Still does not work
  3. java.net.UnknownHostException on Docker

    • No, I already have the entry for 127.0.0.1 localhost I am unable to understand wht am I getting unknown host exception, even when I am using all default options for network on docker. Even without the --network=host flag, I am getting the same exception.
  4. Specifying the dns with docker run - UnknownHostException within Docker Container on Alpine openjdk:8-jdk-alpine

    • No

What should I do to correct this? Please help.

NiharGht
  • 151
  • 5
  • 10
  • 2
    Take a look at the url `www.youtube.comtrue`, it doesn't look like YouTube url – Leonardo Emmanuel de Azevedo Oct 09 '22 at 16:33
  • 2
    It seems that you are sending requests to `www.youtube.comtrue`, which is apparently not a valid domain on the Internet. It might be related to your business logic; could you post the code that sends requests as well? – Yang Hanlin Oct 09 '22 at 16:34
  • Yeah that was the issue. I had checked it using the test cases, but the order of the arguments was changed when I was passing it to the docker run. :( – NiharGht Oct 09 '22 at 16:48

0 Answers0