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 [] 3440 (?) : Connection refused

anyone know what I might be doing wrong?

  • 2
    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 – storm May 15 '18 at 8:23

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.
  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.
RUN apt-get install -y netcat
RUN nc localhost 3440 | tar -x -O > ores-$(date +%s.%N).tgz
# Define default command.
CMD ["bash"]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.