douan2907
2019-03-02 10:13
浏览 125

如何获得两个运行Flask服务和Golang服务的Docker容器互相通信?

I have a flask service running through docker-compose on port 5000. Similarly, I have a different go service running through another docker-compose on port 8000. The Golang service needs to call a flask API running on 5000. I am facing trouble in getting the go service to call flask service. I have tried adding docker-network but failed. What are the pros and cons of running both the services through different docker-compose as compared to single docker-compose? (I have not been able to successfully run them in a single docker-compose, btw). docker ps running both the containers.

Flask Docker compose

version: '3'  # version of compose format

services:
  bidders:
    build:
      dockerfile: Dockerfile
      context: .
    volumes:
      - .:/usr/src/bidders # mount point
    ports:
      - 5000:5000  # host:container

Go Docker Compose

version: '3'

services:
  auctions:
    container_name: auctions
    build: .
    command: go run main.go
    volumes:
      - .:/go/src/auctions
    working_dir: /go/src/auctions
    ports:  
      - "8000:8000"

Third Nwtwork Docker-compose.yml

#docker-compose.yml
version: '3'

networks:
    - second_network

networks:
  second_network:
    driver: bridge

图片转代码服务由CSDN问答提供 功能建议

我有一个烧瓶服务通过docker-compose在端口5000上运行。类似地,我有一个不同的go服务正在运行 另一个在端口8000上的docker-compose。Golang服务需要调用在5000上运行的flask API。在将go服务调用flask服务时,我遇到了麻烦。 我尝试添加docker-network但失败了。 与单个docker-compose相比,通过不同的docker-compose运行这两种服务的优缺点是什么? (我无法在单个docker-compose,btw中成功运行它们)。 同时运行两个容器的docker ps

烧瓶Docker撰写

 版本:'3'#撰写格式的版本
 
服务:
投标人:
构建 :
 dockerfile:Dockerfile 
上下文:。
卷:
-。:/ usr / src / bidders#挂载点
端口:
-5000:5000#host:容器
  <  / pre> 
 
 

执行Docker Compose

 版本:'3'
 
服务:
拍卖:
容器名称:拍卖
构建:  。
命令:运行main.go 
卷:
-。:/ go / src / auctions 
 working_dir:/ go / src / auctions 
端口:
-“ 8000:8000” 
 \  n   
 
 

第三个Nwtwork Docker-compose.yml

 #docker-compose.yml 
version:'3'\  n 
networks:
-second_network 
 
networks:
 second_network:
驱动程序:网桥
   
 
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dsuoedtom207012191 2019-03-02 10:19
    已采纳

    With a single docker-compose.yml it will be easier to make both services inside the same network. So what was the issue you got while doing this ? Also make sure that your flask and go application both are binding to 0.0.0.0 from the code itself and not 127.0.0.1 so you can reach them from outside the container.

    With two docker-compose.yml you have two options:

    • Create a network through one of these files and make the other container which in another file join this external network.

    • Create a network using docker network create and define an external network in both files for your containers

    There is a similar question that you can check it's answer from here with example included

    You can check Networking in Compose for more information

    已采纳该答案
    打赏 评论

相关推荐 更多相似问题