'Flutter can not connect to the docker localhost parse-server

I am trying to run a parse-server and parse-dashboard via the following docker-compose.yml

docker-compose:

version: '3.9'

services:
  database:
    image: mongo:5.0
    environment:
      MONGO_INITDB_ROOT_USERNAME: admin
      MONGO_INITDB_ROOT_PASSWORD: admin
    volumes:
      - data_volume:/data/mongodb

  server:
    restart: always
    image: parseplatform/parse-server:4.10.4
    ports:
      - 1337:1337
    environment:
      - PARSE_SERVER_APPLICATION_ID=COOK_APP
      - PARSE_SERVER_MASTER_KEY=MASTER_KEY_1
      - PARSE_SERVER_CLIENT_KEY=CLIENT_KEY_1
      - PARSE_SERVER_DATABASE_URI=mongodb://admin:admin@mongo/parse_server?authSource=admin
      - PARSE_ENABLE_CLOUD_CODE=yes
    links:
      - database:mongo
    volumes:
      - data_volume:/data/server
      - ./../lib/core/database/parse_server/cloud:/parse-server/cloud

  dashboard:
    image: parseplatform/parse-dashboard:4.0.0
    ports:
      - "4040:4040"
    depends_on:
      - server
    restart: always
    environment:
      - PARSE_DASHBOARD_APP_ID=COOK_APP
      - PARSE_DASHBOARD_MASTER_KEY=MASTER_KEY_1
      - PARSE_DASHBOARD_USER_ID=admin
      - PARSE_DASHBOARD_USER_PASSWORD=admin
      - PARSE_DASHBOARD_ALLOW_INSECURE_HTTP=true
      - PARSE_DASHBOARD_SERVER_URL=http://localhost:1337/parse
    volumes:
      - data_volume:/data/dashboard

volumes:
  data_volume:
    driver: local

After the container is running via docker-compose up I am trying to connect to it using Flutter and write a new class to my server:

Flutter code:

import 'package:flutter/material.dart';
import 'package:parse_server_sdk_flutter/parse_server_sdk.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  const keyApplicationId = 'COOK_APP';
  const keyClientKey = 'CLIENT_KEY_1';
  const keyParseServerUrl = 'http://localhost:1337/parse';

  var res = await Parse().initialize(keyApplicationId, keyParseServerUrl,
      clientKey: keyClientKey, autoSendSessionId: true);
     
  var connRes = await res.healthCheck();
  var s = connRes.error?.message ?? "";
  print("ERROR:" + s);

  var firstObject = ParseObject('FirstClass')
    ..set(
        'message', 'Hey ! First message from Flutter. Parse is now connected');
  await firstObject.save();

  print('done');

}

My error message:

SocketException: Connection refused (OS Error: Connection refused, errno = 111), address = localhost, port = 35262

But for some unknown reason, I can not connect to my local server even if I can access with no problem my dashboard.



Solution 1:[1]

Your application running on the mobile device/emulator will treat localhost as own machine. In order to access the parse server running on your host machine (docker image exposing 0.0.0.0 global) you need to specify the host IP address.

Just replace const keyParseServerUrl = 'http://localhost:1337/parse'; with const keyParseServerUrl = 'http://YOUR_HOST_IP_ADDRESS:1337/parse'; provided in the docker parse server 0.0.0.0 should be exposed.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 user109640