Subscribe to Redis Instance in Azure using redis-cli in Docker - docker

I have a Redis instance running in Azure, and I have the connection string. I want to subscribe to it from the command line using redis-cli running in a Docker container. I can do this when I run redis locally, using the command
docker run -it --link redisDev:redis --rm redis redis-cli -h redis -p 6379:6739
However I can't figure out how to use the connection string for a remote instance.

Can you telnet that remote redis instance? is it exposed on the web? is the connection string an URI?
It should be as easy as providing the right host to the -h param.

Related

Forwarding the port for the redis dashboard in Docker

I'm working on a new scraping project. I already set up redis inside docker by running this
docker run -d --rm --name redis -p 6379:6379 redis:alpine
And then I installed redis client and I've successfully gotten redis to run.
In order to run scrapers locally I need to forward the port for the redis dashboard. I did run this command
redis-cli -h 127.0.0.1 -p 6379
but I'm not sure if that is correct.
Also When I type 127.0.0.1:6379 in browser,The page displaying this
I did search online and didn't get enough helpful resources for windows platform . Does anyone know how to fix this problem? A guidance would be much appreciated !
When you use -p 6379:6379, docker would bind port 6379 of redis container on port 6379 of your host. means that you can connect to this redis via port 6379 even out of localhost, for example in network ...
In this case, command redis-cli -h 127.0.0.1 -p 6379 would be right, and also you can use your network ip instead of localhost ip
If you don't want to connect to this redis over network, it possible not to bind ports, and connect to redis container via its ip, means that use docker run -d --rm --name redis redis:alpine, then try to connect with redis-cli -h CONTAINER_IP -p 6379. you can find container ip with docker inspect redis
Note that browser tries to make connection over http, and won't response any for things like this

What goes in "some-network" placeholder in dockerized redis cli?

I'm looking at documentation here, and see the following line:
$ docker run -it --network some-network --rm redis redis-cli -h some-redis
What should go in the --network some-network field? My docker run command in the field before did default port mapping of docker run -d -p 6379:6379, etc.
I'm starting my redis server with default docker network configuration, and see this is in use:
$ docker container ls
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
abcfa8a32de9 redis "docker-entrypoint.s…" 19 minutes ago Up 19 minutes 0.0.0.0:6379->6379/tcp some-redis
However, using the default bridge network produces:
$ docker run -it --network bridge --rm redis redis-cli -h some-redis
Could not connect to Redis at some-redis:6379: Name or service not known
Ignore the --network bridge command and use:
docker exec -it some-redis redis-cli
Docker includes support for networking containers through the use of network drivers. By default, Docker provides two network drivers for you, the bridge and the overlay drivers. You can also write a network driver plugin so that you can create your own drivers but that is an advanced task.
Read more here.
https://docs.docker.com/engine/tutorials/networkingcontainers/
https://docs.docker.com/v17.09/engine/userguide/networking/
You need to run
docker network create some-network
It doesn't matter what name some-network is, just so long as the Redis server, your special CLI container, and any clients talking to the server all use the same name. (If you're using Docker Compose this happens for you automatically and the network will be named something like directoryname_default; use docker network ls to find it.)
If your Redis server is already running, you can use docker network connect to attach the existing container to the new network. This is one of the few settings you're able to change after you've created a container.
If you're just trying to run a client to talk to this Redis, you don't need Docker for this at all. You can install the Redis client tools locally and run redis-cli, pointing at your host's IP address and the first port in the docker run -p option. The Redis wire protocol is simple enough that you can also use primitive tools like nc or telnet as well.

Connection refused between containers: Docker

i am new to docker and was trying to connect two docker containers. I pulled a Redis image and ran it:
docker run -d -p 6379:6379 --name redis1 redis
After that i used "docker exec" to add some content in the redis container:
docker exec -it redis1 sh
redis-cli
set name surya
incr counter
After that i created a new container using the same redis image and tried accessing the first redis container:
docker run -it --rm --link redis1:redis --name client redis sh
redis-cli -h redis
Now, instead of connecting to redis1 container i am getting an connection refused error.
Could not connect to Redis at 127.0.0.1:6379: Connection refused.
Can someone tell me how to fix this?
are you trying to connect to it's own redis?
if you are trying to connect to the one you first created it should be:
redis-cli -h redis1
it is working fine on my local machine.
https://github.com/docker-library/redis/issues/45#issuecomment-182599683 .
You could use docker-compose when trying to connect from one container to another.

Cannot connect to redis from different docker container

