Connecting redis to a container image

Hi there, I am actually trying to connect my app container to Redis container. However, there is a connection error: Carmine connection error {}

This is my docker file:

FROM clojure:openjdk-8-lein-2.9.6

RUN mkdir -p /usr/src/app

WORKDIR /usr/src/app

COPY project.clj /usr/src/app/

RUN lein deps

COPY . /usr/src/app

RUN mv "$(lein uberjar | sed -n 's/^Created \(.*standalone\.jar\)/\1/p')" app-standalone.jar

CMD ["java", "-jar", "app-standalone.jar"]

Please note that I ensure that the Redis container is up and running before running my app container.

My question to this is, do we need to do some configuration in the Dockerfile to create some sort of tunnel between my app container and Redis container to be able to get the connection?

Thank you in advance for your help

How do you run the redis and app container?
docker run … ? compose ?

How are you trying to connect from the app to redis? which ip/hostname?

I am trying to run it through docker run

Yes, I am trying to connect from the app - PORT 6379 localhost

Good evening,
localhost from within a docker-container is NOT the Docker-host’s localhost.
On Windows you can use host.docker.internal to connect to the Docker-host.
On linux you can use 172.17.0.1 which is the Docker-host in Docker’s default network.
Both options above require that you have forwarded the redis-container’s port 6379 to the Docker-host using docker run -p 6379:6379 ... (see docker run | Docker Documentation)
Or you can use the container’s name as DNS-name to connect to from one container to another.
But localhost from within a Docker-container will not work :slight_smile:

1 Like

In addition to what @matthiasradde is recommended, you can share the network namespace between containers.

docker run -d --name "server" httpd:2.4
docker run --rm -it --network "container:server" curlimages/curl:7.79.1 localhost

This way the server container’s localhost is the same as the curl container’s localhost,

2 Likes

Thank you matthiasradde and rimelek for your guidance! I can now connect to Redis.