Reputation: 4419
I've two containers running on the default docker bridge network. The daemon assign them a "casual" ip on a specific range.
network_mode: bridge -> 172.17.0.X
How can I reach each other via some alias or name?
Upvotes: 1
Views: 3659
Reputation: 1419
I was running a Spring Boot jar in one Docker container and wanted to connect to an Oracle database running in another. In the Spring application.properties file I referenced the database url as
spring.datasource.url=jdbc:oracle:thin:@oracle:1521/xepdb1
In my Windows C:\Windows\System32\drivers\etc\hosts file I resolved 'oracle' as the localhost IP so I could run as Java application from Eclipse...
127.0.0.1 oracle
From the docker inspect command I was able to determine the IP address of the Oracle container as 172.17.0.2.
When I ran my Spring Boot container I ran it with the --add-host switch to resolve 'oracle' as the database container's IP address...
docker run --add-host=oracle:172.17.0.2 -p 9090:8080 -dit --name <name> <image>
In my Jenkins post build batch commands I capture the IP of the running database container and pass it to the app container on startup...
for /F %%x in ('docker inspect -f "{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}" oracle-xe') do set ip=%%x
docker run --add-host=oracle:%ip% -p 9090:8080 -dit --name demo demo
Upvotes: 0
Reputation: 5451
Let's take following containers running with base image Ubuntu:16.04:
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
2461f410228b ubuntu:16.04 "/bin/bash" 20 minutes ago Up 20 minutes competent_shockley
6da9d8196637 ubuntu:16.04 "/bin/bash" 21 minutes ago Up 21 minutes hungry_mccarthy
Default networks by Docker, to know more about the default networks refer here:
$ docker network ls
NETWORK ID NAME DRIVER SCOPE
f83800356529 bridge bridge local
8169b2246500 host host local
b61e4f39c0ec none null local
Let's find out the containers IP address using docker inspect
:
$ docker inspect 2461f410228b | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.18.0.3",
"IPAddress": "172.18.0.3",
$ docker inspect 6da9d8196637 | grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.18.0.2",
By default, containers launched in BRIDGE network will be able to access other containers launched in same network.
root@2461f410228b:/# ping 172.18.0.2
PING 172.18.0.2 (172.18.0.2) 56(84) bytes of data.
64 bytes from 172.18.0.2: icmp_seq=1 ttl=64 time=0.051 ms
As you are looking for reaching the other container using name/alias, add the target container information in the file /etc/hosts to resolve the DNS:
root@2461f410228b:/# cat /etc/hosts
127.0.0.1 localhost
::1 localhost ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
172.18.0.3 2461f410228b
172.18.0.2 docker2
Now try to reach target container using the hostname:
root@2461f410228b:/# ping docker2
PING docker2 (172.18.0.2) 56(84) bytes of data.
64 bytes from docker2 (172.18.0.2): icmp_seq=1 ttl=64 time=0.077 ms
I believe this may help to some extent.
Upvotes: 2