HectorOfTroy407
HectorOfTroy407

Reputation: 1907

Connection Refused Docker Run

I'm getting a connection refused after building my Docker image and running docker run -t imageName

Inside the container my python script is making web requests (external API call) and then communicating over localhost:5000 to a logstash socket.

My dockerfile is really simple:

FROM ubuntu:14.04

RUN apt-get update -y

RUN apt-get install -y nginx git python-setuptools python-dev

RUN easy_install pip

#Install app dependencies
RUN pip install requests configparser

EXPOSE 80
EXPOSE 5000

#Add project directory 
ADD . /usr/local/scripts/

#Set default working directory
WORKDIR /usr/local/scripts

CMD ["python", "logs.py"]

However, I get a [ERROR] Connection refused message when I try to run this. It's not immediately obvious to me what I'm doing wrong here - I believe I'm opening 80 and 5000 to the outside world? Is this incorrect? Thanks.

Upvotes: 2

Views: 2515

Answers (1)

Robert
Robert

Reputation: 36823

Regarding EXPOSE:

Each container you run has its own network interface. Doing EXPOSE 5000 tell docker to link a port 5000 from container-network-interface to a random port in your host machine (see it with docker ps), as long as you tell docker to do it when you docker run with -P.

Regarding logstash.

If your logstash is installed in your host, or in another container, it means that logstash is not in the "localhost" of the container (remember that each container has its own network interface, each one has its own localhost). So you need to point to logstash properly.

How?

Method 1:

Don't give container its own iterface, so it has the same localhost as your machine:

docker run --net=host ...

Method 2:

If you are using docker-compose, use the docker network linking. i.e:

services:
  py_app:
    ...
    links:
      - logstash
  logstash:
     image: .../logstash..

So point as this: logstash:5000 (docker will resolve that name to the internal IP corresponding to logstash)


Method 3:

If logstash listen in your localhost:5000 (from your host), you can point to it as this: 172.17.0.1:5000 from inside your container (the 172.17.0.1 is the host fixed IP, but this option is less elegant, arguably)

Upvotes: 3

Related Questions