TitaoYamamoto
TitaoYamamoto

Reputation: 335

how can I communicate between containers?

Im using a container with api gateway in port 80, and I'm needing communicate the api gateway between another containers (all these one using dockerfile and docker-compose). How can I do these others conteiners not expose the port to localhost but communicate internally with the api gateway?

My docker-compose:

version: '3'

services:
  app:
    build:
      context: .
      dockerfile: Dockerfile
    volumes:
      - ./:/usr/src/app
      - /usr/src/app/node_modules
    ports:
      - "3000:3000"

Solution:

Changed docker-compose file to:

version: '3.5'

    services:
      app:
        build:
          context: .
          dockerfile: Dockerfile
        volumes:
          - ./:/usr/src/app
          - /usr/src/app/node_modules
        expose:
          - "3000"
        image: api-name-service
        container_name: api-name-service
        networks: 
          - api-network

networks:
  api-network:
       name: api-network-service

When the services is in the same network, this services can communicate with service name, like "http://api-name-service:3000".

Upvotes: 3

Views: 1708

Answers (2)

Pulkit Pahwa
Pulkit Pahwa

Reputation: 1422

Use docker network. Here is a very good tutorial on the docker website on how to use networking b/w containers: https://docs.docker.com/network/network-tutorial-standalone/

Upvotes: 1

Jonah
Jonah

Reputation: 737

You want to use expose instead of ports:

https://docs.docker.com/compose/compose-file/compose-file-v2/#expose

For example:

services:
    app1:
        expose:
            - "3000"
    app2:
        ...

assuming some API on port 3000 in app1, then app2 would be able to access http://app1:3000/api

Upvotes: 3

Related Questions