26

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?

Lost
  • 12,007
  • 32
  • 121
  • 193
  • 2
    Did you ever get a resolution to this problem? It seems like it should be possible for a service running on container A to make a web call to container B. I don't know why the below answer of docker-compose.yml is the accepted solution but it shouldn't be. – user3335999 May 10 '19 at 14:17

4 Answers4

17

The idea is not to pass the url, but the hostname of the other container you want to call.
See Networking in Compose

By default Compose sets up a single network for your app. Each container for a service joins the default network and is both reachable by other containers on that network, and discoverable by them at a hostname identical to the container name.

This is what replace the deprecated --link option.

And if your containers are not running on a single Docker server node, Docker Swarm Mode would enable that discoverability across multiple nodes.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
11

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-consumer-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

mst
  • 354
  • 1
  • 2
  • 15
JSS
  • 2,061
  • 1
  • 20
  • 26
8

You can use the link option with docker run:

Run the API:

docker run -d --name api api_image

Run the client:

docker run --link api busybox ping api

You should see that api can be resolved by docker.

That said, going with docker-compose is still a better option.

lang2
  • 11,433
  • 18
  • 83
  • 133
  • 1
    Just a heads up: Container links are now deprecated in favor of user-defined networks per: https://docs.docker.com/network/links/ – jerney Sep 21 '18 at 17:18
  • 1
    This isn't explained. From one service I want to make a call like "http://some_network/some_method to another. How is this a solution? – user3335999 May 10 '19 at 14:13
6

The problem can be solved easily if using compose feature. With compose, you just create one configuration file (docker-compose.yml) like this :

version: '3'
services:
  db:
    image: postgres
  web:
    build: .
    command: python3 manage.py runserver 0.0.0.0:8000
    volumes:
      - .:/code
    ports:
      - "8000:8000"
    depends_on:
      - db

To make it run, just call up like this :

docker-compose up 

This is the best way to run all your stack, so, check this reference : https://docs.docker.com/compose/

Success!

Imam Digmi
  • 452
  • 4
  • 14
  • 16
    So to understand, we want to make a web call from one docker container to a service in another docker container and the above is a solution? That literally makes no sense. Need to explain more. – user3335999 May 10 '19 at 14:11
  • 1
    Need more explanation of how did it work to be a correct answer – JoDev Nov 18 '20 at 11:52
  • It didn't work for me. Nothing to tell this is the right answer. – Tommy Hoang Jan 10 '23 at 07:55