Batchen Regev
Batchen Regev

Reputation: 1013

python redirect from container to container

Im using docker-compose version 1.25.0 , i have 2 containers

api - with python script and flask

apach - some gui

I want when i get a curl request to api /test it will redirect to apach container gui internaly, This is the python code in api container:

@app.route('/test')
def test():
    return redirect('apach:4822/#/myapp', code=302)

If i use this code to my host externaly return redirect('10.X.X.X:4822/#/myapp', code=302) it works but i want the redirect to be internally between the containers, the redirect is not working this way.

I tried to do curl to apach:4822/#/myapp from api container and i do see it goes through to the apach container, but with my python code it doesnt and i get the code 302 and page does not exists.

How can i make it work ? Thanks.

Upvotes: 3

Views: 781

Answers (1)

Shashank V
Shashank V

Reputation: 11193

The reason why redirect('apach:4822/#/myapp', code=302) is not working because the name apach is resolvable only inside the docker network set up by docker-compose.

Flask is sending the HTTP 302 code with Location header set to apach:4822/#/myapp to your browser. From the machine where your browser is running, apach can't be resolved. So you need to use an externally resolvable IP only.

Since you have multiple services that you want to expose to external world, you should look at setting up a reverse proxy server like nginx. You can run nginx as a container itself. You could expose your services on different routes using nginx. For example, your api service can be exposed at /api/ path and your apach container can be exposed at /myapp path. Users would connect to the nginx server and nginx would route to the request to appropriate backend service depending on the requested URL. When users connect to /api/test, you could redirect to /myapp with redirect('/myapp', code=302). nginx would then route the request to apach service.

Upvotes: 1

Related Questions