Docker-compose access container with service name in python file?

Viewed 2861

I have a two container and How I can access another container in python file with django web server

Docker-compose.yml file

version: '2'

services:

  web:
    build: ./web/
    command: python3 manage.py runserver 0.0.0.0:8001
    volumes:
      - ./web:/code
    ports:
      - "8001:80"
    networks:
      - dock_net
    container_name: con_web
    depends_on:
      - "api"
    links:
      - api
  api:
    build: ./api/
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - ./api:/code
    ports:
      - "8000:80"
    networks:
      - dock_net
    container_name: con_api

networks:
  dock_net:
      driver: bridge

Python File:

I can get mail_string from form

   mail = request.POST.get('mail_string', '') 
   url = 'http://con_api:8000/api/'+mail+'/?format=json'
   resp = requests.get(url=url)         
   return HttpResponse(resp.text)

I request api container and get value but I dont know ip address

1 Answers
Related