Home > Blockchain >  Django 'failed connection' between two containers
Django 'failed connection' between two containers

Time:10-30

I'm currently trying to make a connection between two of my docker containers (The requesting container running Gunicorn/Django and the api container running kroki).

I've had a look at other answers but seem to be coming up blank with a solution, so was hoping for a little poke in the right direction.

Docker-compose:

version: '3.8'

services:
  app:
    build:
      context: ./my_app
      dockerfile: Dockerfile.prod
    command: gunicorn my_app.wsgi:application --bind 0.0.0.0:8000 --access-logfile -
    volumes:
      - static_volume:/home/app/web/staticfiles
    expose:
      - 8000
    environment:
      - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 kroki
    env_file:
      - ./.env.prod
    depends_on:
      - db
  db:
    image: postgres:13.0-alpine
    volumes:
      - postgres_data:/var/lib/postgresql/data/
    env_file:
      - ./.env.prod.db
  nginx:
    build: ./nginx
    volumes:
      - static_volume:/home/app/web/staticfiles
    ports:
      - 1337:80
    depends_on:
      - app
  kroki:
    image: yuzutech/kroki
    ports:
      - 7331:8000

volumes:
  postgres_data:
  static_volume:

settings.py

ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS").split(" ")

Requesting code in django

url = 'http://kroki:7331/bytefield/svg/'   base64_var
        try:
            response = requests.get(url)
            return response.text
        except ConnectionError as e:
            print("Connection to bytefield module, unavailable")
            return None

I'm able to access both containers via my browser successfully, however initiating the code for an internal call between the two throws out

requests.exceptions.ConnectionError: HTTPConnectionPool(host='kroki', port=7331): Max retries exceeded with url: /bytefield/svg/<API_URL_HERE> (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x7f286f5ecaf0>: Failed to establish a new connection: [Errno 111] Connection refused'))

I've had a go accessing the url via localhost:7331 & 127.0.0.1:7331 however neither seem to help at all

CodePudding user response:

When you access to other containers within the same network you don't have to access to them using the exposed port in the host but using instead the actual port where the application within the container is listening.

I made a really simple example so you can understand what's your problem:

version: '3.8'

services:
  app:
    image: busybox
    entrypoint: tail -f /dev/null
  kroki:
    image: yuzutech/kroki
    ports:
      - 7331:8000

From Host

❯ curl -s -o /dev/null -w "%{http_code}" localhost:7331

200

From App

/ # wget kroki:7331
Connecting to kroki:7331 (172.18.0.3:7331)
wget: can't connect to remote host (172.18.0.3): Connection refused
/ # wget kroki:8000
Connecting to kroki:8000 (172.18.0.3:8000)
saving to 'index.html'
index.html           100% |************************************************************************| 51087  0:00:00 ETA
'index.html' saved
  • Related