html - Unable to access other service of docker-compose from HTML
问题描述
I want to connect two services via docker-compose. One of them is one HTML file being served by Apache, and another is going server, that simply logs the request and return 200, succeed. I don't know what is the correct way of writing an action link in the <form>
tag.
I've named my images like frontend and server and signed action property as http://server:8080/user/add.
Here is my form:
<form action="http://server:8080/user/add" method="post">
Name: <input name="name"><br>
Sname: <input name="sname"><br>
Email: <input type="email" name="email"><br>
<button type="submit"></button>
</form>
docker-compose.yml:
version: "3"
services:
server:
image: server
frontend:
image: frontend
ports:
- '8080:80'
server works as expected (if run it locally) with
curl -d "name=name&sname=sname&email=email" -X POST http://localhost:8080/user/add
But after hitting the button browser redirects me to server:8080 and tells that page is not found.
What is the right way of linking services in my case?
解决方案
When you serve html file from your apache running on docker, it will be render on your browser (your host machine). So when you complete the form and post it, the browser will send the data to http://server:8080/user/add
that is not publicly accesible, and I see that you don't want to expose it directly. If you want to send the data from your frontend
server, you will need to add some backend functionality to your frontend application that handles the response data that the clients will send (because the browser is which sends the form, not your frontend
).
To summarize if you do not expose your server
to be accesible, you will need to add something else to handle the data that the client sends and then send it to your server.
推荐阅读
- firebase - 可以使用 node-schedule 和 CloudFuntions Firebase 长时间安排任务
- wordpress - WooSquare 同步和重复 SKU
- c# - 如何以最有效的方式更改多个按钮上的文本,而无需在 C# 中重复代码?
- python - 如何使用 python 请求发送表单数据?
- r - 如何按项目计算分类变量的百分比?
- swift - 如何在不使用 Any 的情况下在 Swift 中存储具有关联类型的协议数组?
- c++ - 如何在不复制的情况下在结构中使用来自 stl 的容器
- highcharts - 为什么在使用 400 个数据点时会出现“无法读取未定义的属性 'isValid'”?
- python - 如何修复 open-ai 健身房中的环境错误?
- c++ - 如何使用 mingw-w64 的 Visual Studio 静态库?