what URL can docker container A use to access another docker container B (same dev machine, different projects)

I finally found a SO thread that mentioned for newer versions of Docker such as I am using the hostname host.docker.internal accesses the host machine, so one answer to my question is - since I already have assigned different ports to Foo and Bar - the Bar container can use the url http://host.docker.internal:3000/api_test

But it is still preferred to be able to assign static IPs to each container so I am leaving the question open.


TLDR: Use http://host.docker.internal

From https://docs.docker.com/docker-for-mac/networking/,

The host has a changing IP address (or none if you have no network access). From 18.03 onwards our recommendation is to connect to the special DNS name host.docker.internal, which resolves to the internal IP address used by the host. This is for development purpose and will not work in a production environment outside of Docker Desktop for Mac.

I tried and this works for windows as well.

For example, if you are running a python app/microservice in a container on port 5000, you may use this to connect to it:

requests.get('http://host.docker.internal:5000')

You can use docker network for access container A to B. https://docs.docker.com/network/

Use docker network --help.

  1. Create a network

    • docker network create mynetwork
  2. Put the containers both containerA and containerB in this network which is mynetwork in this case.

    • docker network connect mynetwork containerA

    • docker network connect mynetwork containerB

  3. You can access one container to another container with container name now.

Also you can test it with ping. Go A or B container bash and use;

  1. apt-get update

  2. apt-get install iputils-ping

  3. Test it like if you in containerA use ping containerB.

For example-1:

If you want to use it in apache virtualhost settings with port;

  • ProxyPass "/" "http://containerB:3000"

For example-2:

I have two container; base and myphp named. I'm running nodejs app in base container on port 3000. And I'm running apache and php on second container myphp.

Basicly my nodejs app gives me a "Hello Word" output. It's working on container base Here is the code:

'use strict';

const express = require('express');

// Constants
const PORT = 3000;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`); 

Basicly my php app gives me all source code the connected address. It's working on container myphp. Here is the code:

<?php
    $domain = 'http://base:3000';
    $handle = fopen($domain, 'r');
    $content = stream_get_contents($handle);
    fclose($handle);

    echo "<textarea rows='20' cols='80'>$content</textarea>";
?>

Then i put these two container the same network. And when i run this index.php which is myphp container app. It gives me "Hello Word";

So if you put containers the same network you can access each other with its name like "http://containerB:3000" or if your router listening "/api_test" "http://containerB:3000/api_test".

Finally this is what i am doing communicate containers. I hope it works for you too.