首页 > 解决方案 > 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?

标签: htmldockerdocker-compose

解决方案


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.


推荐阅读