Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker-Compose API Call and Endpoint Containerized

Using docker-compose I've got multiple containers running - one of them is the profiler container that calls an API, the other container apiconnection should be receiving the call and sending information back. However when I run the compose file it always returns Get http://apiconnection:8080/maccaption: dial tcp: lookup apiconnection on 127.0.0.11:53: no such host on the Profiler container at the http.DefaultClient.Do line.

This is a sample of my compose file:

apiConnection:
 image: apiconnection:1.0
 ports:
  - "8080:8080"
 networks: 
  - maccaptionNet
profiler:
  image: profiler:1.0
  networks: 
   - maccaptionNet
  depends_on:
   - "apiConnection"

I'm calling the api in my profiler image like so - it's erroring on DefaultClient:

url := "http://apiconnection:8080/maccaption"

req, err := http.NewRequest("GET", url, nil)
if err != nil {
    log.Fatal(err)
}

res, err := http.DefaultClient.Do(req)
if err != nil {
    fmt.Println("Line 27")
    log.Fatal(err)
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
    fmt.Println("Line 33")
    log.Fatal(err)
}

If I modify the url := "http://apiconnection:8080/maccaption" to look at localhost like this url := "http://localhost:8080/maccaption" it changes to connect refused, but I don't think I should be looking at localhost, I need to look at the Docker container don't I?

I'm setting the url as a connection to the docker container and using the ports I've assigned in the docker-compose file. and receiving the request in my apiconnection image like this:

    r := mux.NewRouter()

r.HandleFunc("/maccaption", handleMaccaption).Methods("GET")

http.ListenAndServe(":8080", r)

All it should be doing now is starting up and making a single request to prove that it is working and returning some json information. This code works outside of docker, replacing the container name in the url variable with localhost. But I cannot get it running within docker.

I've looked at these other sources for assistance but to no avail: how to call api endpoint inside docker container? Making a REST Call to Endpoint in Dockers Docker-compose internal communication using endpoints

Any pointers would be greatly appreciated - thank you!

**EDIT - to fix my wonky code formatting

like image 627
henleyhoudini Avatar asked Jul 07 '26 12:07

henleyhoudini


1 Answers

For anyone who's attempting to do something like this in the future: the above code actually does work. But where I was going wrong was that in my url := "http://apiconnection:8080/maccaption" I was referring to the image name instead of the service name I've defined in the docker-compose.

changing the url to look at the service name instead of the image name corrected the issue and I'm now able to make API calls within my dockerized environment.

like image 50
henleyhoudini Avatar answered Jul 10 '26 04:07

henleyhoudini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!