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
Related
I have 3 docker applications(containers) in which one container is communicating with other 2 containers. If I run that containers using below command, container 3 is able to access the container 1 and container 2.
docker run -d --network="host" --env-file container1.txt -p 8001:8080 img1:latest
docker run -d --network="host" --env-file container2.txt -p 8080:8080 img2:latest
docker run -d --network="host" --env-file container3.txt -p 8000:8080 img3:latest
But this is working only with host network if I remove this --network="host" option then I am not able to access this application outside(on web browser). In order to access it outside i need to make the host port and container ports same as below.
docker run -d --env-file container1.txt -p 8001:8001 img1:latest
docker run -d --env-file container2.txt -p 8080:8080 img2:latest
docker run -d --env-file container3.txt -p 8000:8000 img3:latest
With this above commands i am able to access my application on web browser but container 3 is not able to communicate with container 1. here container 3 can access the container 2 because there i am exposing 8080 host + container port. But i can't expose again 8080 host port for container 3.
How to resolve this issue??
At last my goal is this application should be accessible on browser without using host network, it should use the bridge network . And container 3 needs to communicate with container 1 & 2.
On user-defined networks, containers can not only communicate by IP address but can also resolve a container name to an IP address. This capability is called automatic service discovery.
Read this for more details on Docker container networking.
You can perform the following steps to achieve the desired result.
Create a private bridge network.
docker network create --driver bridge privet-net
Now start your application containers along with the --network private-net added to your docker run command.
docker run -d --env-file container1.txt -p 8001:8001 --network private-net img1:latest
docker run -d --env-file container2.txt -p 8080:8080 --network private-net img2:latest
docker run -d --env-file container3.txt -p 8000:8000 --network private-net img3:latest
With this way, all the three containers will be able to communicate with each other and also to the internet.
In this case when you are using --network=host, then you are telling docker to not isolate the network rather to use the host's network. So all the containers are on the same network, hence can communicate with each other without any issues. However when you remove --newtork=host, then docker will isolate the network as well there by restricting container 3 to communicate with container 1.
You will need some sort of orchestration service like docker compose, docker swarm etc.
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.
I'm having a rather awful issue with running a Redis container. For some reason, even though I have attempted to bind the port and what have you, it won't expose the Redis port it claims to expose (6379). Obviously, I've checked this by scanning the open ports on the IP assigned to the Redis container (172.17.0.3) and it returned no open ports whatsoever. How might I resolve this issue?
Docker Redis Page (for reference to where I pulled the image from): https://hub.docker.com/_/redis/
The command variations I have tried:
docker run --name ausbot-ranksync-redis -p 127.0.0.1:6379:6379 -d redis
docker run --name ausbot-ranksync-redis -p 6379:6379 -d redis
docker run --name ausbot-ranksync-redis -d redis
docker run --name ausbot-ranksync-redis --expose=6379 -d redis
https://gyazo.com/991eb379f66eaa434ad44c5d92721b55 (The last container I scan is a MariaDB container)
The command variations I have tried:
docker run --name ausbot-ranksync-redis -p 127.0.0.1:6379:6379 -d redis
docker run --name ausbot-ranksync-redis -p 6379:6379 -d redis
Those two should work and make the port available on your host.
Obviously, I've checked this by scanning the open ports on the IP assigned to the Redis container (172.17.0.3) and it returned no open ports whatsoever. How might I resolve this issue?
You shouldn't be checking the ports directly on the container from outside of docker. If you want to access the container from the host or outside, you publish the port (as done above), and then access the port on the host IP (or 127.0.0.1 on the host in your first example).
For docker networking, you need to run your application listening on all interfaces (not localhost/loopback). The official redis image already does this, and you can verify with:
docker run --rm --net container:ausbot-ranksync-redis nicolaka/netshoot netstat -lnt
or
docker run --rm --net container:ausbot-ranksync-redis nicolaka/netshoot ss -lnt
To access the container from outside of docker, you need to publish the port (docker run -p ... or ports in the docker-compose.yml). Then you connect to the host IP and the published port.
To access the container from inside of docker, you create a shared network, run your containers there, and access using docker's DNS and the container port (publish and expose are not needed for this):
docker network create app
docker run --name ausbot-ranksync-redis --net app -d redis
docker run --name redis-cli --rm --net app redis redis-cli -h ausbot-ranksync-redis ping
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
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.