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.
Related
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
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'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 am trying to connect from an application container to a database container in two situations, one succeeds, one doesn't.
There are two containers on my dockerhost:
mysql container with port 3306 connected to 3356 on dockerhost
application container
At work, dockerhost has IP-address 10.0.2.15, at home, dockerhost has IP-address 192.168.8.11 (hostname -I).
In both situations, I want to connect to the database container from the app container with host 10.0.2.15/192.168.8.11 and port 3356.
When I do this at work (Windows network, Vagrant/Virtualbox dockerhost), this is no problem. I can 'telnet 10.0.2.15 3356' from the app container and connect to the db container.
When I do this at home (Ubuntu), it is impossible to connect. The only way is to use the docker ip address of the db container (172.17.0.2) with port 3306. However, I can ping 192.168.8.11.
The scripts to start the containers are identical; I do not use --add-host, so the dockerhost IP-address is not in /etc/hosts.
Any suggestions?
Ok, use docker to run 3 database instances
docker run --name mysqldb1 -e MYSQL_ROOT_PASSWORD=changeme -d mysql
docker run --name mysqldb2 -e MYSQL_ROOT_PASSWORD=changeme -d mysql
docker run --name mysqldb3 -e MYSQL_ROOT_PASSWORD=changeme -d mysql
Each one will have a different IP address on my host machine:
$ for i in mysqldb1 mysqldb2 mysqldb3
> do
> docker inspect --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}' $i
> done
172.17.0.2
172.17.0.3
172.17.0.4
Repeat this on your machine and you'll very likely have different IP addresses.
So how is this problem fixed.
The older approach (deprecated in docker 1.9) is to use links. The following commands will shows how environment variables are set within your linked application container (the one using the database)
$ docker run -it --rm --link mysqldb1:mysql mysql env
..
MYSQL_PORT_3306_TCP_ADDR=172.17.0.2
$ docker run -it --rm --link mysqldb2:mysql mysql env
..
MYSQL_PORT_3306_TCP_ADDR=172.17.0.3
$ docker run -it --rm --link mysqldb3:mysql mysql env
..
MYSQL_PORT_3306_TCP_ADDR=172.17.0.4
And the following demonstrates how links are also created:
$ docker run -it --rm --link mysqldb1:mysql mysql grep mysql /etc/hosts
172.17.0.2 mysql 2a12644351a0 mysqldb1
$ docker run -it --rm --link mysqldb2:mysql mysql grep mysql /etc/hosts
172.17.0.3 mysql 89140cbf68c7 mysqldb2
$ docker run -it --rm --link mysqldb3:mysql mysql grep mysql /etc/hosts
172.17.0.4 mysql 27535e8848ef mysqldb3
So you can just refer to the other container using the "mysql" hostname or the "MYSQL_PORT_3306_TCP_ADDR" environment variable.
In Docker 1.9 there is a more powerful networking feature that enables containers to be linked across hosts.
http://docs.docker.com/engine/userguide/networking/dockernetworks/
you can use my container acting as a NAT gateway to dockerhost without any manually setup https://github.com/qoomon/docker-host