Connect to redis from another container in docker - python

I have app, that used Tornado and tornado-redis. [image "app" in docker images]
I start redis:
docker run --name some-redis -d redis
Then I want to link my app with redis:
docker run --name some-app --link some-redis:redis app
And I have error:
Traceback (most recent call last):
File "./app.py", line 41, in <module>
c.connect()
File "/usr/local/lib/python3.4/site-packages/tornadoredis/client.py", line 333
, in connect
self.connection.connect()
File "/usr/local/lib/python3.4/site-packages/tornadoredis/connection.py", line
79, in connect
raise ConnectionError(str(e))
tornadoredis.exceptions.ConnectionError: [Errno 111] Connection refused
I have tested my code with local tornado and redis, and it works. The problem in
c = tornadoredis.Client()
c.connect()
Why my app cant connet to redis-container? How to fix that? I use standart port 6379.
Thanks!

tornadoredis attempts to use redis on localhost. (See source here)
So you need to inform tornadoredis where redis is running (since to the docker image it is not running on localhost).
For example:
c = tornadoredis.Client(host="<hostname>")
c.connect()
In your specific case, substitute "redis" for "<hostname>".

Related

During docker build "socket.gaierror: [Errno -2] Name or service not known" - RabbitMQ Connection

Good morning,
I am facing a weird issue during the composing of my RabbitMQ container.
When I build the container without my python script, which creates the test structure of my RabbitMQ, it works fine. I access the container, run the script manually, and everything gets created perfectly, with no errors.
If I run the script with the same command ("python3 manager.py"), but in a RUN entry at the Dockerfile, it's like it is suddenly unable to find the hostname or something like that during the RabbitMQ connector creation. So, it aborts the creation of the container.
I have tried executing it as a background Linux process, and the container is created, but the RabbitMQ structure creation keeps failing.
Docker-compose
version: "3.8"
services:
rabbitmq:
container_name: rabbitmq
image: rabbitmq
build: src/server/
env_file:
- src/server/server.env
ports:
- "15672:15672"
- "5672:5672"
hostname: rabbitmq
networks:
- rabbitmqnet
networks:
rabbitmqnet:
name: rabbitmqnet
driver: bridge
Dockerfile
FROM rabbitmq:3-management
WORKDIR /app
EXPOSE 15672
COPY . /app
RUN apt-get update -y
RUN apt-get install -y python python3-pip
RUN pip install -r requirements.txt
RUN python3 manager.py
manager.py
import pika
import config
connection = pika.BlockingConnection(pika.ConnectionParameters(config.server, config.port, '/', pika.PlainCredentials(config.user, config.password)))
channel = connection.channel()
def main():
createQueue("test-queue")
createExchange("test-exchange")
createBinding("test-exchange", "test-queue", "test")
# This method creates a queue.
def createQueue(qName):
channel.queue_declare(queue=qName)
# This method creates an exchange.
def createExchange(eName):
channel.exchange_declare(
exchange=eName,
exchange_type='direct'
)
# This method creates a binding routing key between an exchange and a queue. This allows the publisher to send messages to the queue through the exchange.
def createBinding(eName, qName, routingKey):
channel.queue_bind(
exchange=eName,
queue=qName,
routing_key=routingKey
)
if __name__ == "__main__":
main()
config.py
server= 'rabbitmq'
port= 5672
user= 'user'
password= 'password'
Error
[7/7] RUN python3 manager.py:
#0 0.276 Traceback (most recent call last):
#0 0.276 File "manager.py", line 4, in <module>
#0 0.276 connection = pika.BlockingConnection(pika.ConnectionParameters(config.server, config.port, '/', pika.PlainCredentials(config.user, config.password)))
#0 0.276 File "/usr/local/lib/python3.8/dist-packages/pika/adapters/blocking_connection.py", line 360, in init
#0 0.276 self._impl = self._create_connection(parameters, _impl_class)
#0 0.276 File "/usr/local/lib/python3.8/dist-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
#0 0.276 raise self._reap_last_connection_workflow_error(error)
#0 0.276 File "/usr/local/lib/python3.8/dist-packages/pika/adapters/utils/selector_ioloop_adapter.py", line 565, in _resolve
#0 0.276 result = socket.getaddrinfo(self._host, self._port, self._family,
#0 0.276 File "/usr/lib/python3.8/socket.py", line 918, in getaddrinfo
#0 0.276 for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
#0 0.276 socket.gaierror: [Errno -2] Name or service not known
failed to solve: executor failed running [/bin/sh -c python3 manager.py]: exit code: 1
I think you need to use CMD or ENTRYPOINT instead of RUN in your dockerfile.
Please read more about the topic: Difference between RUN and CMD in a Dockerfile
RUN is an image build step, the state of the container after a RUN
command will be committed to the container image. A Dockerfile can
have many RUN steps that layer on top of one another to build the
image.
CMD is the command the container executes by default when you launch
the built image. A Dockerfile will only use the final CMD defined. The
CMD can be overridden when starting a container with docker run $image
$other_command.
ENTRYPOINT is also closely related to CMD and can modify the way a
container starts an image.
Solved! I am so newbie with docker. the issue is that the image by default ends starting the RabbitMQ.
So, if I executed with RUN the script, the container has not started nor the server, then for sure it cannot connect.
Instead, when I used CMD, I was overriding the default initialization, so also the RabbitMQ did not ever start.
Thank you!

