skip to Main Content

Problem:

I have a mid-sized team of student developers working on a wide variety of devices. We’re using expo-go to live-preview our development progress on our mobile phones. We need to update the REACT_NATIVE_PACKAGER_HOSTNAME environment variable in the docker-compose.yml file to the public ip of our devices. Right now this is being done manually, which is a minor time sink and doesn’t meet our "business requirements".

This project is being run inside a VSCode Devcontainer.

Docker:

Docker Desktop – v4.12.0

Docker Engine – v20.10.17

Note:
We are junior developers with little to no experience in Docker, please answer thoroughly and forgive any "basic" mistakes we’ve made.

docker-compose.yml:

version: '3.8'
services:      
  expo:
    build:
      context: ../App/frontend
      args:
        - NODE_ENV=development
    environment:
      - NODE_ENV=development
      - EXPO_DEVTOOLS_LISTEN_ADDRESS=0.0.0.0 
      - REACT_NATIVE_PACKAGER_HOSTNAME=<YOUR IP>
    tty: true
    ports:
      - '19006:19006'
      - '19001:19001'
      - '19002:19002'
      - '19000:19000'
    volumes:
      - ../App:/opt/App:delegated
      - ../App/frontend/package.json:/opt/App/frontend/package.json
      - ../App/frontend/package-lock.json:/opt/App/frontend/package-lock.json
      - notused:/opt/App/frontend/node_modules
    healthcheck:
      disable: true
  
  apps:
    build:
      context: ../App/backend
      dockerfile: Dockerfile.app.prod
    volumes:
      - ../App/backend/src:/home/App/backend
    depends_on:
      - database
      - expo

  database:
    image: postgres:latest
    environment:
      POSTGRES_USER: *****
      POSTGRES_PASSWORD: *****
      POSTGRES_DB: *****
    expose:
      - '5432'
    ports:
      - 5433:5432

volumes:
  notused:

What We’ve Tried

  • Makefile that sets environment variable on host machine before devcontainer starts
    • Couldn’t figure out how to start devcontainer from makefile/command line
  • Running script/command from inside docker to identify host IP
    • Unable to get IP Address stored in variable, or get host IP
  • Using docker variables (e.g. host.docker.internal)
    • Results in exp://host.docker.internal:19000 invalid IP address.

2

Answers


  1. You can set this script

    mobile-app:
      IP_ADDRESS=$$(hostname -I | cut -d ' ' -f 1); 
      docker-compose run --rm -e REACT_NATIVE_PACKAGER_HOSTNAME=$$IP_ADDRESS expo npm start
    
    Login or Signup to reply.
  2. You can use MY_PUBLIC_IP=$(curl ifconfig.me) to store your machine’s public IP as system env variable, then in docker-compose file you can use

    - REACT_NATIVE_PACKAGER_HOSTNAME=${MY_PUBLIC_IP}
    

    https://docs.docker.com/compose/environment-variables/set-environment-variables/#substitute-from-the-shell

    Example Script:
    example.sh

    #!/usr/bin/bash
    
    MY_PUBLIC_IP=$(curl ifconfig.me)
    docker compose up
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search