Docker compose for java后端和python前端应用程序

2024-10-01 15:33:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个简单的spring boot java后端应用程序,它从列表中获取元素(endpoint/get/{id}),并将元素添加到列表中(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);
    }
}

我为此创建了Dockerfile,如下所示:

FROM openjdk:11

# copy the packaged jar file into our docker image
COPY target/demo-0.0.1-SNAPSHOT.jar /demo.jar

EXPOSE 8080/tcp

# set the startup command to execute the jar
CMD ["java", "-jar", "/demo.jar"]

现在我可以构建映像(docker build -t java_backend .)并运行应用程序(docker run --rm -p 8080:8080 java_backend

因此,在localhost:8080/get/0下我得到了"test",在localhost:8080/add/something下我可以添加元素。一切都很好

作为前端,我有一个简单的python应用程序,如:

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()

和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 /
RUN sed -i 's/localhost:8080/backend/g' /main.py

EXPOSE 80/tcp

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

最后,我创建了docker-compose.yml文件来运行这两个文件:

version: '3.3'
services:
        backend:
                image: java_backend
                ports:
                         - "8080:8080"

        frontend:
                image: frontend
                ports:
                        - "8081:80"

现在我试着开始docker-compose up --build backend frontend

前端和后端是独立工作的,因此,如果我尝试放置localhost:8080/get/0,我会得到"test",如果我转到localhost:8081,我会看到我的前端。但如果我在文本框中输入一些值,然后单击submit,那么我就得到了500个内部服务器错误。所以服务没有连接。你能告诉我为什么吗


Tags: dockernameformaddidbackendlocalhostinput
2条回答

我还有一个问题要问。假设我希望从容器开始前端,但后端只从本地IDE(IntelliJ)开始

当然,第RUN sed -i 's/localhost:8080/backend/g' /main.py行被注释,因此frontent正在http://localhost:8080下调用端点

在这种情况下,前端在http://localhost:8081下,后端在http://localhost:8080下(两个端点-/get/{id}/add/{product})。但为什么在容器中运行的前端不能“看到”我的后端

我想你是无意中删除了后端端口。试着替换

RUN sed -i 's/localhost:8080/backend/g' /main.py

RUN sed -i 's/localhost:8080/backend:8080/g' /main.py

相关问题 更多 >

    热门问题