Using rabbitmq fails from docker container pika.exceptions.AMQPConnectionError

I am trying to learn how to use docker and Rabbitmq at the same time now.
#Specifying the base image
FROM python:3.10
ADD ./task.py ./home/
#Here we added the python file that we want to run in docker and define its location.
RUN pip install requests celery pika
#Here we installed the dependencies, we are using the pygame library in our main.py file so we
have to use the pip command for installing the library
CMD [ "python3" ,"/home/task.py" ]
#lastly we specified the entry command this line is simply running python
This is how my Dockerfile looks like and then I setup another container with rabbitmq through the command :
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.9-management
This is what task.py looks like :
from celery import Celery
import pika
from time import sleep
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
connection.close()
app = Celery('task', broker="localhost")
#app.task()
def reverse(text):
sleep(5)
return text[::-1 ]
And i run the docker run command , but I keep getting this error.
PS C:\Users\xyz\PycharmProjects\Sellerie> docker run sellerie
Traceback (most recent call last):
File "/home/task.py", line 5, in <module>
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost', port=5672))
File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line
360, in __init__
self._impl = self._create_connection(parameters, _impl_class)
File "/usr/local/lib/python3.10/site-packages/pika/adapters/blocking_connection.py", line 451, in _create_connection
raise self._reap_last_connection_workflow_error(error)
pika.exceptions.AMQPConnectionError
PS >
Can anyone help me better understand where the problem is. Maybe how to connect rabbitmq with the other docker container where my python file is located??
Thank you so much in advance

I have Postgres server connection error when I use docker?

My application works fine on my system but when I build a docker image by docker composer and then run the application it shows the error below.
However, I resolved it by changing the ip address from 127.0.0.1 => 0.0.0.0 in the Flask setting as below and the docker image works fine on my system. But when I run it on another ubuntu it gives me the same error and changes the IP to 127.0.0.1.
if __name__ == "__main__":
app.run(host='**0.0.0.0**',debug=False)
ERROR:
Traceback (most recent call last):
File "./main.py", line 22, in <module>
ps.create_table() # create the table if is not existed
File "/prj/Modules/postgres.py", line 49, in create_table
connection = get_connection()
File "/prj/Modules/postgres.py", line 36, in get_connection
return psycopg2.connect(f"dbname={db_name} user={user} password={password} host={host}")
File "/usr/local/lib/python3.8/site-packages/psycopg2/__init__.py", line 126, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host **"127.0.0.1"** and accepting
TCP/IP connections on port 5432?
you can have access to my code and my docker hub:
docker hub: applia65/duplicatebugreportsearchengine
Github: https://github.com/ghasemieh/Duplicated-Bug-Report-Detection-System/blob/master/main.py
Basically I have 3 container my-app, Postgres, and MongoDB.
I appreciate it if you help me with this problem.
Thanks
From the trace, the PostgreSql isn't accessible. Can you try updating your docker compose and add expose statement like below:
expose:
- 5432
This will allow access only to the linked services unless "ports" is also specified.

connection refused error in paho-mqtt python package

