-1

I have simple spring boot java backend application which get element from the list (endpoint /get/{id}) and add element to the list (endpoint /add/{product}):

@RestController
public class DemoController {

    List<String> products = Arrays.asList("test");

    @PostMapping(path="/add/{product}")
    public int addProduct(@PathVariable final String product){
        products.add(product);
        return products.size()-1;
    }

    @GetMapping(path="/get/{id}")
    public String getValue(@PathVariable final int id){
        return products.get(id);
    }
}

As frontend I have simple python application like:

from flask import Flask, request
import requests as r
import os
app = Flask(__name__)

@app.route("/")
def renderProduct():
    return """
            <html>
                <head>
                    <title>""" + os.environ["title"] + """</title>
                </head>    
                <form id="1" method="POST">
                    <input name="getID"/>
                    <br>
                    <input name="addID">
                    <input type="submit">
                </form>
                </html>
                """

@app.route("/", methods=["POST"])
def queryAndRender():
    builded = "<html>"
    if request.form["getID"] is not None:
        resp = r.get("http://localhost:8080/get/" + request.form["getID"])
        builded = builded + "PRODUCT:" + resp.text + "<br>"

    if request.form["addID"] is not None:
        resp = r.get("http://localhost:8080/add/" + request.form["addID"])
        builded = builded + "ADDED ID:" + resp.text + "<br>"

    builded = builded + """<html>
                            <head>
                                <title>""" + os.environ["title"] + """</title>
                            </head>
                            <form id="1" method="POST">
                                <input name="getID"/>
                                <br>
                                <input name="addID">
                                <input type="submit">
                            </form>
                            </html>
                            """

    return builded;


if __name__ == "__main__":
    app.run()

And Dockerfile:

ARG version=3.8.5-alpine3.11
FROM python:${version}

ENV title="Hello world"
ENV test testspacja
ENV FLASK_APP=/main.py

RUN pip install Flask==1.1.2
RUN pip install requests==2.22.0

COPY main.py /

EXPOSE 80/tcp

ENTRYPOINT ["flask", "run"]
CMD ["-h", "0.0.0.0", "-p", "80"]

Now I can run my frontend in docker container:

docker run -p 8081:80 frontend

and it is visible under http://localhost:8081/

Now I would like to start my backend in intelliJ. Why my frontend doesn't "see" backend under endpoints http://localhost:8080/get/{id} and http://localhost:8080/add/{product} ? I can call them from browser but why frontend can't? I see only:

Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.  
milko
  • 11
  • 2
  • `localhost` in Docker is usually "this container"; it doesn't refer to other containers or non-Docker processes running on the same host. [Networking in Compose](https://docs.docker.com/compose/networking/) is good background material, even if you're not otherwise using Compose; you can manually build a similar network setup using a [bridge network](https://docs.docker.com/network/bridge/). – David Maze Sep 28 '20 at 17:08

1 Answers1

-1

I believe localhost for the container isn't the same as for your client. Try to specify the network when running your docker:

docker run --network host -p 8081:80 frontend

I guess it is the same problem as discussed here: Docker is listening to port specified in run command

Ihor Shylo
  • 572
  • 5
  • 12
  • With you command I can not run frontend from my browser. – milko Sep 28 '20 at 17:06
  • Host networking generally disables Docker's networking stack; it's not the right solution for simple inter-container calls. – David Maze Sep 28 '20 at 17:07
  • @David Maze I can tell you, that hardcoding the localhost address in your application is not the best practice either and still the question hasn't been answered... – Ihor Shylo Sep 28 '20 at 17:11
  • @milko You can't access it cause of this line CMD ["-h", "0.0.0.0", "-p", "80"], instead of "0.0.0.0" you need to specify local host. – Ihor Shylo Sep 28 '20 at 17:18
  • @IhorShylo You generally need an option like that `-h 0.0.0.0` or else the service isn't accessible outside its own container; inter-container connections and published ports won't work without it. See _e.g._ [Deploying a minimal flask app in docker - server connection issues](https://stackoverflow.com/questions/30323224/deploying-a-minimal-flask-app-in-docker-server-connection-issues). – David Maze Sep 28 '20 at 17:40
  • But if you run the docker with --network host it should take the network of the host, right? Thus a localhost specified in Dockerfile becomes clients localhost. Or am I wrong? – Ihor Shylo Sep 28 '20 at 17:44