I am attempting to create a docker image via docker-py and I am using the following code:
import docker
import os
docker_client = docker.from_env()
path = os.path.dirname(os.path.abspath(__file__)) + "/container"
docker_client.images.build(path=path, tag='container-tag')
The last line throws this error:
requests.exceptions.ConnectionError: ('Connection aborted.', error(104, 'Connection reset by peer'))
The user I'm using is a member of the docker group and I have checked the permissions of the socket file.
Since docker_client.containers.list() works for you, your issue is not with docker connection. It is that the build is failing for some reason.
Run journalctl -f -n10 in another terminal and then run your code and see if you can find what is going wrong. That will give your clear exception happening when you call
docker_client.images.build(path=path, tag='container-tag')
Related
I am running an Apache Phoenix container along with Hbase using the following command,
docker run -it --name phoenix -p 8765:8765 avapno/apache-phoenix
This is done as explained here,
https://hub.docker.com/r/avapno/apache-phoenix
The containers run without a problem.
Now, I want to connect to Phoenix using Python. This is the code I have used to do so,
import phoenixdb
database_url = 'http://localhost:8765/'
conn = phoenixdb.connect(database_url, autocommit=True)
This does not, however, seem to work. I keep getting the following error,
ConnectionError: HTTPConnectionPool(host='localhost', port=8765): Max retries exceeded with url: / (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7ffb03782080>: Failed to establish a new connection: [Errno 111] Connection refused'))
How exactly do I get this done correctly?
Also, is there a better way to get Hbase + Phoenix running?
The Problem
I'm getting started with MongoDB on Python, I have a Ubuntu machine in my local network and MongoDB is installed there. When I try to connect with database using Python from Mac it throughs me an error. I searched about it and found out there is a .service called mongod.service that needs to be started along with mongodb.service. But when I try to start the mongod.service the it says the .service doesn't even exist. I tried both with IP and mongodb url, nothing works.
Ubuntu Terminal
$ sudo service mongod start
$ Failed to start mongod.service: Unit mongod.service not found.
$ sudo systemctl start mongod
$ Failed to start mongod.service: Unit mongod.service not found.
DataBase Link (a)
mongodb://user:password#192.168.0.106/database
Python Script (a)
#!/usr/bin/env python3
from pymongo import MongoClient
client = MongoClient('mongodb://user:password#192.168.0.106/database')
db = client['database']
collection = db['collection']
json = dict(message='hello world', token=0)
collection.insert_one(json)
macOS Terminal (a)
pymongo.errors.ServerSelectionTimeoutError: 192.168.0.106:27017: [Errno 61] Connection refused, Timeout: 30s, Topology Description: <TopologyDescription id: 60e140982a43032aef0dd634, topology_type: Single, servers: [<ServerDescription ('192.168.0.106', 27017) server_type: Unknown, rtt: None, error=AutoReconnect('192.168.0.106:27017: [Errno 61] Connection refused')>]>
DataBase Link (b)
mongodb+srv://user:password#cluster0.h9fmz.mongodb.net/database?retryWrites=true&w=majority
Python Script (b)
#!/usr/bin/env python3
from pymongo import MongoClient
client = MongoClient('mongodb+srv://user:password#cluster0.h9fmz.mongodb.net/database?retryWrites=true&w=majority')
db = client['database']
collection = db['collection']
json = dict(message='hello world', token=0)
collection.insert_one(json)
macOS Terminal (b)
Traceback (most recent call last):
File "/usr/local/lib/python3.9/site-packages/pymongo/pool.py", line 1278, in _get_socket
sock_info = self.sockets.popleft()
IndexError: pop from an empty deque
During handling of the above exception, another exception occurred:
.....
.....
.....
pymongo.errors.OperationFailure: bad auth : Authentication failed., full error: {'ok': 0, 'errmsg': 'bad auth : Authentication failed.', 'code': 8000, 'codeName': 'AtlasError'}
Note That
I'm providing the correct username and password for the database.
I'm using a machine on my local network, which is not a live server.
I've also tried the following commands but they did not solve anything.
Ubuntu Terminal
$ mongod --auth --port 27017
$ mongod --port 27017
$ sudo rm /var/lib/mongodb/mongod.lock
$ sudo mongod --repair
For accessing mongodb from another machine in local network. You will need to check the following:
There is no firewall restriction in the server machine or client machine. In case there is a firewall, you will need to add rule exceptions to allow this port to be accessible. Both incoming and outgoing. (Ubuntu firewall)
You will have to add bindIp config to the mongodb config in server machine. Refer to docs here. You will need to add something like this:
net:
bindIp: 0.0.0.0
port: 27017
Make sure you are able to connect using this ip: 192.168.0.106(server in local network) from the server machine itself. This will make sure the server is listening in this ip.
$ Failed to start mongod.service: Unit mongod.service not found.
The solution for this error could be found here
The mongo atlas error might be due to the following reasons:
You will have to create an database user in order to connect to mongodb.
you can find it under the left panel -> Database access -> Add user
This will be because of a mismatch with username and password. In case you have any special characters in your password you will have to url encode them.
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 running two docker containers as follows:
one is for selenium/standalone-chrome
$ docker run -d -p 4444:4444 -v /dev/shm:/dev/shm selenium/standalone-chrome
and the main container uses host networking to connect to MySQL which is running on localhost
$ docker run --rm --network="host" $(IMAGE_REPO)
but when I tried to initiate the driver in the main container
from selenium import webdriver
driver = webdriver.Remote(command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=webdriver.DesiredCapabilities.CHROME)
I got error message which is either
urllib3.exceptions.ProtocolError: ('Connection aborted.', BadStatusLine("''",))
or
urllib3.exceptions.ProtocolError: ('Connection aborted.', error(104, 'Connection reset by peer'))
did I forget anything / any parameters when trying to establish the containers? or the value passing to command_executor should not be 'http://127.0.0.1:4444/wd/hub'?
I also tried to use docker bridge networking, but then I have no idea how to connect to MySQL server on localhost
SQLALCHEMY_URI = '{driver}://{user}:{pwd}#{host}/{db}?charset=utf8' \
.format(
driver='mysql+pymysql',
host='127.0.0.1:3306',
user='root',
pwd='0000',
db='default'
)
Update the following libraries in your Dockerfile:
Set Selenium library version to 3.3.1 (selenium==3.3.1).
i.e: Make use of a Selenium image, whose version is 3.3.1.
Use selenium/node-chrome:3.3.1 instead of selenium/node-chrome:3.141.59-gold
In order to communicate with MongoDB from Flask, I use the following code to define the connection :
CLIENT = pymongo.MongoClient('mongodb://localhost:27017/')
Everything works fine but once I'm trying to deploy my application on Docker I get the following error:
pymongo.errors.ServerSelectionTimeoutError: localhost:27017: [Errno 111] Connection refused
To solve this problem, I can replace the previous code by:
import os
client = MongoClient(os.environ['DB_PORT_27017_TCP_ADDR'],27017)
But then it only works on Docker and not on my local machine.
How can I define the connection in order to make it work on both Docker and my local machine ?
You can specify that if DB_PORT_27017_TCP_ADDR environment variable is present, use that; else use localhost using something like this:
import os
client = MongoClient(os.environ.get('DB_PORT_27017_TCP_ADDR') or 'localhost', 27017)