Establish conversation between hello-world apps in Docker containers

docker

99 观看

2回复

287 作者的声誉

I'm trying to run my hello-world apps inside Docker: frontend need to consume REST from backend.

I run

docker run -p 1337:1337 --net=bridge me/p-dockerfile-advanced-backend:latest
docker run -p 1338:1338 --net=bridge me/p-dockerfile-advanced-frontend:latest http://127.0.0.1:1337

I am able to connect to both of them using a browser from the host OS (My desktop Windows 10 x64) :

enter image description here

enter image description here

The http://127.0.0.1:1337 parameter needed for the frontend application to know where the restful services reside. But the app cannot connect to them. I cannot connect too.

Windows PowerShell
Copyright (C) 2016 Microsoft Corporation. All rights reserved.

PS C:\Users\user1> docker ps
CONTAINER ID        IMAGE                                           COMMAND                  CREATED             STATUS              PORTS                    NAMES
4b0852253b8a        me/p-dockerfile-advanced-frontend:latest   "/usr/bin/java -ja..."   24 minutes ago      Up 24 minutes       0.0.0.0:1338->1338/tcp   laughing_noyce
e73f8a6efa24        me/p-dockerfile-advanced-backend:latest    "/usr/bin/java -ja..."   26 minutes ago      Up 26 minutes                                youthful_chandrasekhar
PS C:\Users\user1> docker exec -it 4b0852253b8a bash
root@4b0852253b8a:/# apt-get install telnet
<...>
root@4b0852253b8a:/# telnet localhost 1337
Trying 127.0.0.1...
Trying ::1...
telnet: Unable to connect to remote host: Cannot assign requested address
root@4b0852253b8a:/#

Unable to connect, but it should because I specified --net=bridge on both containers and backend listen the port 1337 :

root@e73f8a6efa24:/# netstat -lntu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:1337            0.0.0.0:*               LISTEN
root@e73f8a6efa24:/#

PS: I spent almost all day trying to make it work before asking here.

作者: ieXcept 的来源 发布者: 2017 年 12 月 27 日

回应 2


0

2593 作者的声誉

Since these are different containers, you have to expose ports on both of them. Run the first with:

docker run -p 1337:1337 --net=bridge me/p-dockerfile-advanced-backend:latest

Note that bridge is the default network so you it is extra. Both containers will be on the same bridge network by default anyway.

作者: tayfun 发布者: 2017 年 12 月 27 日

1

3838 作者的声誉

决定

The problem is the 127.0.0.1 address.

Each container is assigned, by default, 2 interfaces: eth0 and lo (the loopback interface with the 127.0.0.1 address).

You need to specify the name or address of the previous container. For this simple application you may use the --link option.

docker run -p 1337:1337 --name backend me/p-dockerfile-advanced-backend:latest
docker run -p 1338:1338 --link backend:backend me/p-dockerfile-advanced-frontend:latest http://backend:1337

Note that the --link option is deprecated as stated in: https://docs.docker.com/engine/userguide/networking/default_network/dockerlinks/

作者: Ricardo Branco 发布者: 2017 年 12 月 27 日
32x32