Hi,<\/p>\n
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.<\/p>\n
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<\/p>\n
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<\/p>\n
I’m running docker and docker compose on a raspbian VM<\/p>\n
My code is as follows:<\/p>\n
YAML
\nversion: “3”<\/p>\n
services: ports:<\/p>\n volumes:<\/p>\n dns:<\/p>\n cap_add:<\/p>\n I even tried to create a new network interface<\/p>\n BASH<\/p>\n But could not get the Ip to change, so I commented the network isolation stuff and run with the IP same as the host<\/p>\n Thanks in advance for your help.<\/p>","upvoteCount":2,"answerCount":3,"datePublished":"2021-10-17T01:10:29.000Z","author":{"@type":"Person","name":"spiceuser-ssdfi","url":"https://community.spiceworks.com/u/spiceuser-ssdfi"},"suggestedAnswer":[{"@type":"Answer","text":" Hi,<\/p>\n 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.<\/p>\n 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<\/p>\n 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<\/p>\n I’m running docker and docker compose on a raspbian VM<\/p>\n My code is as follows:<\/p>\n YAML services: ports:<\/p>\n volumes:<\/p>\n dns:<\/p>\n cap_add:<\/p>\n I even tried to create a new network interface<\/p>\n BASH<\/p>\n But could not get the Ip to change, so I commented the network isolation stuff and run with the IP same as the host<\/p>\n Thanks in advance for your help.<\/p>","upvoteCount":2,"datePublished":"2021-10-17T01:10:29.000Z","url":"https://community.spiceworks.com/t/can-the-container-ip-be-different-from-the-host/814233/1","author":{"@type":"Person","name":"spiceuser-ssdfi","url":"https://community.spiceworks.com/u/spiceuser-ssdfi"}},{"@type":"Answer","text":" TL;DR - yes, the container IP can be different to the host.<\/p>\n 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?<\/p>","upvoteCount":0,"datePublished":"2021-10-17T08:41:52.000Z","url":"https://community.spiceworks.com/t/can-the-container-ip-be-different-from-the-host/814233/2","author":{"@type":"Person","name":"DoctorDNS","url":"https://community.spiceworks.com/u/DoctorDNS"}},{"@type":"Answer","text":" I removed thank you<\/p>\n 192168101.win<\/a> 100001.dev<\/a> routerlogin.win<\/a><\/p>","upvoteCount":0,"datePublished":"2021-12-07T22:06:02.000Z","url":"https://community.spiceworks.com/t/can-the-container-ip-be-different-from-the-host/814233/3","author":{"@type":"Person","name":"spiceuser-ssdfi","url":"https://community.spiceworks.com/u/spiceuser-ssdfi"}}]}}
\npihole:
\ncontainer_name: pihole
\nimage: pihole/pihole:latest
\nhostname: pi-hole
\n#ip:<\/span> “192.168.0.201”<\/p>\n<\/a>For DHCP it is recommended to remove these ports and instead add: network_mode: “host”<\/h1>\n
\n
\nenvironment:
\nServerIP: “192.168.0.130”
\nTZ: ‘America/Chicago’
\nWEBPASSWORD: ‘Password123’<\/li>\n<\/ul>\n<\/a>Volumes store your data between container upgrades<\/h1>\n
\n
<\/a>run
touch ./var-log/pihole.log<\/code> first unless you like errors<\/h1>\n
<\/a>- ‘./var-log/pihole.log:/var/log/pihole.log’<\/h1>\n
\n
\n#-<\/span> 192.168.1.107<\/li>\n<\/ul>\n<\/a>Recommended but not required (DHCP needs NET_ADMIN)<\/h1>\n
<\/a>GitHub - pi-hole/docker-pi-hole: Pi-hole in a docker container<\/a><\/h1>\n
\n
\nrestart: unless-stopped<\/li>\n<\/ul>\n<\/a>networks:<\/h1>\n
<\/a>- dockervlan<\/h1>\n
<\/a>networks:<\/h1>\n
<\/a>dockervlan:<\/h1>\n
<\/a>#This<\/span> interface should be defined as using null driver. Do not remove it.<\/h1>\n
<\/a>driver: null<\/h1>\n
<\/a>driver_opts:<\/h1>\n
<\/a>parent: eth0<\/h1>\n
<\/a>ipam:<\/h1>\n
<\/a>config:<\/h1>\n
<\/a>- subnet: “192.168.0.0/24”<\/h1>\n
<\/a># ip_range: “192.168.0.128/27”<\/h1>\n
<\/a># gateway: “192.168.0.1”<\/h1>\n
<\/a>dockervlan:<\/h1>\n
<\/a>#This<\/span> is the interface which is used for containers networking<\/h1>\n
<\/a>driver: macvlan<\/h1>\n
<\/a>driver_opts:<\/h1>\n
<\/a>parent: eth0<\/h1>\n
<\/a>ipam:<\/h1>\n
<\/a>config:<\/h1>\n
<\/a>- subnet: “192.168.0.0/24”<\/h1>\n
<\/a>ip_range: “192.168.0.128/27”<\/h1>\n
<\/a>gateway: “192.168.0.1”<\/h1>\n
YAML\nversion: \"3\"\n# https://github.com/pi-hole/docker-pi-hole/blob/master/README.md\nservices:\n pihole:\n container_name: pihole\n image: pihole/pihole:latest\n hostname: pi-hole\n #ip: \"192.168.0.201\"\n # For DHCP it is recommended to remove these ports and instead add: network_mode: \"host\"\n ports:\n - \"53:53/tcp\"\n - \"53:53/udp\"\n - \"67:67/udp\"\n - \"80:80/tcp\"\n - \"443:443/tcp\"\n environment:\n ServerIP: \"192.168.0.130\"\n TZ: 'America/Chicago'\n WEBPASSWORD: 'Password123'\n # Volumes store your data between container upgrades\n volumes:\n - './etc-pihole/:/etc/pihole/'\n - './etc-dnsmasq.d/:/etc/dnsmasq.d/'\n # run `touch ./var-log/pihole.log` first unless you like errors\n # - './var-log/pihole.log:/var/log/pihole.log'\n dns:\n - 127.0.0.1\n - 1.1.1.1\n #- 192.168.1.107 \n # Recommended but not required (DHCP needs NET_ADMIN)\n # https://github.com/pi-hole/docker-pi-hole#note-on-capabilities\n cap_add:\n - NET_ADMIN\n restart: unless-stopped\n # networks:\n # - dockervlan\n# networks:\n # dockervlan:\n # #This interface should be defined as using null driver. Do not remove it.\n # driver: null\n # driver_opts:\n # parent: eth0\n # ipam:\n # config:\n # - subnet: \"192.168.0.0/24\"\n# # ip_range: \"192.168.0.128/27\"\n# # gateway: \"192.168.0.1\"\n # dockervlan:\n # #This is the interface which is used for containers networking\n # driver: macvlan\n # driver_opts:\n # parent: eth0\n # ipam:\n # config:\n # - subnet: \"192.168.0.0/24\"\n# ip_range: \"192.168.0.128/27\"\n# gateway: \"192.168.0.1\"\n<\/code><\/pre>\n
sudo ip link add dockervlan link eth0 type macvlan mode bridge\nsudo ip addr add 192.168.0.249/32 dev dockervlan\nsudo ip link set dockervlan up\nsudo ip route add 192.168.0.128/27 dev dockervlan\n<\/code><\/pre>\n
\nversion: “3”<\/p>\n<\/a>docker-pi-hole/README.md at master · pi-hole/docker-pi-hole · GitHub<\/a><\/h1>\n
\npihole:
\ncontainer_name: pihole
\nimage: pihole/pihole:latest
\nhostname: pi-hole
\n#ip:<\/span> “192.168.0.201”<\/p>\n<\/a>For DHCP it is recommended to remove these ports and instead add: network_mode: “host”<\/h1>\n
\n
\nenvironment:
\nServerIP: “192.168.0.130”
\nTZ: ‘America/Chicago’
\nWEBPASSWORD: ‘Password123’<\/li>\n<\/ul>\n<\/a>Volumes store your data between container upgrades<\/h1>\n
\n
<\/a>run
touch ./var-log/pihole.log<\/code> first unless you like errors<\/h1>\n
<\/a>- ‘./var-log/pihole.log:/var/log/pihole.log’<\/h1>\n
\n
\n#-<\/span> 192.168.1.107<\/li>\n<\/ul>\n<\/a>Recommended but not required (DHCP needs NET_ADMIN)<\/h1>\n
<\/a>GitHub - pi-hole/docker-pi-hole: Pi-hole in a docker container<\/a><\/h1>\n
\n
\nrestart: unless-stopped<\/li>\n<\/ul>\n<\/a>networks:<\/h1>\n
<\/a>- dockervlan<\/h1>\n
<\/a>networks:<\/h1>\n
<\/a>dockervlan:<\/h1>\n
<\/a>#This<\/span> interface should be defined as using null driver. Do not remove it.<\/h1>\n
<\/a>driver: null<\/h1>\n
<\/a>driver_opts:<\/h1>\n
<\/a>parent: eth0<\/h1>\n
<\/a>ipam:<\/h1>\n
<\/a>config:<\/h1>\n
<\/a>- subnet: “192.168.0.0/24”<\/h1>\n
<\/a># ip_range: “192.168.0.128/27”<\/h1>\n
<\/a># gateway: “192.168.0.1”<\/h1>\n
<\/a>dockervlan:<\/h1>\n
<\/a>#This<\/span> is the interface which is used for containers networking<\/h1>\n
<\/a>driver: macvlan<\/h1>\n
<\/a>driver_opts:<\/h1>\n
<\/a>parent: eth0<\/h1>\n
<\/a>ipam:<\/h1>\n
<\/a>config:<\/h1>\n
<\/a>- subnet: “192.168.0.0/24”<\/h1>\n
<\/a>ip_range: “192.168.0.128/27”<\/h1>\n
<\/a>gateway: “192.168.0.1”<\/h1>\n
YAML\nversion: \"3\"\n# https://github.com/pi-hole/docker-pi-hole/blob/master/README.md\nservices:\n pihole:\n container_name: pihole\n image: pihole/pihole:latest\n hostname: pi-hole\n #ip: \"192.168.0.201\"\n # For DHCP it is recommended to remove these ports and instead add: network_mode: \"host\"\n ports:\n - \"53:53/tcp\"\n - \"53:53/udp\"\n - \"67:67/udp\"\n - \"80:80/tcp\"\n - \"443:443/tcp\"\n environment:\n ServerIP: \"192.168.0.130\"\n TZ: 'America/Chicago'\n WEBPASSWORD: 'Password123'\n # Volumes store your data between container upgrades\n volumes:\n - './etc-pihole/:/etc/pihole/'\n - './etc-dnsmasq.d/:/etc/dnsmasq.d/'\n # run `touch ./var-log/pihole.log` first unless you like errors\n # - './var-log/pihole.log:/var/log/pihole.log'\n dns:\n - 127.0.0.1\n - 1.1.1.1\n #- 192.168.1.107 \n # Recommended but not required (DHCP needs NET_ADMIN)\n # https://github.com/pi-hole/docker-pi-hole#note-on-capabilities\n cap_add:\n - NET_ADMIN\n restart: unless-stopped\n # networks:\n # - dockervlan\n# networks:\n # dockervlan:\n # #This interface should be defined as using null driver. Do not remove it.\n # driver: null\n # driver_opts:\n # parent: eth0\n # ipam:\n # config:\n # - subnet: \"192.168.0.0/24\"\n# # ip_range: \"192.168.0.128/27\"\n# # gateway: \"192.168.0.1\"\n # dockervlan:\n # #This is the interface which is used for containers networking\n # driver: macvlan\n # driver_opts:\n # parent: eth0\n # ipam:\n # config:\n # - subnet: \"192.168.0.0/24\"\n# ip_range: \"192.168.0.128/27\"\n# gateway: \"192.168.0.1\"\n<\/code><\/pre>\n
sudo ip link add dockervlan link eth0 type macvlan mode bridge\nsudo ip addr add 192.168.0.249/32 dev dockervlan\nsudo ip link set dockervlan up\nsudo ip route add 192.168.0.128/27 dev dockervlan\n<\/code><\/pre>\n