1

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

  • 1
    The hostname of the Redis container is "redis", how are you trying to communicate? – Maroun Mar 19 '19 at 13:06
  • I currently use two containers one container has app_server and another is redis but they are not communicating through a network – Suraj Agarkar Mar 19 '19 at 13:11
  • docker-compose sets a default network for you. – Maroun Mar 19 '19 at 13:13
  • I want to create custom network – Suraj Agarkar Mar 19 '19 at 13:14
  • How is the app server configured to reach redis? From the error it looks like it is using `localhost` or the loopback interface; try using the hostname `redis` instead. – Mike Mar 19 '19 at 13:16
  • Possible duplicate of [docker-compose: redis connection refused between containers](https://stackoverflow.com/questions/41302791/docker-compose-redis-connection-refused-between-containers) – David Maze Mar 19 '19 at 13:43

2 Answers2

2

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.

Christian W.
  • 2,532
  • 1
  • 19
  • 31
0

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/

Truong Dang
  • 3,119
  • 1
  • 15
  • 21