Network: Publish custom IP and all ports

Hi all,

My host IP is 10.1.1.1 and I’d like my docker to be reachable at 10.1.1.2 for all ports (udp/tcp)

I have tried below but the syntax doesn’t seem correct.
#docker run -d -P 10.1.1.2 container_name

The syntax below works but I want to be able to map all ports to 10.1.1.2.

docker run -d -p 10.1.1.2:80:80 container_name

Many thanks,
ioan

docker run -d -p 10.1.1.2:80:80 container_name

Would bind container port 80 to port 80 on the host’s interface having the ip 10.1.1.2. If the host doesn’t have an interface using this ip, the command should fail.

Seems like what you want is a distinct ip on your lan. Please search the forum for macvlan and how it works.

1 Like

Hi meyay,

Thank you for your reply. So, exposing the IP on port 80 works, shall I understand that this command cannot be used for exposing the IP on all ports? I thought -P (capital) would expose all the ports.
docker run -d -p 10.1.1.2:80:80 container_name

Your command actualy publishes port 80 only to the interface with the ip 10.1.1.2. It does not introduce a new ip to your host or your network. Think about it as a filter: instead of binding the port on all existing interfaces, it restricts it to one specific interface.

The option -P will publish all container ports to random ports on all host interfaces. Usualy this is NOT what you want.

Understood, thank you. Then since I only need to publish a few ports, I’ll use -p a few times, I find it easier than using macvlan.

If you don’t use docker-compose so far, I would strongly suggest to start using it. It will make your life much easier and your deployments reproducable.

Usualy people that are trying to use docker containers like vm’s (they are not!) tend to use macvlan. Most usecases are achivable with publised ports on bridged (=single host docker network) or overlay (=multi host docker network) networks.