Suraj Agarkar
Suraj Agarkar

Reputation: 137

Docker container issue can't communicate

I beginner in Docker, I write the simple docker-compose.yml file for run two service container first container for node app and another one for redis issue with my app server unable to connect with redis container here is my code:

version: '3'
services:
    redis:
        image: redis
        ports:
            - "6379:6379"
        networks:
            - test
    app_server:
        image: app_server
        depends_on:
            - redis
        links:
           - redis
        ports:
            - "4004:4004"
        networks:
            - test
networks:
    test:

Output: Error: Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED

Upvotes: 1

Views: 426

Answers (2)

Truong Dang
Truong Dang

Reputation: 3407

127.0.0.1:6379 is connect to current container localhost not to redis container

With your docker-composer file. Now your connect to redis via redis container name. Becase docker-compose automatic create an docker bridge network - whic allow you call to another container via their name... docker inspect to see redis container name - for example current redis container name is redis_abc, so you can connect to redis via redis_abc:6379 Or more simple, just add container_name: redis_server to docker-compose file for certain container name.. https://docs.docker.com/network/bridge/

Upvotes: 0

Christian W.
Christian W.

Reputation: 2650

Looks like your webapp is connecting to 127.0.0.1/localhost instead of redis. So not a docker issue, but more of a programming issue within your web app. you could add environment variable in your webapp (something like REDIS_HOST) and then give that parameter in the compose-file. This of course requires your web application to read redis host from environment variable.

Example environment variable assignment in compose:

  webapp:
      image: my_web_app
      environment:
          - REDIS_HOST=redis

Again, this requires that your web app is actually utilizing REDIS_HOST environment variable in its code.

Upvotes: 2

Related Questions