Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can one Docker container call another Docker container

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?

like image 447
Lost Avatar asked May 31 '17 04:05

Lost


3 Answers

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.

like image 61
VonC Avatar answered Sep 24 '22 04:09

VonC


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.

like image 7
lang2 Avatar answered Sep 23 '22 04:09

lang2


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!

like image 4
Imam Digmi Avatar answered Sep 26 '22 04:09

Imam Digmi