简体   繁体   中英

Docker container can't connect to Redis

I have a Docker container running a C application using hiredis, which should write data to a Redis server exposed at the default address and port running locally on the same Linux device at 127.0.0.1:6379 .

The Redis server is running in a different Docker container. I start this container running, exposing port 6379 as follows: sudo docker run --name redis_container -d -p 6379:6379 40c68ed3a4d2

redsi-cli can connect to this via 127.0.0.1:6379 without issues.

However, no matter what I try, my container which should write to the Redis gets a Redis connection refused error from the C code all the time. This was my last attempt at running the container: sudo docker run --expose=6379 -i 7340dfee8ea5

What exactly am I missing here? Thanks

The C client is running inside a container, that means 127.0.0.1 points to the container itself, not to your host. You should configure the redis client to redis_container:6379 as that is the name you have used when docker run the redis container. More about this here

Besides, both containers need to be inside the same docker network. Use the following command to create a simple network

docker network create my-net

and add --network my-net to both docker run commands (redis client and redis server)

You can read more about docker network here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM