0

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.

tom
  • 83
  • 1
  • 10
  • Please provide a [mcve]. The docker-compose configuration seem correct. Here is a similar problem: https://stackoverflow.com/questions/65860011/docker-nginx-angular-spring-boot/65863448#65863448 – Thomas Sablik Jan 28 '21 at 08:08
  • When you say "an HTTP request to the server inside the client container works but that is not what I want", what specific action are you performing, and how is it different from the code you show? What does the actual client code look like? What is the actual error you're getting? – David Maze Jan 28 '21 at 12:38
  • Sorry it was a stupid mistake from me that in the config.toml file i define the serverIP = "severA" wrongly instead of what is above serverIP = "serverA". Wrong spelling. – tom Jan 28 '21 at 15:47

0 Answers0