Reputation: 13262
In my docker-compose file, redis is exposed at 6379, and reader
is a gcc container.
reader:
build:
context: ./reader-manager
dockerfile: reader_manager.dockerfile
volumes:
- './reader-manager:/avn/reader-manager'
depends_on:
- redis
redis:
image: redis
ports:
- 6379:6379
In my C++ code, I am having trouble connecting to the redis instance:
auto redis = Redis("tcp://127.0.0.1:6379");
redis.set("key", "val");
This results in
Failed to connect to Redis: Connection refused
Other containers in the environment (like flask) are connecting fine to redis, although they use a url starting with redis://
but when I try in my C++ code:
auto redis = Redis("redis://127.0.0.1:6379");
redis.set("key", "val");
I get
invalid URI: invalid type.
I've also tried unix://
with the result:
Failed to connect to Redis: No such file or directory
Not sure if it matters, but here's the reader dockerfile
FROM gcc:latest
RUN apt-get update -y
RUN apt-get install cmake -y
# install/compile hiredis
WORKDIR /avn
RUN git clone https://github.com/redis/hiredis.git
WORKDIR /avn/hiredis
RUN make
RUN make install
# install/compile redis-plus-plus
WORKDIR /avn
RUN git clone https://github.com/sewenew/redis-plus-plus.git
WORKDIR /avn/redis-plus-plus
RUN mkdir compile
WORKDIR /avn/redis-plus-plus/compile
RUN cmake -DCMAKE_BUILD_TYPE=Release -DREDIS_PLUS_PLUS_BUILD_TEST=OFF -DREDIS_PLUS_PLUS_CXX_STANDARD=17 ..
RUN make
RUN make install
# compile and run the program
WORKDIR /avn/reader-manager
CMD make && ../../bin/main
Upvotes: 0
Views: 801
Reputation: 238281
127.0.0.1 is a loop-back address. A loop-back address connects back to the container that makes the connection. If the C++ program runs in the reader
container, then redis isn't running in the same container. You need to connect to the redis
container:
auto redis = Redis("tcp://redis:6379");
I wouldn't recommend hard-coding it in the executable though. Make it configurable.
Upvotes: 1