currently I have one personal docker image uploaded to DockerHub, I am making the changes to connect this application to redis (that is running in another docker container as well)
Now, I have the next message from my app when it tries to connect to redis:
Error saving to redis: dial tcp 127.0.0.1:6379: connect: connection refused
I understand that they're not in the same network, so my app is pointing to 127.0.0.1:6379 that is not executing anything, so, I am looking for the best way to connect those containers in some way that don't make my app depending of the IP where redis is hosted, from my local machine I can use redis, but not from another docker container. Briefly, what I did was:
docker run --name redis_server -p 6379:6379 -d redis
sudo docker run -d --restart=always -p 10000:10000 --link redis_server:redis --name my_app repo/my_app
So, I am looking for solutions on how to make 127.0.0.1:6379 accessible for my_app. I don't use docker-compose by the way
The easiest solution would be to run Docker with "--network=host" which binds the containers to the host network. This is a perfectly fine solution for testing, however it will expose the ports and services respectively to the internet (if you don't have a firewall), which may or may not be secure.
Another way of doing this would be to define a network in the following way:
docker network create -d bridge my-net
And then to run your containers on that same network by running the docker run command with --network=my-net. This way you can reference each container by its name. For example in your case you can ping my_app from the Redis container and vice-versa.

Linking Docker Containers

I have a nodejs app i'm trying to run in a docker container. My app uses mongodb and redis.
I've pulled down both a mongo and redis container and dockerized my app.
I started up my mongo and redis containers like:
docker run -i -t --name redis -d redis
docker run -i -t --name mongo -d mongo
Now, I link my nodejs app container to both of these and run the app:
docker run -i -t --name myapp --link mongo:mongo --link redis:redis mseay/myapp node /myapp/server.js
When I run my app, it fails with the error
Error: Redis connection to localhost:6379 failed - connect ECONNREFUSED
My app cannot connect to either my redis container or mongo even though they're both running.
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
8709c818014a redis:latest "/entrypoint.sh redi 12 minutes ago Up 12 minutes 6379/tcp redis
6d87364ad4c9 mongo:latest "/entrypoint.sh mong 12 minutes ago Up 12 minutes 27017/tcp mongo
Any ideas?
Make sure that you are connecting to your mongodb and redis instance as so:
Note that I have made some changes how you link your containers. The names are important as they are referred later.
docker run -i -t --name myapp --link mongo:MONGODB --link redis:REDIS mseay/myapp node /myapp/server.js
For connecting to MongoDB:
IP = process.env.MONGODB_PORT_27017_TCP_ADDR
PORT = process.env.MONGODB_PORT_27017_TCP_PORT
var mongoUrl = 'mongodb://' + IP + ':' + PORT + '/';
or you can simply use:
var mongoUrl = 'mongodb://' + MONGODB + ':27017/';
Similarly connect to redis database by using its ip as REDIS.
Explanation:
When you create a docker container and link other docker containers via the --link parameter, docker modifies your containers hosts file and inserts the IP of the linked containers against their names (that you choose as --link=container_name:NAME_OF_YOUR_CHOICE).
Hence, if you open a bash in your new container and try to run
ping MONGODB
ping REDIS
you can see that both are reachable, and hence if you try connecting to them, it works (assuming your have mongodb and redis installed in the new container, and that your redis and mongodb instances are running on default ports)
mongo --host=MONGODB
redis-cli -h REDIS
If you are using the official repo for redis
https://registry.hub.docker.com/_/redis/,run the command
docker run --name redis -d redis insted of
docker run -i -t --name redis -d redis
-i -t opens an interactive session
-d opens as a daemon process so both should not be used together .
The linking command seems appropriate.
To check if the container is linked properly with your app,
go into your app using /bin/bash and use env command.You should be able to see two environment variables stating redis host and redis port
This worked for me.Please let us know if you this worked for you also.
Your error message says that you're trying to connect to localhost to get to redis. But you started your container with --link redis:redis, so you should be looking for Redis at hostname redis.
Another cause of "connection refused" can be the Redis config not allowing anything else but 127.0.0.1 to connect. This is for example the default setting if you installed Redis using apt-get install redis-server.
Since the container linking to Redis will get a different originating ip-adress, you will get "Connection refused" when trying to connect.
One solution is to put a hash character in front of the line bind 127.0.0.1 in redis.conf.
This will however allow any host or container to connect to your Redis container, so this is only recommended if you have control over the host, so you can add firewall filters using on the host. Also, make sure that you trust all other containers that are executing on the host, otherwise they will be able to connect to your Redis container. Note that Redis also supports password upon connecting, which would make things safer even though you are sharing the host environment with other peoples containers.

Resources