so I have multiple services runing in the same docker network with flask
they are set like this:
version: '3'
services:
login:
build:
context: ./repo_login
dockerfile: Development.Dockerfile
volumes:
- ./repo_login:/home/app
ports:
- "5007:5001"
restart: unless-stopped
environment:
ENV: 'development'
networks:
- qa_network
jobs:
build:
context: ./repo_jobs
dockerfile: Development.Dockerfile
volumes:
- ./repo_jobs:/home/app
ports:
- "5017:5001"
restart: unless-stopped
environment:
PYTHONUNBUFFERED: 1
ENV: 'development'
networks:
- qa_network
sla:
build:
context: ./repo_sla
dockerfile: Development.Dockerfile
volumes:
- ./repo_sla:/home/app
ports:
- "5008:5001"
restart: unless-stopped
environment:
ENV: 'development'
networks:
- qa_network
I know I can petitions from on service to the another using only the service name like this:
requests.post('http://jobs:5001/some/api', data=json.dumps(data), headers=headers)
but is there a way to know from which service a petition came from?
I already try with request.environ, but I only get An IP and port from whitch I don’t know its service. I used the next code
@app.route('/some/api', methods=['POST'])
def execute_task():
try:
origin = request.environ['REMOTE_ADDR']
origin_port = request.environ['REMOTE_PORT']
logging.warning(f'the origin of the petition is: {origin} : {origin_port}')
except Exception as ex:
data = None
message = getattr(ex, 'message', str(ex))
logging.exception(ex)
2
Answers
I found a way you can resolve de dns for the services that you have and compare it with the remote_addr in the request Something like this:
This can be expanded to check all the services you have
you can use more advanced tools like service discovery systems that do the job. But there is a simple solution for it if you don’t want to use those tools.
You can add the headers in your
requester
application, like:and then in your
receiver
application, you can check the headers:Then you will have the service name to play with.