Connection refused between containers: Docker - 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.

Related

Redis docker, cannot connect to redis-cli

I am trying to run redis with persistence storage. I followed official docker page of redis fo installation.
I pulled the image using -
docker pull redis
I started redis with persistence storage using -
docker run --name some-redis -d redis redis-server --appendonly yes
And I am tring to connect to redis instance with redis cli with -
docker run -it --network some-network --rm redis redis-cli -h some-redis
It gives me following error -
docker: Error response from daemon: network some-network not found.
I am following the documentation to the point, why is this not working?
Both answers give interesting information but lack the main thing.
In docker, containers may connect to some networks.
By default if you don't specify any network when you run a container, it will use the default bridge network : inside that network any container can communicate with any other but only via their ip addresses.
With the default bridge network
You cannot communicate between containers of that network via container name.
So here docker run -it --network some-network --rm redis redis-cli -h some-redis, the some-redis part is not a resolvable hostname.
To overcome that, you have to refer the container by its ip address.
Inspect the container to know that :
docker container inspect some-redis | grep -i ipaddress
You should get something like : "IPAddress": "172.17...."
Now specify the ip address as -h parameter and it should be fine :
docker run -it --network some-network --rm redis redis-cli -h 172.17...
That is really not a net/reusable/portable way to make two containers to communicate each other. That is more suitable to experiment things.
With a custom bridge network
You can communicate between containers of that network via container name.
Create your network :
docker network create redis-network
Run the redis server and connect it to that network :
docker run --name some-redis -d --network redis-network redis redis-server --appendonly yes
Run the redis client and connect it to that network :
docker run -it --rm --network redis-network redis redis-cli -h some-redis
Now the client can connect to the server instance via -h some-redis.
First create the network
docker network create some-network
then use it in all your containers that have to use the network.
docker run --network some-network
When you have finished remove the network.
docker network rm some-network
First ,you this command run as redis-server
docker run --name some-redis -d redis redis-server --appendonly yes
Next,you want run redis-cli to connect redis-server
should be attach redis-server container and run redis-cli
docker exec -it some-redis redis-cli -h
Of course, if you want to access through the network like a redis page, you need to attach the network of the cli's container to the redis-server container to share the network
docker run --rm -it --net=container:some-redis redis redis-cli -h

Connect to Redis Docker container from a none docker app in a remote server

Goal: Connect to Redis via an app from a remote server.
Problem: I don't know the exact syntax of a Redis container creation.
You have to expose ports from docker to the world.
docker run --name some-redis -d -p 6379:6379 redis
But you need to be carefuly if you doing this on public IP,
so is better to attach a config file with security enabled.
docker run --name some-redis -d -p 6379:6379 \
-v /path/redis.conf:/usr/local/etc/redis/redis.conf \
redis redis-server /usr/local/etc/redis/redis.conf
Bind Redis container on host port & connect from the remote server using "REDIS_HOST:REDIS_HOST_PORT".
docker run -d --name redis -v <data-dir>:/data -p 6379:6379 redis
You should be able to connect to redis now from remote app server on REDIS_HOST and port 6379.
PS - The DNS/IP address of the Redis host should not change.
Ref - https://docs.docker.com/config/containers/container-networking/#published-ports

Subscribe to Redis Instance in Azure using redis-cli in 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.

why I can't ping my docker container?

I run a docker container, which is named "redis". I want to use the "redis" container redis service, but I can't ping the container!
As the picture shows, my "redis" container is IP address is 172.17.0.15, but I can't connect to it.
I want to use the redis services. What is wrong with my configuration?
Because you're not on the same network. Containers are started on their own network by default, separate to the host's network.
If you run:
docker run -it debian ping 172.17.0.15
You should find it works. Even better, you can link containers and refer to them by name:
$ docker run -d --name redis redis
$ docker run --link redis:redis redis redis-cli -h redis ping
PONG
If you really want to access redis from your host, just publish a port through to the host:
$ docker run -d -p 6379:6379 redis
You should now be able to reach it at localhost:6379 on the host.

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