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

℡╲_俬逩灬. 提交于 2020-08-21 11:29:21

问题


Assuming we need to do this very rarely only at certain stages of development (to smoke test a few api calls), what is the simplest possible way to let a dockerized web service in project Bar access a dockerized web service in Project Foo?

On a development Mac, Docker Engine: 18.09.2, Compose: 1.23.2, we have Project Foo and Project Bar which each have their own docker-compose files, each with a web service and a database service.

Normally they run stand-alone, and are developed independently.

However Project Foo's web service hosts an API that only occasionally we want to access from Project Bar's web container

They are assigned to different host ports, docker ps shows Project Foo uses port 0.0.0.0:3000->3000/tcp (eg, we use localhost:3000 to access the web service from the Mac's browser. Project Bar uses port 0.0.0.0:3010->3010/tcp

docker inspect for Foo shows it's IP address is "172.18.0.3" (gateway "172.18.0.1") and for Bar shows it's IP address is "172.22.0.4" (gateway "172.22.0.1")

docker network ls shows they are both using the same "bridge" Driver.

Both projects are running on the Mac, 4 containers total, (web+db on Foo, and web+db on Bar)

If a program (Ruby) running on Bar needs to access a REST URL on Foo, what is the URL of "/api_test" on Foo?

From the Bar web container I've tried http://localhost:3000/api_test and http://127.0.0.1:3000/api_test (which is what we'd use from a web browser so didn't really expect that to work from container-to-container) and I've tried http://172.18.0.3:3000/api_test and http://172.18.0.3/api_test neither of which worked.

I see online references to setting up a link or a docker network, but all of the examples are for when using docker run instead of using docker-compose. I would expect that if you know the IP and port of each container's web server, it ought to be a matter of using the correct URL without any extra network setup?

Any help would be appreciated.

A manually-assigned static IP solution is preferred... Before Docker, we used Vagrant and that was simple, in each project's Vagrantfile we simply manually assigned them an IP on the same private subnet 192.168.50.50 and 192.168.50.51 and they "talked" to each other just fine, and we could simply 'code' those IPs into our development code. Docker seems to have an additional layer of abstraction that has me puzzled.


回答1:


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')



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/55311465/what-url-can-docker-container-a-use-to-access-another-docker-container-b-same-d

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!