Answers: <p>First you have to start netcat listening in the background.
  $ netcat -l 3440 &amp;</p>
  
  <p>Then build your Dockerfile.</p>
  
  <pre><code>#
  # Ubuntu Dockerfile
  #
  # https://github.com/dockerfile/ubuntu
  #
  
  # Pull base image.
  FROM ubuntu:14.04
  
  # Install.
  RUN \
    sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list &amp;&amp; \
    apt-get update &amp;&amp; \
    apt-get -y upgrade &amp;&amp; \
    apt-get install -y build-essential &amp;&amp; \
    apt-get install -y software-properties-common &amp;&amp; \
    apt-get install -y byobu curl git htop man unzip vim wget &amp;&amp; \
    rm -rf /var/lib/apt/lists/*
  
  
  # Set environment variables.
  ENV HOME /root
  
  # Define working directory.
  WORKDIR /root
  RUN apt-get install -y netcat
  RUN nc localhost 3440 | tar -x -O &gt; ores-$(date +%s.%N).tgz
  # Define default command.
  CMD ["bash"]
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2018-05-15T09:25:03.180Z
Answers: 2018-05-15T09:25:03.180Z
Answers: James Knott
Body: <p>I have a server running on localhost:3440 on the host machine.
  In a Dockerfile I have:</p>
  
  <pre><code>RUN apt-get install -y netcat
  RUN nc localhost 3440 | tar -x -O &gt; ores-$(date +%s.%N).tgz
  </code></pre>
  
  <p>I've read that using <code>--net="host"</code> with <code>docker run</code> will allow a <em>running container</em> to connect to a server running on the host. But my question is - when <em>building an image</em>, how can I connect to a server on localhost?</p>
  
  <p>The error I get is simply:</p>
  
  <blockquote>
    <p>localhost [127.0.0.1] 3440 (?) : Connection refused</p>
  </blockquote>
  
  <p>anyone know what I might be doing wrong?</p>
  
Comments: CC BY-SA 4.0
Comments: 2018-05-15T08:23:53.030Z
Comments: You have to change localhost to the host machine IP address as nc is run inside the container localhost will refer to the container local address
Comments: storm
Tags: <docker><dockerfile>
Title: Cannot connect to server running on host from Dockerfile / image build
UserDisplayName: Alexander Mills
ThreadText: # Cannot connect to server running on host from Dockerfile / image build
  
  (Asked by: username_0 on 2018-05-15)
  
  I have a server running on localhost:3440 on the host machine.
  In a Dockerfile I have:
  
  ```
  RUN apt-get install -y netcat
  RUN nc localhost 3440 | tar -x -O > ores-$(date +%s.%N).tgz
  
  ```
  
  I've read that using `--net="host"` with `docker run` will allow a *running container* to connect to a server running on the host. But my question is - when *building an image*, how can I connect to a server on localhost?
  
  The error I get is simply:
  
  > localhost [127.0.0.1] 3440 (?) : Connection refused
  
  anyone know what I might be doing wrong?
  
  **Tags:** docker, dockerfile
  
  > **Comments:**
  > **username_1**: You have to change localhost to the host machine IP address as nc is run inside the container localhost will refer to the container local address  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_2 on 2018-05-15. Score: 1
  
  First you have to start netcat listening in the background.
  $ netcat -l 3440 &
  
  Then build your Dockerfile.
  
  ```
  #
  # Ubuntu Dockerfile
  #
  # https://github.com/dockerfile/ubuntu
  #
  
  # Pull base image.
  FROM ubuntu:14.04
  
  # Install.
  RUN \
    sed -i 's/# \(.*multiverse$\)/\1/g' /etc/apt/sources.list && \
    apt-get update && \
    apt-get -y upgrade && \
    apt-get install -y build-essential && \
    apt-get install -y software-properties-common && \
    apt-get install -y byobu curl git htop man unzip vim wget && \
    rm -rf /var/lib/apt/lists/*
  
  # Set environment variables.
  ENV HOME /root
  
  # Define working directory.
  WORKDIR /root
  RUN apt-get install -y netcat
  RUN nc localhost 3440 | tar -x -O > ores-$(date +%s.%N).tgz
  # Define default command.
  CMD ["bash"]
  
  ```
  
  
  ---
  
  