How do i bind a container to a specific NIC?

I am running a pihole (DNS and DHCP) container in Docker for Mac. My Mac has two network interfaces, Wifi and Ethernet over USB-C.

I want my pihole DNS and DHCP to ONLY respond to requests coming from the USB-C Ethernet interface. I don’t want to give out IP addresses to the clients on my Wifi network.

What i have tried so far:

  • create a docker network in the same range as the fixed IP address of my Ethernet adapter.

  • attach the pihole container to this network

  • give the pihole container a fixed IP address in the same range as the Ethernet adapter

Currently i can access the pihole interface over 127.0.0.1 (docker_bridge), but i cannot access the pihole web interface over 192.168.2.2 (docker_laptop).

Any suggestions on how to set this up?

This is my docker-compose file:

version: '3'

services:

pihole:

container_name: pihole

image: pihole/pihole:latest

environment:

- COMPOSE_PROJECT_NAME=pihole

- ServerIP=192.168.2.2

networks:

laptop:

ipv4_address:

  • 192.168.2.2

ports:

- "53:53/tcp"

- "53:53/udp"

- "80:80"

- "443:443"

dns:

-``192.168.2.2

-
1.1.1.1

cap_add:

- NET_ADMIN

volumes:

- ./pihole:/etc/pihole

- ./pihole/pihole.log:/var/log/pihole.log

- ./pihole/hosts:/etc/hosts

- ./pihole/dnsmasq.d:/etc/dnsmasq.d

networks:

laptop:

driver: bridge

ipam:

driver: default

config:

- subnet:
192.168.2.0/24

Docker container inspect gives me:

"Networks": {

"docker_default": {

"IPAMConfig": null,

"Links": null,

"Aliases": [

"03e47ebb5251"

],

"NetworkID": "c1310b3b912ba48f3e9ebed14eac81ffb6fcb4247079fa1699f5d61202026b86",

"EndpointID": "8c6ad565ba214e380b58c870f57b697bc721e3c1770c1256984e8504a6f90bc5",

"Gateway": "
172.20.0.1

"IPAddress": "
172.20.0.2

"IPPrefixLen": 16,

"IPv6Gateway": "",

"GlobalIPv6Address": "",

"GlobalIPv6PrefixLen": 0,

"MacAddress": "02:42:ac:14:00:02",

"DriverOpts": null

},

"docker_laptop": {

"IPAMConfig": {

"IPv4Address": "
192.168.2.2

},

"Links": null,

"Aliases": [

"03e47ebb5251",

"pihole"

],

"NetworkID": "831bfe29b8fefe26d632ff5137f98c479bf73e3e618d2534977fbc9b081ab4ff",

"EndpointID": "1f6252c36205020f5195b500cd7c2b4fe3652127ab3e9ebb2be3d3ce81e30e27",

`“Gateway”: “192.168.2.1”

"IPAddress": "
192.168.2.2

"IPPrefixLen": 24,

"IPv6Gateway": "",

"GlobalIPv6Address": "",

"GlobalIPv6PrefixLen": 0,

"MacAddress": "02:42:c0:a8:02:02",

"DriverOpts": null

}

`}

I believe all you have to do is tell docker to mount the container to a specific IP when you are specifying the IP addresses to expose. For example in your docker-compose, if your NIC IP that you want to use is 192.168.111 then do this:
ports:
- "192.168.111:53:53/tcp"
- "192.168.111:53:53/udp"
- "192.168.111:80:80"
- "192.168.111:443:443"