How to map docker container address to custom 'redis://redis:6379' host? - docker

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

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

Cannot access dockerized MySQL instance from another container

When I start MySQL :
docker run --rm -d -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -v /Docker/data/matos/mysql:/var/lib/mysql mysql:5.7
And start PHPMyAdmin :
docker run --rm -d -e PMA_HOST=172.17.0.1 phpmyadmin/phpmyadmin:latest
PMA cannot connect to the DB server.
When I try with PMA_HOST=172.17.0.2 (which is the address assigned to the MySQL container), it works.
But :
as MySQL container publishes its 3306 port, I think it should be reachable on 172.17.0.1:3306.
I don't want to use the 172.17.0.2 address because the MySQL container can be assigned another address whenever it restarts
Am I wrong ?
(I know I can handle this with docker-compose, but prefer managing my containers one by one).
(My MySQL container is successfully telnetable from my laptop with telnet 172.17.0.1 3306).
(My docker version : Docker version 20.10.3, build 48d30b5).
Thanks for your help.
Create a new docker network and start both containers with the network
docker network create my-network
docker run --rm -d --network my-network -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 -v /Docker/data/matos/mysql:/var/lib/mysql --name mysql mysql:5.7
docker run --rm -d --network my-network -e PMA_HOST=mysql phpmyadmin/phpmyadmin:latest
Notice in the command that I've given the mysql container a name 'mysql' and used it as the address for phpmyadmin
Just found out the problem.
My ufw was active on my laptop, and did not allow explicitly port 3306.
I managed to communicate between PMA container and MySQL, using 172.17.0.1, either by disabling ufw or adding a rule to explicitly accept port 3306.
Thanks #kidustiliksew for your quick reply, and the opportunity you gave me to test user-defined networks.
maybe it's a good idea to use docker-compose.
Create a docker-compose.yml file and inside declare two services, one web and the other db, then you can reference them through their service names (web, db)
ex: PMA_HOST=db

not able to host machine's redis to docker rails app

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

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

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.

Resources