简体   繁体   中英

How do I refer to a docker container from another docker container, inside a network?

I'm attempting to create a service with Confluence and one with JIRA.

I've created a network as such:

docker network create --driver bridge atlassian_stack

And published one container each on this network, one exposing port 8080 and another 8090:

docker run -it --detach --publish 8080:8080 --net=atlassian_stack --name JIRA cptactionhank/atlassian-jira-software:latest

docker run -it --detach --publish 8090:8090 --net=atlassian_stack --name CONFLUENCE cptactionhank/atlassian-confluence:eap

I can access both locally on http://localhost:8080 (JIRA) and http://localhost:8090 (Confluence).

However, linking the applications together requires a hard coded IP within the Atlassian product. Confluence wants to know where JIRA resides, and vica verca. It does not appear they can see each other on the above IP addresses(which makes sense), as it complains about unresolved DNS.

The question is then: When the application requires a hardcoded DNS/IP, and they reside in the same network, how do I refer them to one another properly?

When multiple containers share a common network, then each container can resolve any of the other containers (on the same network) using the container's name .

So in your case, you should be able to run docker exec -it JIRA ping CONFLUENCE and also docker exec -it CONFLUENCE ping JIRA assuming ping is available within these containers, if not, you can exec / attach a shell into each and install the required network tools ( ping , telnet , nc etc) [ docker exec -it JIRA sh and then at # prompt, yum install telnet -y or apt instal -y depending on the particular distro of the base image.]

If you can use only the ip address (and not hostname / name), then you would need to fetch it at the time Confluence starts up, by running an nslookup jira or something similar. An example is below:

~ ᐅ docker network create -d bridge www
9e1ab7c25a58fdbdaa1ec46bbcf9c9b37bb6ddc776abe6b2fb47dbb0c100d750
~ ᐅ docker run -d --name w1 --network www busybox top
Unable to find image 'busybox:latest' locally
latest: Pulling from library/busybox
03b1be98f3f9: Pull complete
Digest: sha256:99ccecf3da28a93c063d5dddcdf69aeed44826d0db219aabc3d5178d47649dfa
Status: Downloaded newer image for busybox:latest
fb0c198b002559b6ccd7dd145a394592a7258b1a955012aae8c707a5bea70c86
~ ᐅ docker run -d --name w2 --network www busybox top
13ea6ceb82f7d80d34a8cc3b6ba79bdfcc1d3a5f1df1b5f59a20323db21a7190
~ ᐅ docker exec -it w1 nslookup w2
Server:    127.0.0.11
Address 1: 127.0.0.11

Name:      w2
Address 1: 172.19.0.3 w2.www

~ ᐅ docker exec -it w1 sh

/ # nslookup w2 | awk '/^Address.* w2.*/ { print $3 ; exit }'
172.19.0.3
/ # exit

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM