4

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

onuryartasi
  • 587
  • 2
  • 6
  • 13

1 Answers1

2

Updated Answer

In your python file, you can use url = 'http://api/'+mail+'/?format=json'. This will enable you to access the url you are trying to get request from.

Original Answer

If the two containers are independent, then you can create a network and when you make both the containers a part of same network then you can access them using their hostname which you can specify by --hostname=HOSTNAME.


Another easy way is to use docker-compose file which creates a network by default and all the services declared in the file are a part of that network. By that you can access other services by their service name. Simply like http://container1/ when your docker-compose file is like this:

version: '3'
services:

  container1:
    image: some_image

  container2:
    image: another_or_same_image

Now enter into container2 by:

docker exec -it container2 /bin/bash

and run ping http://contianer1

You will receive packets and therefore be able to access other container.

Ayushya
  • 9,599
  • 6
  • 41
  • 57
  • I can this but I can't this in python file – onuryartasi Aug 01 '17 at 06:07
  • The answer I gave is complete and you are getting a URL which you can use in your python file. If you are not familiar with docker-compose, and running container using docker run I have given another answer [here](https://stackoverflow.com/a/45197727/6207775) using the containers only. – Ayushya Aug 01 '17 at 06:11
  • Can you give me example? I try getting ip address with service in python file but print service_name – onuryartasi Aug 01 '17 at 06:29
  • Update the question and include how you are trying, I'll be able to help you better if I know what you want to do. Include how you are running containers and also include what you have written in python to access other container. – Ayushya Aug 01 '17 at 06:57
  • But python file in con_web container , api is con_api ? – onuryartasi Aug 01 '17 at 07:38
  • we don't use container name, we use service name to access the container. – Ayushya Aug 01 '17 at 07:39