Search code examples
asp.net-coreredisdocker-composedocker-network

Can not make containers communicate despite being in the same network


Hello i am trying to make a .NET Coreserver communicate with a Redis database.The server works well outside of the container.
The containers are set up.I can access the database using the host-port mapping .
However when i am doing an operation that requires the server to call the database the connection gets refused.

dockercompose

version: '3.3'
    services:
           db:
              image: redis:4.0.5-alpine
              container_name: redis0
              networks:
                - redis-net
              ports:
                - 6381:6379   //i can acess redis from the hostmachine via 6381  but i can't acess it via 6379 from the container with the server

            backend:
              image: server
              container_name: server0
              build: ./Server
              command: ["dotnet","Server.dll"]
              ports:
                - 9400:9300
              networks:
                - redis-net
              environment:
                - dbport=6379
              depends_on:
                - db

        networks:
            redis-net:

Startup

 public void ConfigureServices(IServiceCollection services) {

            services.AddOptions();
            services.AddMvc();

    try {
            var env = Environment.GetEnvironmentVariables();
            var portStr = env["dbport"].ToString();
            System.IO.File.AppendAllText("dock.txt",$"dbport:{portStr}");
            if (!(int.TryParse(portStr,out int redisPort))) {
                return;
            }

            var mux = new Multiplexer();
            var logicalConnection = mux.OpenLink(redius.Address.Create(redisPort));
            services.AddSingleton(logicalConnection);

        } catch (Exception ex) {

            Console.WriteLine("Could not open link, ex:" + ex.Message);
        }
    }

For debugging i am writing the exception that the server throws inside a file in the container and i look it up using docker exec -it <container_id> bash

Controller Method that throws

 public async Task<List<User>> GetUsers()
        {
            try
            {
                var usersRaw = await this.connection.HMGetAllAsync("users");
            }
            catch (Exception ex)
            {
                System.IO.File.AppendAllText(Constants.OUTPUT,ex.Message);
                return null;
            }
            return null;

        }

The error says : connection refused for 127.0.0.1:6379.Is the ip address wrong? Basically the problem is the following:

                Redis    Server
HostPort        6381     9400
ContainerPort   6379     9300
AppPort         6379     9300

I can acess Redis from outside via 6381 and i can also acess Server via 9400. Server can communicate with a Redis both outside containers (so no proprietary library fault).
Can not access Redis from the Server via 6379 (both being in the network -> redis-net)


Solution

  • For the error message, it seems you are using localhost:6379 from server image.

    Try base url with http://redis0:6379 or http://db:6379