I am new to paho-mqtt. I was trying to publish a topic using my localhost and I encountered the following error :
Traceback (most recent call last):
File "server.py", line 10, in <module>
client1.connect(host,port,keepalive)
File "/usr/local/lib/python2.7/dist-packages/paho_mqtt-1.3.1-py2.7.egg/paho/mqtt/client.py", line 768, in connect
return self.reconnect()
File "/usr/local/lib/python2.7/dist-packages/paho_mqtt-1.3.1-py2.7.egg/paho/mqtt/client.py", line 895, in reconnect
sock = socket.create_connection((self._host, self._port), source_address=(self._bind_address, 0))
File "/usr/lib/python2.7/socket.py", line 575, in create_connection
raise err
socket.error: [Errno 111] Connection refused
My python code is below :
import paho.mqtt.client as paho
port=1883
host = "localhost"
keepalive = 60
def on_publish(client,userdata,result):
print("data published \n")
pass
client1= paho.Client("control1")
client1.on_publish = on_publish
client1.connect(host,port,keepalive)
ret= client1.publish("Robot","Robot 1 move_left")
When I run the same code with iot.eclipse.org as host then it works fine. Any help would be highly appreciated.
I was facing the same issue.
The solution was to install a local MQTT broker.
http://www.steves-internet-guide.com/install-mosquitto-linux/
The exposed docker port for mqtt is usually different than 1883.
I use the official eclipse mosquitto docker and the run example on their page is something like:
sudo docker run -it -p 11883:1883 -p 9001:9001 eclipse-mosquitto
therefore the client should connect to port 11883
client.connect(broker_address, 11883)

Pika to RabbitMQ connection failure using Raspberry PI and Docker container

I'm working on a simple setup for a Raspberry PI to communicate with a server via RabbitMQ and I'm not making a connection. Here is the setup:
Client: Raspberry PI (Raspbien Debian 8.0) with Python 2.7 and Pika 0.10.0
RabbitMQ Server: MacMini running 10.11.6 - OS X El Capitan with Docker
Docker execution on Mac:
docker run -v /Users/tigelane/Documents/Development/brimstone_manager:/var/lib/rabbitmq -d --hostname my-rabbit --name some-rabbit rabbitmq:3
Python code to execute on client:
def rabbit_post():
entry = get_ipaddress()
post_time = datetime.datetime.now().strftime("%d %B %Y : %I:%M%p")
rabbit_server = 'machine.tigelane.com'
credentials = pika.PlainCredentials('machine', 'machine')
connectionParams = pika.ConnectionParameters(host=rabbit_server, credentials=credentials)
connection = pika.BlockingConnection(connectionParams)
channel = connection.channel()
channel.queue_declare(queue='hello')
try:
channel.basic_publish(exchange='', routing_key='hello', body='Hello World!')
print(" [x] Sent 'Hello World!'")
except:
print ("Unable to post to server: {}".format(rabbit_server))
connection.close()
I kept seeing this error:
Traceback (most recent call last): File "./brimstone_post.py", line
75, in
main() File "./brimstone_post.py", line 71, in main
rabbit_post() File "./brimstone_post.py", line 58, in rabbit_post
connection = pika.BlockingConnection(connectionParams) File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py",
line 339, in init
self._process_io_for_connection_setup() File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py",
line 374, in _process_io_for_connection_setup
self._open_error_result.is_ready) File "/usr/local/lib/python2.7/dist-packages/pika/adapters/blocking_connection.py",
line 395, in _flush_output
raise exceptions.ConnectionClosed() pika.exceptions.ConnectionClosed
I had read a few other posts and tried some things like the following to troubleshoot:
Viewing the rabbitmq logs: docker logs some-rabbit The log file
didn't show any connection.
Capturing traffic on the raspberry to
see if I'm even trying to send traffic to the server: sudo tcpdump
port 5672
Making sure the Firewall was turned off / ports open on the Mac.
I finally realized that on the Docker container there is not -p option to forward ports to the container. I changed the container to open ports 5672 on the command-line and and now it's working. Hope this can help someone else that might be using the documents from hub.docker.com on RabbitMQ.
This is the example they give for the docker container: $ docker run -d --hostname my-rabbit --name some-rabbit rabbitmq:3
This is the new command I'm using to start my RabbitMQ docker container and it's working well: docker run -v /Users/tigelane/Documents/Development/brimstone_manager:/var/lib/rabbitmq -d --hostname my-rabbit --name some-rabbit -p 5672:5672 rabbitmq:3
While I think I have solved my problem, I have this nagging feeling that I'm missing something (besides other ports that I might need to add) and that there may have been a reason that the port was omitted on the example, or maybe they just left it off thinking everyone would add the port naturally because that's how you use Docker containers. Either way, please feel free to correct my mistake.

Categories