Ping from specific host ip

I have seetup a docker host with a couple of services. One of them ar phpipam.
When I ping from phpipam i want it to come from a specific ip adress.

our host has one nic with several adresses: 192.168.1.4, 192.168.1.5, 192.168.1.6 etcetera
Network is 192.168.1.0/24 and default gw 192.168.1.254

the part in my docker compose for the web-service is like this:
I have no configuration in the compose for network except the the one in ‘ports;’

version: '3'

services:
  web:
    image: phpipam/phpipam-www:v1.5.2
    ports:
      - "192.168.1.7:80:80"
    environment:
      - IPAM_DATABASE_HOST=mariadb
      - IPAM_DATABASE_USER=root
      - IPAM_DATABASE_PASS=$MYSQL_ROOT_PASSWORD
      - IPAM_DATABASE_WEBHOST=%
    restart: unless-stopped
    volumes:
      - /mnt/dockerdata/phpipam/logo:/phpipam/css/images/logo
      - /mnt/dockerdata/phpipam/ca:/usr/local/share/ca-certificates:ro
    depends_on:
      - mariadb

I can reach the application from http://192.168.1.7 but when I do a ping from the application it comes from adress 192.168.1.4. How can I make it to come from 192.168.1.7?

It is possible to set the IP to use for container outbound traffic, search for com.docker.network.host_ipv4

Example docker-compose.yml snippet:

networks:
  default:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 192.168.0.0/24
    driver_opts:
      com.docker.network.host_ipv4: 1.2.3.4
2 Likes

I didn’t even know about this. I tried now and worked. I only added the label without the static subnet. If you made it a topic in Tips & HowTos that would quickly become a frequently referred topic. I couldn’t find it in the documentation even though it seems it has been there for years:

I added this to the end of my compose:

networks:
  default:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 192.168.1.0/24
    driver_opts:
      com.docker.network.host_ipv4: 192.168.1.7

I then exec into phpipam-web and run a ping command an can see in our firewall that the ping still comes from the hosts ip-adress.

What is your Docker version?

update:

I guess the problem is that you have a single interface with multiple ip addresses. So the traffic will go to the interface which has the ip address set in the label but it will still go through that single interface so the remote will see the default ip. You could try to create separate interfaces for the ip addresses.

I also edited your post as your previous post before. Please, format your posts according to the following guide: How to format your forum posts
In short: please, use </> button to share codes, terminal outputs, error messages or anything that can contain special characters which would be interpreted by the MarkDown filter. Use the preview feature to make sure your text is formatted as you would expect it and check your post after you have sent it so you can still fix it.

My Docker version is 24.0.5
OS is Ubuntu 22.04
I installed it two weeks ago and applied all patches.

How do I setup another interface on the same subnet?
the interface I have is ens160. I added another interface ens192.
Here are my netplan config before adding the new interface:

network:
  ethernets:
    ens160:
      addresses:
      - 192.168.1.4/24
      - 192.168.1.5/24
      - 192.168.1.6/24
      - 192.168.1.7/24
      - 192.168.1.8/24
      - 192.168.1.9/24
      nameservers:
        addresses:
        - 192.168.2.10
        - 192.168.2.11
        search:
        - mydomain.local
      routes:
      - to: default
        via: 192.168.1.254
  version: 2

Thank you for editing the code in my previous reply and thank you for your time helping me with this case.

I tested to deploy another container with some network tools:

My docker compose is like this:

version: '3.3'
services:
  nettool:
    image: praqma/network-multitool
networks:
  default:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 192.168.1.0/24
    driver_opts:
      com.docker.network.host_ipv4: 192.168.1.7

When I exec into it and run a ping I can see in our firewall that the ping comes from 192.168.1.7 and the ping is allowed by the firewall but I do not get any responce on the ping.

Another thing i noticed is that the container gets ip-adress 192.168.1.2. That address is used by another server in our network.

When i put the

networks:
  default:
....

section in the compose for phpipam and starts it ant exec into phpipam web and do a ping it comes from ip 192.168.1.4.

Why does it come from 192168.1.7 when i have it in one compose and not in another?

phpipam is working for me now.

I skipped the subnet part of networks default. Now it looks like this:

networks:
  default:
    driver: bridge
    ipam:
      driver: default
    driver_opts:
      com.docker.network.host_ipv4: 192.168.1.7

(I only have one NIC in the docker host. It wasn’t needed to add another NIC ass suggested).

I tested to skip the ipam-part as well and it still works. Now it looks like this:

networks:
  default:
    driver: bridge
    driver_opts:
      com.docker.network.host_ipv4: 192.168.1.7

Maybe I could skip the ‘driver: bridge’ as well.

With this configuration you assigned the lan’s subnet to the bridge’s IP Address Management. It is not designed to be used like this. It must be a different subnet than the lan’s subnet.

If you look at the example of @bluepuma77 , he specified a subnet that is not in the lan’s subnet.

1 Like

@thonyj I’m glad to see that despite my misleading answer everything works now.
Thank you @meyay, I should have noticed that too.