I have a docker-compose file which defines an HTTP server and client as follows
version: '3'
services:
serverA:
container_name: serverA
image: xxx/apiserver
restart: unless-stopped
ports:
- '9090:9090'
networks:
- apinet
clientA:
container_name: clientA
image: xxx/apiclient
restart: unless-stopped
ports:
- '20005:20005'
depends_on:
- serverA
networks:
- apinet
networks:
apinet:
driver: bridge
The server image contain a simple golang HTTP server code ready to handle request
func main() {
http.HandleFunc("/itemhandler", headers)
http.ListenAndServe(":9090", nil)
}
func itemhandler(w http.ResponseWriter, req *http.Request) {
//handle http request
}
while the client image contain a function SendhttpPOSTRequest which contain an HTTP client module that sends HTTP request to the server and retry until it is able to send the request to the server. The client image when run as container contain a configuration file config.toml file which contain the IP address and PORT of the server.
//
func main() {
SendhttpPOSTRequest() // sends http post request with retry
}
The problem is when start the docker-compose file, both container get created and running however the client keeps sending the HTTP request but could not reach the server even though in the config.toml I have changed the server information as shown below and restart the client container. I have used the server IP address obtained from the apinet network as well and restart the client container but still could not send the HTTP request.
config.toml:
serverIP = "serverA" or serverIP = "172.x.0.x"
serverPort = "9090"
A ping between serverA and ClientA using their name works, eg ping serverA from clientA and vise versa works. HTTP request from the host machine to the server works but the HTTP request from the client does not get to the server. An HTTP request to the server inside the client container works but that is not what I want. What I want is that the client sends HTTP request (executes the SendhttpPOSTRequest() ) to the server when its image container starts and retry until the server is up and running.
I have search stackoverflow but could not get similar problem. Can anyone help me. Am new to docker.
question from:
https://stackoverflow.com/questions/65933078/how-docker-http-client-container-send-http-request-on-start 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…