I’m relatively new to Docker (just a week or two). Here’s my problem, which I’ve spent days trying to resolve:
– My first Docker container is running Redis
– My second Docker container is running Tomcat with a custom web app (Java based REST API)
– The web app uses Redis as a data caching layer
– My primary reference for implementing the linking is https://hub.docker.com/_/redis/
– Currently I’m running on Docker for Windows, but the host platform is likely to change in Production
I start the Redis container with
docker run --name myredisserver -d -p 6379:6379 redis redis-server --appendonly yes
(have also tried with various combinations, such as no -p parameter, etc)
I start the Tomcat container with
docker run --name myapi -p 8080:8080 --link myredisserver:redis -d myapiimage
The code uses the Lettuce library and initialises Redis with
The API (Tomcat) responds fine when calling services that don’t use Redis (e.g. “http://localhost:8080/api/debug” displays the message I expect)
My test service for Redis adds a key/value pair then retrieves it and displays it. When initialising Redis using localhost as above, I get a 500 error:
io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
After trying everything I could find online, in desperation, I got the value of the Redis container’s IP, and changed localhost to that IP:
Then rebuilt and redeployed.
This time it worked as expected. That made it pretty clear what the problem was, but I thought the purpose of the --link parameter was so that it would take care of network communication between the two containers. Also that the -p param on the Redis container would expose port 6379 to localhost. What am I missing?
What’s the best practice for achieving this (what should be quite basic) communication, especially when you don’t know what the final IPs of the two containers will be, or whether they’ll be on different hosts, or clustered (swarmed?), etc.?
Thanks for your help!