Establish conversation between hello-world apps in Docker containers


99 观看


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

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 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>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 ::1...
telnet: Unable to connect to remote host: Cannot assign requested address

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  *               LISTEN

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

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

回应 (2)


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 发布者: 27.12.2017 05:19


3838 作者的声誉


The problem is the address.

Each container is assigned, by default, 2 interfaces: eth0 and lo (the loopback interface with the 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:

作者: Ricardo Branco 发布者: 27.12.2017 06:31