Python Cassandra driver: connect to Docker container on server - cassandra.UnresolvableContactPoints: {} - python

I am runnning cassandra in a docker container on a custom server.
I start cassandra docker like this:
docker run --name cassandra -p 9042:9042 -d cassandra:latest
When i want to conect to the server via the python cassandra driver from datastax like this:
from cassandra.cqlengine import connection
connection.setup(["http://myserver.myname.com"], "cqlengine", protocol_version=3)
The exception is thrown:
File "C:\LONG\PATH\TO\venv\lib\site-packages\cassandra\cqlengine\connection.py", line 106, in setup
self.cluster = Cluster(self.hosts, **self.cluster_options)
File "cassandra\cluster.py", line 1181, in cassandra.cluster.Cluster.__init__
cassandra.UnresolvableContactPoints: {}
python-BaseException
After hours of searching through docker network permissions I found the simple solution, so maybe this will help you too.

The simple solution is removing "http://" from the server url and changing my code from
connection.setup(["http://myserver.myname.com"], "cqlengine", protocol_version=3)
To
connection.setup(["myserver.myname.com"], "cqlengine", protocol_version=3)
I thought its a docker networking issue and it took me many hours to pin it down to this simple mistake

Related

docker.errors.DockerException: Error while fetching server API version

I want to install this module but there is something wrong when I try the step docker-compose build ...
I tried to update the Docker version and restart Docker many times. But it didn't work.
git clone https://github.com/uhh-lt/158.git
cd 158
docker-compose build
File "/home/ming/.local/bin/docker-compose", line 8, in <module>
sys.exit(main())
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/main.py", line 67, in main
command()
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/main.py", line 123, in perform_command
project = project_from_options('.', options)
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/command.py", line 60, in project_from_options
return get_project(
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/command.py", line 131, in get_project
client = get_client(
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/docker_client.py", line 41, in get_client
client = docker_client(
File "/home/ming/.local/lib/python3.8/site-packages/compose/cli/docker_client.py", line 170, in docker_client
client = APIClient(**kwargs)
File "/home/ming/.local/lib/python3.8/site-packages/docker/api/client.py", line 188, in __init__
self._version = self._retrieve_server_version()
File "/home/ming/.local/lib/python3.8/site-packages/docker/api/client.py", line 212, in _retrieve_server_version
raise DockerException(
docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))
Update 2020-11-23
Thanks for two of you helping me with the error! I tried the commend but can't connect to my Docker:
ming#KITM-7664:~$ sudo /etc/init.d/docker start
[sudo] password for ming:
* Starting Docker: docker [ OK ]
ming#KITM-7664:~$ which docker
/usr/bin/docker
ming#KITM-7664:~$ docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:52 2020
OS/Arch: linux/amd64
Experimental: false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
ming#KITM-7664:~$ systemctl status docker
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
ming#KITM-7664:~$ systemctl start docker
System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
ming#KITM-7664:~$ sudo /etc/init.d/docker start
* Starting Docker: docker [ OK ]
ming#KITM-7664:~$ docker version
Client: Docker Engine - Community
Version: 19.03.13
API version: 1.40
Go version: go1.13.15
Git commit: 4484c46d9d
Built: Wed Sep 16 17:02:52 2020
OS/Arch: linux/amd64
Experimental: false
Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?
Are you sure docker is running on your system? You can get that error when compose is not able to connect to docker via docker socket (if any other way for connection is not defined).
If you are running on linux, usually you can run systemctl status docker to check if docker daemon is running and systemctl start docker to start it.
It would help to tell what OS and docker version are you using.
set the permission like this,
sudo chmod 666 /var/run/docker.sock
When used WSL (Windows Subsystem for Linux) need to enable 'WSL Integration' for required distro in Windows Docker Desktop (Settings -> Resources-> WSL Integration -> Enable integration with required distros).
By default, the docker command can only be run the root user or by a user in the docker group, which is automatically created during Docker’s installation process. If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:
sudo usermod -aG docker ${USER}
To apply the new group membership, log out of the server and back in, or type the following:
su - ${USER}
You will be prompted to enter your user’s password to continue.
sudo service docker start
or
sudo service docker restart
Just had the same issue after updating Windows Docker desktop to it's latest version (20.10.2, build 2291f61). It happened that this update disabled the WSL2 integration with my virtual Ubuntu 18.04, which I use to run most projects.
I solved it this way:
Open Docker Desktop
Go to Settings > Resources > WSL Integration
Make sure that your distribution is enabled
Restart Docker
Without the need of restarting WSL2, docker should work again
I had a similar issue and it turned out to be due to Docker server not running. I started the app and then ran docker-compose up and it started working fine. Hope it helps anyone who's caught in a similar situation. :-)
You don't have permission to use the docker socket, by default only the docker group can access it. You can verify this with ls -l /var/run/docker.sock, which will print something like:
srw-rw----. 1 root docker 0 Oct 4 18:04 /var/run/docker.sock
To be able to access the socket and use Docker, add yourself to the Docker group with the following command:
sudo usermod -a -G docker $(whoami)
Then logout and back in. Docker will now work.
Make sure you check the box for 'use Docker Compose V2'. It solved for me.
docker desktop
If you're using docker-compose inside podman try the below command to resolve this issue "docker.errors.DockerException: Error while fetching server API version: ('Connection aborted.', FileNotFoundError(2, 'No such file or directory'))"
systemctl start podman.socket
I had the same problem but I managed to solve it when I ran docker server.
I'm also get this error when i try to run the docker-compose and docker desktop is turn off, i think that is bug in docker-compose that not notify the user it that the problem is becuse the docker service is down, same as the docker cli that throw correct error:
C:\Users\x\IdeaProjects\mongo-exmple>docker pull node
Using default tag: latest
error during connect: This error may indicate that the docker daemon is not running.: Post "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/images/create?fromImage=node&tag=latest": open //./pipe/docker_engine: The system cannot find
the file specified.
In Mac OS and Windows sometimes that's enough to start up docker application and this error is because of loss of docker daemon in system.
My issue was I was not signed into docker desktop.
At Linux (at least Ubuntu 18.04.x), you sometimes need to log out and re-login your X user session. Logging out of bash or out of the terminal window and re-logging in is not enough, see for example https://askubuntu.com/questions/1161020/groups-and-groups-user-show-different-groups-dialout-is-missing
I had this happen on a Mac after doing an OS update.
I was able to run docker on the command line but Docker Desktop wasn't running - starting Docker Desktop up fixed the issue.
In Windows exiting from Docker and running it again sometimes helps. Apparently not always docker starts up properly.
I had this problem while using ubuntu 22.04 wsl on windows and then I solve it like this
sudo update-alternatives --config iptables
it will show something like this
Selection Path Priority Status
------------------------------------------------------------
0 /usr/sbin/iptables-nft 20 auto mode
* 1 /usr/sbin/iptables-legacy 10 manual mode
2 /usr/sbin/iptables-nft 20 manual mode
then press 1
Press <enter> to keep the current choice[*], or type selection number: 1
Start docker using services
sudo service docker start
I had this error running docker compose in Linux Subsystem on Win10, the problem was that I have forgotten to start Docker Desktop application

All nameservers failed to answer UDP port 53 Google cloud functions python 3.7 atlas mongodb

i can connect locally to my mongodb server with the address 0.0.0.0/0. However, when I deploy my code to the cloud I get the error deploy to google cloud function.
google cloud function with python 3.7 (beta)
atlas mongo db
python lib:
-pymongo
-dnspython
Error: function crashed. Details:
All nameservers failed to answer the query _mongodb._tcp.**-***.gcp.mongodb.net. IN SRV: Server ***.***.***.*** UDP port 53 answered SERVFAIL
Traceback (most recent call last): File "/env/local/lib/python3.7/site-packages/pymongo/uri_parser.py", line 287, in _get_dns_srv_hosts results = resolver.query('_mongodb._tcp.' + hostname, 'SRV') File "/env/local/lib/python3.7/site-packages/dns/resolver.py", line 1132, in query raise_on_no_answer, source_port) File "/env/local/lib/python3.7/site-packages/dns/resolver.py", line 947, in query raise NoNameservers(request=request, errors=errors) dns.resolver.NoNameservers: All nameservers failed to answer the query _mongodb._tcp.**mymongodb**-r091o.gcp.mongodb.net. IN SRV: Server ***.***.***.*** UDP port 53
finally after stuck 2 day, goblok banget semaleman
just change connection
from
SRV connection string (3.6+ driver)
to
Standard connection string (3.4+ driver)
mongodb://<USERNAME>:<PASSWORD>#<DATABASE>-shard-00-00-r091o.gcp.mongodb.net:27017,<COLLECTION>-shard-00-01-r091o.gcp.mongodb.net:27017,<COLLECTION>-shard-00-02-r091o.gcp.mongodb.net:27017/test?ssl=true&replicaSet=<COLLECTION>-shard-0&authSource=admin&retryWrites=true
or you can see your connection string in atlas mongodb.
idk why can`t connect with srv connection string in google cloud functions, maybe not suppot now, or just misconfiguration.
This might help somone, I've struggled with this error on Windows when I tried to connect to MongoDB with the mongodb+srv:// syntax, it worked fine if I try it through WSL or on a Linux machine.
From https://forum.omz-software.com/topic/6751/pymongo-errors-configurationerror-resolver-configuration-could-not-be-read-or-specified-no-nameservers/4
import dns.resolver
dns.resolver.default_resolver=dns.resolver.Resolver(configure=False)
dns.resolver.default_resolver.nameservers=['8.8.8.8'] # this is a google public dns server, use whatever dns server you like here
# as a test, dns.resolver.query('www.google.com') should return an answer, not an exception
Most of the time the error occurs when your network isn't configured with 8.8.8.8 DNS Adress.
Type the following command on command prompt to verify
ipconfig /all
check your IP configuration & in the config you should see the following
DNS Servers . . . . . . . . . . . : 8.8.8.8
8.8.4.4
if not you should configure your network using 8.8.8.8 as primary DNS and 8.8.4.4 as secondary.
OR
May be you are using a VPN or private DNS.In that case contact you service provider for the assistance.

Can't connect to redis container with custom config file via redis-py

I'm able to run a normal redis docker image with:
docker run --name redis -p 6379:6379 -v /home/maryam/redis:/data -d redis redis-server
and then I can access the database via redis-py as I supposed to.
but I want to use a custom config file and In order to do so, I specify the path to my redis.conf and create the container like this:
docker run -v /home/maryam/Desktop/redis.conf:/usr/local/etc/redis/redis.conf -v /home/maryam/redis:/data -p 6379:6379 --name redis -d redis redis-server /usr/local/etc/redis/redis.conf
The container is built without any errors and I can see the redis.conf file inside it. Problem is when I want to do anything with the database via redis-py, I get the following error:
redis.exceptions.ConnectionError: Error while reading from socket: ('Connection closed by server.',)
I think source of the problem should be redis.conf file which I have edited and added these lines to it:
maxmemory 4gb (I also tried 4200000000 bytes)
maxmemory-policy allkeys-lru
maxmemory-samples 5
Without it, it works perfectly!
So any idea how can I overcome this?

connect to mysql server in docker

If I run mysql server in first docker container:
docker run --name mysqlserver -e MYSQL_ROOT_PASSWORD=pass -d mysql:5.5
and link second docker container with first container:
docker run --name myapp --link mysqlserver:mysql -d vladimir/app
and if in second docker container I have python app, where I just want to connect to the database in first container, how to determine what host to use?
I am not too familiar with mysql. Looking at the mysql documentation it seems like this might be the sort of command you want:
import _mysql
db=_mysql.connect(host="mysql",port=3306,passwd="...",db="...")
when you did the --link in your apps run, you declared that you are linking the EXPOSEd ports for the container named mysqlserver to the container to be named myapp. Docker updates the /etc/hosts file. So, if you cat the /etc/hosts file you should see a line that looks like:
172.17.0.26 mysql 5c960433e26a mysqlserver
so this is why the reference in your python to host 'mysql' works. The default port is 3306. So, that should connect you. Use this host and port for whatever mysql connection package you are using from python.
This is how I connect with other objects, on their well known ports. You can also use environment variables, but, I think this might be being phased out...dunno...
root#d23d8be2a6fa:/# env | grep MYSQL
MYSQL_ENV_MYSQL_ROOT_PASSWORD=pass
MYSQL_PORT_3306_TCP_PORT=3306
MYSQL_PORT_3306_TCP=tcp://172.17.0.32:3306
MYSQL_ENV_MYSQL_VERSION=5.5.42
MYSQL_NAME=/myapp/mysql
MYSQL_PORT_3306_TCP_PROTO=tcp
MYSQL_PORT_3306_TCP_ADDR=172.17.0.32
MYSQL_ENV_MYSQL_MAJOR=5.5
MYSQL_PORT=tcp://172.17.0.32:3306
so you could modify your connect line to use the environment:
import os
myhost = os.environ.get('MYSQL_PORT_3306_TCP_ADDR')
myport = int(os.environ.get('MYSQL_PORT_3306_TCP_PORT'))
import _mysql
db=_mysql.connect(host=myhost,port=myport,passwd="...",db="...")
Either way. This is discovery. There are more complicated methods as well, using consul, etcd, skydns, registrator. I like the skydns/registrator pairing, registrator watches your docker daemon, when new containers are created it inserts dns records into etcd, which skydns can use to mimic a dns server. Using this technique you get ip and port as well.

Unable to connect to mongodb running on a remote machine

I have mongodb running on a remote server. I can ssh to the remote server and connect to mongodb from the shell on the remote machine. However i have to connect to that mongodb instance from my python script.
However, im unable to connect to mongodb directly from the shell on my local machine running linux using the command:
mongo <remote_ip>:27017
or through pymongo using
connection = pymongo.Connection("<remote_ip>", 27017)
I get the below error when using pymongo:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-packages/pymongo-1.11-py2.6-linux-i686.egg/pymongo/connection.py", line 370, in __init__
self.__find_master()
File "/usr/local/lib/python2.6/dist-packages/pymongo-1.11-py2.6-linux-i686.egg/pymongo/connection.py", line 605, in __find_master
raise AutoReconnect("could not find master/primary")
AutoReconnect: could not find master/primary
What could be causing this problem ?. Does it mean mongo is running on a port other than 27017 and if so how can i find out which port it is running on ?
Please Help
Thank You
You can use netstat -a -p on the machine running mongodb to see what port it's attached to. (netstat -a lists all connections and -p provides the name of the program owning the connection.) Also make sure the remote computer is allowing external connections on that port (the connections aren't being blocked by a firewall) and that mongodb is accepting remote connections.

Categories

Resources