Redis through docker in windows gives the error The remote computer refused the network connection

210 Views Asked by At

I am running Redis through Docker in Windows. When I send the request to localhost:6379 it gives the following error:

SocketException: The remote computer refused the network connection.\r\n (OS Error: The remote computer refused the network connection.\r\n, errno = 1225), address = localhost, port = 8233

I am using Docker Desktop and running the official redis.

Following is my code in the dart_frog backend located at routes/cache/redis:

final conn = RedisConnection();

Handler middleware(Handler handler) {
  return (context) async {
Response response;

try {
  final command = await conn.connect('localhost', 6379);

  try {
    await command.send_object(['AUTH', 'default', 'pass']);
    response =
        await handler.use(provider<Command>((_) => command)).call(context);
// in case of success return Response.json(body: {'success': '$success'})
  } catch (e) {
    response = Response.json(
      body: {'success': false, 'message': e.toString()},
    );
  }
} catch (e) {
  response = Response.json(
    body: {'success': false, 'message': e.toString()},
  );
}

return response;
  };
}
1

There are 1 best solutions below

0
Anmol Singh On

Simply creating a container in Docker Desktop from the official redis may not work.

Short Answer:

I used the following command in Powershell for creating redis container in docker-

docker run  --publish=6379:6379 redis

After running it once, a working redis container is created and can be run through Docker Desktop.

Detailed Answer:

Consider the following steps for proper installation of Docker for Windows:

  1. Install Docker Desktop for Windows

  2. Pull the Official Redis Image from Docker Hub using Powershell:

    docker pull redis
    
  3. Create and Run the Redis Container:

    docker run --name my-redis-container -p 6379:6379 -d redis
    

--name my-redis-container: This flag assigns a name (in this case, "my-redis-container") to your Redis container for easy reference.

-p 6379:6379: This flag maps port 6379 on your host machine to port 6379 inside the container, allowing you to access Redis from your Windows environment.

-d: This flag runs the container in detached mode.

  1. Verify the Container is Running:

    docker ps
    
  2. Steps for accessing Redis using docker are mentioned below.

  3. Stop and Remove the Container. This can also be done through Docker Desktop.

    docker stop my-redis-container
    docker rm my-redis-container
    

Steps for accessing Redis running on Docker using dart_frog:

  1. Make a GET request from dart_frog using Command Prompt:

    curl --request GET --url http://localhost:8080/cache/redis
    

After running this command, you should expect to see an empty line as the result.

  1. Make a POST request from dart_frog using Command Prompt:

    curl --request POST --url http://localhost:8080/cache/redis --header "Content-Type: application/json" --data "{\"loggedin\":1}"
    
  2. Check your result using a GET request as mentioned in step 1. You expected result should be:

{"success": "true"}

In case you want to use GET and POST requests using Powershell:

GET:

Invoke-RestMethod -Uri 'http://localhost:8080/cache/redis' -Method 'GET'

POST:

$headers = @{
'Content-Type' = 'application/json'
    }
$body = @{
    'loggedin' = 1
} | ConvertTo-Json

Invoke-RestMethod -Uri 'http://localhost:8080/cache/redis' -Method 'POST' -Headers $headers -Body $body