Search code examples
dockergoflaskdocker-composedocker-network

how to get two docker containers one running a flask service and golang services to talk to each other?


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

Solution

  • 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