Connection Refused Docker Run - python

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.

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)

Related

Docker executes python scripts only when I stopping the container

I'm trying to build a Docker image for my python app (a small api on aiohttp with a couple endpoints)
FROM python:3
WORKDIR /home/emil/Projects/elastic_simple_engine
COPY . .
RUN pip3 install -r requirements.txt
EXPOSE 5000/tcp
CMD ["python3", "entry.py"]
The last line of the Dockerfile runs a python script which starts aiohttp.web.Application():
# entry.py
# ...a few dozens of code lines above...
if __name__ == '__main__':
print('Initializing...')
aiohttp.web.run_app(app, host='127.0.0.1', port=5000)
After building an image I'm trying to run the container:
$ docker run -p 5000:5000 myapp
Docker runs the container silently without any output in shell but I can't reach my app's host: 127.0.0.1:5000 (everything works perfectly when I launch it without docker).
Only when I stop the container it prints in console the lines that should be shown during app's launch and shuts down:
Initializing...
======== Running on http://127.0.0.1:5000 ========
(Press CTRL+C to quit)
Please, help me figure out that I do wrong.
TLDR
Set host to 0.0.0.0
127.0.0.1 is the IP address to local interface. It can only communicate within the same host.
0.0.0.0 means a server (in this context) would listen to every available network interface (including 127.0.0.1).
Here, since you are not sharing the docker network, 127.0.0.1 is only available inside the container and not from outside the container. You should use 0.0.0.0 to access it from outside the container or pass --network="host" in docker run but this can have other complications with port sharing.

Container didn't respond to HTTP pings on port: 80, failing site start

I have deployed python:3.8-slim-buster image to the App Service. Generally it is being run correctly as I can see the processing in the logs, however the health-check mechanism tries to ping the hosted server but it does not respond as it is only code that runs in a loop and process the messages from the queue.
It would be fine, but the application is being killed with the error:
Container didn't respond to HTTP pings on port: 80, failing site start.
Stopping site because it failed during startup.
Is there either a way to remove this Waiting for response to warmup request for container or specify in the dockerfile to respond with OK to those requests?
Currently my dockerfile is a 2 liner, that only copies the scripts and then runs python script.
The code that is inside this script is copied from https://learn.microsoft.com/en-us/azure/event-hubs/event-hubs-python-get-started-send#create-a-python-script-to-receive-events
The Dockerfile:
FROM python:3.8-slim-buster
COPY ./Scripts .
CMD [ "python3","-u","./calculate.py"]
The fix for that is to either host the script in a server e.g. nodejs or equivalent for given language, or create separate process that will return something for the 80 port.
There also might be a problem with the port configured on default, here is an answer how to fix that case:
https://serverfault.com/questions/1003418/azure-docker-app-error-site-did-not-start-within-expected-time-limit-and-co
step-1
add EXPOSE 8080 inside Dockerfile
step-2
build image from Dockerfile:
docker build . -t python-calculator
step-3
docker images #search the image with the tag you mentioned earlier i.e python-calculator
step-4
docker run -p 8080:8080 -d
step-5
localhost:8080

get a python docker container to interact with a redis docker container

