How can one Docker container call another Docker container

Viewed 41035

I have two Docker containers

  1. A Web API
  2. A Console Application that calls Web API

Now, on my local web api is local host and Console application has no problem calling the API.However, I have no idea when these two things are Dockerized, how can I possibly make the Url of Dockerized API available to Dockerized Console application?

i don't think i need a Docker Compose because I am passing the Url of API as an argument of the API so its just the matter of making sure that the Dockerized API's url is accessible by Dockerized Console

Any ideas?

4 Answers

This is the best way I have found to connect multiple containers in a local machine / single cluster.

Given: data-provider-service, data-consumer-service

  • Option 1: Using Network
docker network create data-network
docker run --name=data-provider-service --net=data-network -p 8081:8081 data-provider-image
docker run --name=data-consumer-service --net=data-network -p 8080:8080 data-consumer-image

Make sure to use URIs like: http://data-provider-service:8081/ inside your data-provider-service.

  • Option 2: Using Docker Compose

You can define both the services in a docker-compose.yml file and use depends_on property in data-provider-service. e.g.

data-consumer-service:
  depends_on:
    - data-provider-service
     

You can see more details here on my Medium post: https://saggu.medium.com/how-to-connect-nultiple-docker-conatiners-17f7ca72e67f

Related