Any ideas why I get a connection error when trying to run a redis command from one container to another?
I'm trying to run an old python 2.7 lambda locally in order to test before upgrading to 3.8.10
I'm running a script that builds and links redisdb container with my_app container:
docker build . -t my_app:latest
docker pull redis:3.2.4-alpine && \
docker run --name=redisdb -d redis:3.2.4-alpine redis-server
docker run \
--rm -t -i -d \
--link redisdb:redis \
-h redis -p 6379 \
-e CONFIG_FILE=local_config.yaml \
my_app
I am running a python script on my_app with the following commands:
r = redis.Redis()
r.mset({"Bahamas": "Nassau"})
print(r.get("Bahamas"))
I've also tried the top suggestion here: Error 99 connecting to localhost:6379. Cannot assign requested address and passed r = redis.Redis(host='localhost', port=6379, decode_responses=True) with the same results.
Every time I try to run that python script in the my_app container, I get:
redis.exceptions.ConnectionError: Error 99 connecting to localhost:6379. Cannot assign requested address.
When I run the containers and then check docker ps I get this:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
76aea9308821 mar-atlas-readings-processor "python2" About a minute ago Up About a minute 0.0.0.0:64098->6379/tcp mar-atlas-readings-processor
f313113341de redis:3.2.4-alpine "docker-entrypoint.s…" About a minute ago Up About a minute 6379/tcp redisdb
This shows the correct port connections.
Your python app is trying to talk to Redis on localhost but the Redis container is on a different localhost.
You should be able to use the link configured in the docker run ... command, i.e redisdb, for the host option.
I can successfully ping windows host using Ansible from my localhost terminal but when trying this via a docker container, it fails with the following message:
flask | fatal: [eudc2.dea.com]: UNREACHABLE! => {"changed": false, "msg": "basic: HTTPConnectionPool(host='eudc2.dea.com', port=5985): Max retries exceeded with url: /wsman (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7fbc16d50550>, 'Connection to eudc2.dea.com timed out. (connect timeout=30)'))", "unreachable": true}
However, when pinged via localhost:
~/Documents/Projects/user oam !5 ?6 ❯ ansible dc -m win_ping
eudc2.dea.com | SUCCESS => {
"changed": false,
"ping": "pong"
}
I would say this means the problem is in the docker container so that's the dockerfile:
FROM python
WORKDIR /app
ADD . /app
RUN pip3 install --upgrade pip
RUN pip3 install flask uwsgi requests ansible ansible_runner pywinrm
RUN apt-get update -y && \
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
sshpass
CMD ["uwsgi","app.ini"]
This is the hostvars:
[dc]
eudc2.dea.com
[dc:vars]
ansible_user='username'
ansible_password='pass'
ansible_connection=winrm
ansible_winrm_server_cert_validation=ignore
ansible_winrm_port=5985
ansible_winrm_transport=basic
Detailed output of win_ping from docker container:
~/Documents/Projects/user oam !5 ?7 ❯ docker exec flask ansible -i /app/ansible/inventory/hosts dc -m win_ping -vvvv ✘ INT
ansible 2.10.2
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.9/site-packages/ansible
executable location = /usr/local/bin/ansible
python version = 3.9.0 (default, Oct 13 2020, 20:14:06) [GCC 8.3.0]
No config file found; using defaults
setting up inventory plugins
host_list declined parsing /app/ansible/inventory/hosts as it did not pass its verify_file() method
script declined parsing /app/ansible/inventory/hosts as it did not pass its verify_file() method
auto declined parsing /app/ansible/inventory/hosts as it did not pass its verify_file() method
Set default localhost to localhost
Parsed /app/ansible/inventory/hosts inventory source with ini plugin
Loading callback plugin minimal of type stdout, v2.0 from /usr/local/lib/python3.9/site-packages/ansible/plugins/callback/minimal.py
META: ran handlers
redirecting (type: modules) ansible.builtin.win_ping to ansible.windows.win_ping
Loading collection ansible.windows from /usr/local/lib/python3.9/site-packages/ansible_collections/ansible/windows
Using module file /usr/local/lib/python3.9/site-packages/ansible_collections/ansible/windows/plugins/modules/win_ping.ps1
Pipelining is enabled.
<eudc2.dea.com> ESTABLISH WINRM CONNECTION FOR USER: <username_truncated> on PORT 5985 TO eudc2.dea.com
eudc2.ictv.com | UNREACHABLE! => {
"changed": false,
"msg": "basic: HTTPConnectionPool(host='eudc2.dea.com', port=5985): Max retries exceeded with url: /wsman (Caused by ConnectTimeoutError(<urllib3.connection.HTTPConnection object at 0x7f628ecd08e0>, 'Connection to eudc2.dea.com timed out. (connect timeout=30)'))",
"unreachable": true
}
~/Documents/Projects/user oam !5 ?7 ❯ docker exec flask curl eudc2.dea.com ✘ INT
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:16 --:--:-- 0
It's interesting that the curl run from within the container took 16 seconds to complete. That looks a long time.
Ansible's WinRM timeout is 20 seconds for operations:
https://docs.ansible.com/ansible/latest/user_guide/windows_winrm.html
Some suggestions:
Run a few curl request from within and outside the container (I'm considering the container is running on the same box as what you're calling localhost); compare the times to see they differ substantially
Run win_ping multiple times from both container and localhost; does container ever work or localhost ever fails?; time the runs (time ansible dc -m win_ping)
Change the configurations ansible_winrm_operation_timeout_sec and ansible_winrm_read_timeout_sec mentioned on the link above, and see if that helps
In Python3, I am using get.requests() in a parallel loop to query a server that I have running in a docker container (specifically an OSRM server).
I get this error after it successfully queries a few:
requests.exceptions.ConnectionError: ('Connection aborted.', ConnectionResetError(104, 'Connection reset by peer'))
I think the Docker container has a limit on the number of queries at a time or from a single IP, but I don't know how to fix it. I'm assuming it's in the Docker settings.
I initiate the docker container using: docker run --name osrm-nc -t -i -p 6002:5000 -v /homedirs/tml62/osm:/data osrm/osrm-backend osrm-routed --algorithm mld /data/north-carolina-latest.osrm, so noteably, I'm assigning the ports
I've set the retries so it can repeat and that sometimes fixes it, but it causes delays and sometimes will still break.
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)
I am trying to run a simple connection to pymongo but it keeps returning that the connection was refused
Here is what I tried:
>>>from pymongo import Connection
>>>connection = Connection('localhost',27017)
here is what I get
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/pymongo-2.0.1_-py2.7-linux i686.egg/pymongo/connection.py", line 348, in __init__
self.__find_node()
File "/usr/local/lib/python2.7/dist-packages/pymongo-2.0.1_-py2.7-linux- i686.egg/pymongo/connection.py", line 627, in __find_node
raise AutoReconnect(', '.join(errors))
pymongo.errors.AutoReconnect: could not connect to localhost:27017: [Errno 111] Connection refused
How do I fix this?
Removing mongod.lock inside /var/lib/mongodb
sudo rm /var/lib/mongodb/mongod.lock
And then restarting the service should do it. For example, in my Ubuntu installation, restarting the server is something like this:
sudo service mongodb start
If you found this page because you use Docker and you face the connection problem,
try to use in your client initialization the docker container name of the mongodb instead of the localhost:27017 or 0.0.0.0:27017
Steps to fix:
write docker ps in console
find the name of container (it's in the last column of the command output called NAMES
MongoClient('mongodb://CONTAINER_NAME')
PROFIT.
Just try following commands in given order :
sudo rm /var/lib/mongodb/mongod.lock
sudo mongod --repair
sudo service mongodb start
sudo service mongodb status
That's it now you could see following as output of last command:
mongodb start/running, process 2796
For anyone who's having this problem on a remote server rather than the localhost, try enabling external interfaces:
Go to the configuration file (ex. /etc/mongodb.conf)
Find bind_ip=127.0.0.1
Comment out that line with a # at the front
Restart mongod
It looks like you might not be running the MongoDB server. One thing that frequently trips me up is that if the server was shut down uncleanly, it will refuse to start up again until you remove the mongod.lock file from the data directory.
Try following commands :
sudo service mongod start
sudo service mongod status
db.py
import pymongo
from pymongo import MongoClient
#mongo client is connected
client = MongoClient()
db = client['db']
Rather than deleting mongod.lock, I'd recommend running 'mongod --repair'. (I figure it's better to go in through the front door whenever possible. And there may be other things that this catches as well, AFAIK.)
None of the above answers worked for me, as I am using docker-compose so this worked for me:
docker run --rm --volumes-from my-mongo-server mongo unlink "/data/db/mongod.lock"
docker run --rm --volumes-from my-mongo-server mongo --repair
Replace my-mongo-server with your container name/id.
If you're trying to connect from a server (other than your localhost), try checking if your mongo installation is complete :
you should have:
* /var/lib/mongodb file
* /var/log/mongodb file
* mongodb.service as service ( check by starting the service sudo service mongodb start )
If any of those fails, try reinstalling mongo ( Failed to start mongod.service: Unit mongod.service not found )
This solved my problem.
Cheers
For the newer versions(4.x) of MongoDb, you can try:
sudo rm /var/lib/mongodb/mongod.lock
sudo systemctl daemon-reload
sudo systemctl start mongod
First make sure you have installed mongodb using sudo apt install mongodb
Thanks, but for me i just had to stop mongod and then restart it and it worked fine without having to remove anything. PS : pymongo 2.7.2