dongwanqiang_2017 2019-02-12 20:56
浏览 218

Docker-Compose API调用和端点容器化

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

  • 写回答

1条回答 默认 最新

  • douyan8961 2019-02-12 22:55
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!