Hi,

I’ve recently started studying docker and docker compose and as my first task was able to build a container fairly quickly. But I just wanted to see if it’s possible to have a container’s IP different to that of your host.

For example: My host Ip is 192.168.0.110, I want my pihole to be of a different IP 192.168.0.150 for example

I tried creating a macvlan and setting the ServerIP variable to no success and was wondering if anyone had already done this and to see if it’s possible

I’m running docker and docker compose on a raspbian VM

My code is as follows:

YAML
version: “3”

docker-pi-hole/README.md at master · pi-hole/docker-pi-hole · GitHub

services:
pihole:
container_name: pihole
image: pihole/pihole:latest
hostname: pi-hole
#ip: “192.168.0.201”

For DHCP it is recommended to remove these ports and instead add: network_mode: “host”

ports:

  • “53:53/tcp”
  • “53:53/udp”
  • “67:67/udp”
  • “80:80/tcp”
  • “443:443/tcp”
    environment:
    ServerIP: “192.168.0.130”
    TZ: ‘America/Chicago’
    WEBPASSWORD: ‘Password123’

Volumes store your data between container upgrades

volumes:

  • ‘./etc-pihole/:/etc/pihole/’
  • ‘./etc-dnsmasq.d/:/etc/dnsmasq.d/’

run touch ./var-log/pihole.log first unless you like errors

- ‘./var-log/pihole.log:/var/log/pihole.log’

dns:

  • 127.0.0.1
  • 1.1.1.1
    #- 192.168.1.107

Recommended but not required (DHCP needs NET_ADMIN)

GitHub - pi-hole/docker-pi-hole: Pi-hole in a docker container

cap_add:

  • NET_ADMIN
    restart: unless-stopped

networks:

- dockervlan

networks:

dockervlan:

#This interface should be defined as using null driver. Do not remove it.

driver: null

driver_opts:

parent: eth0

ipam:

config:

- subnet: “192.168.0.0/24”

# ip_range: “192.168.0.128/27”

# gateway: “192.168.0.1”

dockervlan:

#This is the interface which is used for containers networking

driver: macvlan

driver_opts:

parent: eth0

ipam:

config:

- subnet: “192.168.0.0/24”

ip_range: “192.168.0.128/27”

gateway: “192.168.0.1”

YAML
version: "3"
# https://github.com/pi-hole/docker-pi-hole/blob/master/README.md
services:
    pihole:
        container_name: pihole
        image: pihole/pihole:latest
        hostname: pi-hole
        #ip: "192.168.0.201"
        # For DHCP it is recommended to remove these ports and instead add: network_mode: "host"
        ports:
            - "53:53/tcp"
            - "53:53/udp"
            - "67:67/udp"
            - "80:80/tcp"
            - "443:443/tcp"
        environment:
            ServerIP: "192.168.0.130"
            TZ: 'America/Chicago'
            WEBPASSWORD: 'Password123'
            # Volumes store your data between container upgrades
        volumes:
            - './etc-pihole/:/etc/pihole/'
            - './etc-dnsmasq.d/:/etc/dnsmasq.d/'
            # run `touch ./var-log/pihole.log` first unless you like errors
            # - './var-log/pihole.log:/var/log/pihole.log'
        dns:
            - 127.0.0.1
            - 1.1.1.1
            #- 192.168.1.107  
            # Recommended but not required (DHCP needs NET_ADMIN)
            #   https://github.com/pi-hole/docker-pi-hole#note-on-capabilities
        cap_add:
            - NET_ADMIN
        restart: unless-stopped
        # networks:
            # - dockervlan
# networks:
    # dockervlan:
    # #This interface should be defined as using null driver. Do not remove it.
        # driver: null
        # driver_opts:
            # parent: eth0
        # ipam:
            # config:
                # - subnet: "192.168.0.0/24"
# #                  ip_range: "192.168.0.128/27"
# #                  gateway: "192.168.0.1"
    # dockervlan:
    # #This is the interface which is used for containers networking
        # driver: macvlan
        # driver_opts:
            # parent: eth0
        # ipam:
            # config:
                # - subnet: "192.168.0.0/24"
#                  ip_range: "192.168.0.128/27"
#                  gateway: "192.168.0.1"

I even tried to create a new network interface

BASH

sudo ip link add dockervlan link eth0 type macvlan mode bridge
sudo ip addr add 192.168.0.249/32 dev dockervlan
sudo ip link set dockervlan up
sudo ip route add 192.168.0.128/27 dev dockervlan

But could not get the Ip to change, so I commented the network isolation stuff and run with the IP same as the host

Thanks in advance for your help.

2 Spice ups

TL;DR - yes, the container IP can be different to the host.

And just a small tidyness plea - you included the YAML twice - I appreciate putting it inside a code fence, but can you remove the first YAML fragment?

I removed thank you

192168101.win 100001.dev routerlogin.win