Get IP addresses of interfaces in Docker containers

Docker containers can be binded to multiple interfaces

Each interfaces comes with a dynamic IP address.

Think about following scenario:

  • In your Docker compose file you specified your DB and Web service.

  • You bind DB to internal network interface.

  • You bind your web service to both your internal network interface and external interface.

  • You want your Web service only bind to external interface.

  • How do you do that?

Sample Docker compose file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
version: '3.1'
services:
web:
image: web-service:latest
container_name: web-service
restart: unless-stopped
depends_on:
- db
networks:
- external
     - internal
volumes:
- /data:/data:Z
environment:
database_client: mysql
database_connection_host: db
database_connection_port: 3306
database_connection_user: web
database_connection_password: web
database_connection_database: web
server_port: 443
server_host: 0.0.0.0
db:
image: mariadb:bionic
container_name: mariaDB
restart: unless-stopped
environment:
MYSQL_RANDOM_ROOT_PASSWORD: "true"
MYSQL_USER: web
MYSQL_PASSWORD: web
MYSQL_DATABASE: web
networks:
- internal
volumes:
- /mariaDB:/var/lib/mysql:Z
networks:
external:
   external: true
 internal:
driver: bridge

What should be put in server_host if I don’t like 0.0.0.0?

Would be interesting to investigate.

Answer:

One may simply bind the web server to “external” network by looking up the available links using something similar to ip addr.
Meanwhile it is possible to assign static IP to a container.