Engineer999
Engineer999

Reputation: 3955

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

Upvotes: 3

Views: 9809

Answers (1)

usuario
usuario

Reputation: 2467

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

Upvotes: 5

Related Questions