I'm new to docker, redis and any kind of networking, (I know python at least!). Firstly I have figured out how to get a redis docker image and run it in a docker container:
docker run --name some-redis -d redis
As I understand this redis instance has port 6379 available to connect to other containers.
docker network inspect bridge
"Containers": {
"2ecceba2756abf20d5396078fd9b2ecf0d60ab04ca6b8df5e1b631b6fb5e9a85": {
"Name": "some-redis",
"EndpointID": "09f0069dae3632a2456cb4d82ad5e7c9782a2b58cb7a4ee655f57b5c410c3e87",
"MacAddress": "02:42:ac:11:00:02",
"IPv4Address": "172.17.0.2/16",
"IPv6Address": ""
}
If I run the following command I can interact with the redis instance and generate key:value pairs:
docker run -it --link some-redis:redis --rm redis redis-cli -h redis -p 6379
set 'a' 'abc'
>OK
get 'a'
>"abc"
quit
I have figured out how to make and run a docker container with the redis library installed that will run a python script as follows:
Here is my Dockerfile:
FROM python:3
ADD redis_test_script.py /
RUN pip install redis
CMD [ "python", "./redis_test_script.py" ]
Here is redis_test_script.py:
import redis
print("hello redis-py")
Build the docker image:
docker build -t python-redis-py .
If I run the following command the script runs in its container:
docker run -it --rm --name pyRed python-redis-py
and returns the expected:
>hello redis-py
It seems like both containers are working ok, the problem is connecting them both together, I would like to ultimately use python to perform operation on the redis container. If I modify the script as follows and rebuild the image for the python container it fails:
import redis
print("hello redis-py")
r = redis.Redis(host="localhost", port=6379, db=0)
r.set('z', 'xyz')
r.get('z')
I get several errors:
...
OSError: [Errno 99] Cannot assign requested address
...
redis.exceptions.ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address.
.....
It looks like they're not connecting, I tried again using the bridge IP in the python script:
r = redis.Redis(host="172.17.0.0/16", port=6379, db=0)
and get this error:
redis.exceptions.ConnectionError: Error -2 connecting to 172.17.0.0/16:6379. Name or service not known.
and I tried the redis sub IP:
r = redis.Redis(host="172.17.0.2/16", port=6379, db=0)
and I get this error:
redis.exceptions.ConnectionError: Error -2 connecting to 172.17.0.2/16:6379. Name or service not known.
It feels like I'm fundamentally misunderstanding something about how to get the containers to talk to each other. I've read quite a lot of documentation and tutorials but as I say have no networking experience and have not previously used docker so any helpful explanations and/or solutions would be really great.
Many thanks
That's all about Docker networking. Fast solution - use host network mode for both containers. Drawback is low isolation, but you will get it working fast:
docker run -d --network=host redis ...
docker run --network=host python-redis-py ...
Then to connect from python to redis just use localhost as a hostname.
Better solution is to use docker user-defined bridge network
# create network
docker network create foo
docker run -d --network=foo --name my-db redis ...
docker run --network=foo python-redis-py ...
Note that in this case you cannot use localhost but instead use my-db as a hostname. That's why I've used --name my-db parameter when starting first container. In user-defined bridge networks containers reach each other by theirs names.
Do:
Explicitly create a Docker network for your application, and run your containers connected to that network. (If you use Docker Compose, this happens for you automatically and you don’t need to do anything.)
docker network create foo
docker run -d --net foo --name some-redis redis
docker run -it --rm --net foo --name pyRed python-redis-py
Use containers’ --name as DNS hostnames: you connect to some-redis:6379 to reach the container. (In Docker Compose the name of the service block works too.)
Make the locations of external services configurable, most likely using an environment variable. In your Python code you can connect to
redis.Redis(host=os.environ.get("REDIS_HOST", "localhost"),
port=int(os.environ.get("REDIS_PORT", "6379"))
docker run --rm -it \
--name py-red \
--net foo \
-e REDIS_HOST=some-redis \
python-redis-py
Don’t:
docker inspect anything to find the container-private IP addresses. Between containers you can always use hostnames as described above. The container-private IP addresses are unreachable from other hosts, and may even be unreachable from the same hosts on some platforms.
Use localhost in Docker for anything, expect the specific case of connecting from a browser or other process running directly on the host (not in a container) to a port you’ve published with docker run -p on the same host. (It generally means “this container”.)
Hard-code host names in your code like this; it makes it hard to run the service in a different environment. (For databases in particular it’s not uncommon to run them outside of Docker or even in a hosted cloud service.)
Use --link, it’s outdated and unnecessary.

I can't access docker from outside the container [duplicate]

This question already has answers here:
Configure Flask dev server to be visible across the network
(17 answers)
Are a WSGI server and HTTP server required to serve a Flask app?
(3 answers)
Closed 5 years ago.
I have to "dockerize" a service I'm writing (a small Flask app). This is what I wrote:
FROM python:3
RUN apt-get update && apt-get install -y build-essential
WORKDIR /app
COPY requirements.txt /app
RUN pip install --no-cache-dir -r requirements.txt
COPY . /app
EXPOSE 5000
ENV FLASK_APP=app.py
ENV FLASK_DEBUG=1
CMD flask run
I can build the image and run the container with
docker run -p 5000:5000 <container-name>
and flask tells me it's listening on 127.0.0.1:5000. But I can't access the application from my host machine. I've also tried using the localhost and 0.0.0.0 as the address. However, if I exec into the container I can curl the address to receive the response I expect.
Does anyone know what's going on here?
The EXPOSE instruction does not actually publish the port. You also have to also tell docker to forward connections to that port. When running you would do docker run -p 80:5000 my_docker_image and then traffic inbound on port 80 will be forwarded to port 5000 within the docker container. Alternatively you could forward port 5000 to 5000 in the docker image if you don't want to listen on port 80.

Docker connection refused on all ports except 5000

I have just started with Docker. I have installed Docker Toolbox for Windows. I was trying out a sample Flask app to understand how things work. But I am stuck!. I am trying to access my app like this http://docker-machine-ip : port number but every time I do, I get '{docker-machine ip} refused to connect.'
I get no exceptions during the building and deploying stages. I also did docker ps to see that container is running. I even tried to access it via Kitematic but still no luck. Below are details related to the app
app.py
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Flask inside Docker shakel!!"
if __name__ == "__main__":
app.run(debug=True,host='0.0.0.0')
requirements.txt
flask
Dockerfile
FROM python:2.7
MAINTAINER Shekhar Gulati "shekhargulati84#gmail.com"
COPY . /app
WORKDIR /app
RUN pip install -r requirements.txt
ENTRYPOINT ["python"]
CMD ["app.py"]
The docker commands I used for building and running are:
docker-machine ip default //To get docker machine ip
docker build -t flask-app .
docker run -d -p 6000:6000 flask-app
I have Windows 7 64 bit. Please let me know if any more info is required.
P.S. However I noticed that if I map my container to 5000 port it will run fine but on any other port I get connection refused
I don't know what am I missing here. This is my first attempt at Docker and I have googled for 4 hrs to find a solution but nothing is working. So it might be a very dumb mistake I am doing somewhere :).anyhow any help is very much appreciated. Thanks in advance.
That's because you didn't set the port for your flask application, which is port 5000 by default.
From flask documentation:
port – the port of the webserver. Defaults to 5000 or the port defined in the SERVER_NAME config variable if present.

Categories