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
Related
I'm trying to connect my rails app which is in the docker container and trying to connect that to host machine's Redis server which is running on port 6379.
I'm getting
dockefile
EXPOSE 3000
EXPOSE 6379
sudo docker run -it -e RAILS_ENV=development -p 3000:3000 -p 6379:6380 <containerid>
gives error
Redis::ConnectionError: Connection lost (ECONNRESET)
when redis is running on 6380.
and
when I try to run Redis on 6379 I get the following error
with
sudo docker run -it -e RAILS_ENV=development -p 3000:3000 -p 6379:6379
docker: Error response from daemon: driver failed programming external connectivity on endpoint vigorous_turing (2b5c8e2b4f5df5f1bfcccfdfc87fd5ea78c5c2643de4e00774e7dec67acbd8c4): Error starting userland proxy: listen tcp 0.0.0.0:6379: bind: address already in use.
If Redis is running on the host and you want to communicate outside the container, docker recommends using host.docker.internal.
So instead of specifying localhost as the host in your Redis config, use host.docker.internal. Your Rails app will be able to communicate outside now.
-p 6379:6379 is unnecessary
I am writing a celery application which needs to connect to redis at the host:
redis://redis:6379 - it has to be this specific has because of the way my other apps are connecting to redis.
I do the following:
# Install redis from docker hub
docker run -name redis -d redis
Then bind to local port:
docker run -d -p 6379:6379 redis
I know how to map it to localhost:6379 but how do i map it to the host redis://redis:6379?
Any ideas?
Add an entry to your /etc/hosts
127.0.0.1 redis
That's it, now hostname redis is resolved to the loopback address. Then you expose the port as usual:
docker run -d -p 6379:6379 redis
I've installed docker in a VM which is publicy available on internet. I've installed mongodb in a docker container in the VM.Mongodb is listening on 27017 port.
I've installed using the following steps
docker run -p 27017:27017 --name da-mongo -v ~/mongo-data:/data/db -d mongo
The port from container is redirected to the host using the -p flag. But the port 27017 is exposed on the internet. I don't want it to happen.
Is there any way to fix it?
Well, if you want it available for certain hosts then you need a firewall. But, if all you need is it working on localhost (your VM machine), then you don't need to expose/bind the port with the host. I suggest you to run the container without the -p option, then, run the following command:
docker inspect -f '{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' your_container_id_or_name
After that, it will display an IP, it is the IP of the container you've just ran (Yes, docker uses somewhat an internal virtual network connecting your containers and your host machine between them).
After that, you can connect to it using the IP and port combination, something like:
172.17.0.2:27017
When you publish the port, you can select which host interface to publish on:
docker run -p 127.0.0.1:27017:27017 --name da-mongo \
-v ~/mongo-data:/data/db -d mongo
That will publish the container port 27017 to host interface 127.0.0.1 port 27017. You can only add the interface to the host port, the container itself must still bind to 0.0.0.0.
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.
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.