miaoz2001
miaoz2001

Reputation: 341

ngrok in docker cannot connect to Django development server

I am working on a localhost django webserver http://localhost:8000, which works fine. Meanwhile i need ngrok to do the port forwarding, ngrok http 8000, which works fine too.

Then I want to put ngrok, postgres, redis, maildev, etc all in docker containers, all others works fine, except ngrok.

ngrok failed to contain to localhost:8000.

I understand why, i suppose because ngrok is running on a seperate 'server 'and the localhost on that server does not have web server running.

I am wondering how i can fix it.

any help is appreciated! Thanks.

here is my docker-compose file:

    ngrok:
        image: wernight/ngrok
        ports:
            - '4040:4040'
        environment:
            - NGROK_PORT=8000
            - NGROK_AUTH=${NGROK_AUTH_TOKEN}
        network_mode: "host"

Upvotes: 2

Views: 2242

Answers (2)

Ashwin Balamohan
Ashwin Balamohan

Reputation: 3332

I was able to get it to work by doing the following:

  1. Instruct Django to bind to port 8000 with the following command: python manage.py runserver 0.0.0.0:8000
  2. Instruct ngrok to connect to the web docker service in my docker compose file by passing in web:8000 as the NGROK_PORT environment variable.

I've pasted truncated versions of my settings below.

docker-compose.yml:

version: '3.7'

services:
  ngrok:
    image: wernight/ngrok
    depends_on:
      - web
    env_file:
      - ./ngrok/.env
    ports:
      - 4040:4040

  web:
    build:
      context: ./app
      dockerfile: Dockerfile.dev
    command: python manage.py runserver 0.0.0.0:8000
    env_file:
      - ./app/django-project/settings/.env
    ports:
      - 8000:8000
    volumes:
      - ./app/:/app/

And here is the env file referenced above (i.e. ./ngrok/.env):

NGROK_AUTH=your-auth-token-here
NGROK_DEBUG=1
NGROK_PORT=web:8000
NGROK_SUBDOMAIN=(optional)-your-subdomain-here

You can leave out the subdomain and auth fields. I figured this out by looking through their docker entrypoint

Upvotes: 1

miaoz2001
miaoz2001

Reputation: 341

UPDATE:

stripe has a new tool [stripe-cli][1], which can do the same thing. just do as below

stripe-cli:
      image: stripe/stripe-cli
      command: listen --api-key $STRIPE_SECRET_KEY
                    --load-from-webhooks-api
                    --forward-to host.docker.internal:8000/api/webhook/

I ended up getting rid of ngrok, using serveo instead to solve the problem, here is the code, in case anyone run into the same problem

serveo:
    image: taichunmin/serveo
    tty: true
    stdin_open: true
    command: "ssh -o ServerAliveInterval=60 -R 80:host.docker.internal:8000 -o \"StrictHostKeyChecking no\" serveo.net"

Upvotes: 3

Related Questions