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.

    评论

报告相同问题?

悬赏问题

  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用