Question

How can the mobile app communicate with Docker in a local machine?

Posted June 24, 2021 145 views
DockerNetworking

I am using Docker for the backend of my mobile app and I need to set communication between my mobile app and docker because I am running docker on my local machine because of testing the API. Postman is working well with my docker but the mobile app is not communicating with my docker as expected. If you could share any sample document or a way I would appreciate it.

docker-compose.yml :

version: '3.4'

services:
  nginx:
    build: ./nginx
    container_name: nginx
    links:
      - game-alpha
      - main-alpha
      - main-beta
    ports:
      - '80:80'
      - '443:443'

  game-alpha:
    build:
      context: .
      dockerfile: ./Dockerfile.game
    container_name: game-alpha
    expose:
      - '3000'
    links:
      - redis
    env_file: 
      - .env
    volumes:
      - ./firebaseConfig.json:/root/firebaseConfig.json

  main-alpha:
    build:
      context: .
      dockerfile: ./Dockerfile.knowin
    container_name: main-alpha
    expose:
      - '8000'
    env_file: 
      - .env
    volumes:
      - ./firebaseConfig.json:/root/firebaseConfig.json
  main-beta:
    build:
      context: .
      dockerfile: ./Dockerfile.knowin
    container_name: main-beta
    expose:
      - '8000'
    env_file: 
      - .env
    volumes:
      - ./firebaseConfig.json:/root/firebaseConfig.json

  polling-alpha:
    build:
      context: .
      dockerfile: ./Dockerfile.polling
    container_name: polling-alpha
    env_file: 
      - .env
    volumes:
      - ./firebaseConfig.json:/root/firebaseConfig.json

  redis:
    image: redis:alpine
    container_name: redis 
    expose:
      - '6379'

nginx.conf :

worker_processes auto;

events {
  worker_connections 1024;
}

http {

  server {
    listen 80 default_server;
    server_name "";
    return 444;
  }

  server {
    server_name game.localhost;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";

      proxy_pass http://game_nodes;
      proxy_redirect off;
    }
  }
  server {
    if ($host = game.localhost) {
      return 301 https://$host$request_uri;
    }


    listen 80;
    listen [::]:80;
    server_name game.localhost;
    return 404;
  }

  upstream game_nodes {
    # enable sticky session
    #ip_hash;
    server game-alpha:3000;
    keepalive 8;
  }

  server {
    server_name localhost;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://main_nodes;
      proxy_redirect off;

    }
  }

  server {
    if ($host = localhost) {
      return 301 https://$host$request_uri;
    }

    listen 80;
    listen [::]:80;
    server_name localhost;
    return 404;
  }

  upstream main_nodes {
    server main-alpha:8000;
    server main-beta:8000;
    keepalive 8;
  }
}

This is my NetworkModule.kt file from app side :

val NetworkModule = module {

    factory {
        Moshi.Builder()
            .add(KotlinJsonAdapterFactory())
            .build()
    }

    factory {
        var URL: String = ""

        URL = "localhost"

        val authInterceptor = getAuthInterceptor(androidApplication())
        val okHttpClient = OkHttpClient().newBuilder()
            .connectTimeout(120, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .addInterceptor(HttpLoggingInterceptor())
            .addInterceptor(authInterceptor)
            .build()


        Retrofit.Builder()
            .baseUrl(URL)
            .client(okHttpClient)
            .addConverterFactory(MoshiConverterFactory.create(get<Moshi>()))
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            .build()
    }

//    Contains Retrofit Singleton Object Which Will Be Injected In Required Service
    single {
        get<Retrofit>().create(ApiService::class.java)
    }
}

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
Submit an Answer
1 answer

Hi There,
still not clear if I understood your scenario but … let me check .

If you have success testing with POSTMAN … than first you need check if the same port you are using in the request from POSTMAN are EXPOSED to the environment where you app are running.

Did you check that ?