Home > Enterprise >  Is it possible to bind on another IP than 127.0.0.1 on MacOS?
Is it possible to bind on another IP than 127.0.0.1 on MacOS?

Time:11-14

On MacOS 12, using Docker 20.10, I'm not able to start a container on another IP:

% docker run -p 127.123.2.13:80:80 -d nginx
a9216ae29940f7357b9b4826ecddf041f1805c9ee48ba1336361277fc0dcb524
docker: Error response from daemon: Ports are not available: listen tcp 127.0.17.1:80: bind: can't assign requested address.

Is there any other way?

CodePudding user response:

In order to bind to the ip, other than 0.0.0.0, you need to have an interface in your system with the desired ip. For example, watch docker failing to bind to a non-existent ip of 127.0.0.2:

docker run -p 127.0.0.2:80:80 -d nginx
cc79b1b60c9f5e245b326bbfcc17d4a1f1abe6fad6fd12f9677b66bbee972a12
docker: Error response from daemon: Ports are not available: listen tcp 127.0.0.2:80: bind: can't assign requested address.

Now I create an alias for my existing interface lo0:

sudo ifconfig lo0 alias 127.0.0.2 netmask 0xff000000

and try again:

docker run -p 127.0.0.2:80:80 -d nginx
05223ecb6ae99a25b7423f014b9b95422c621717705ce1c255bea04072c45263
docker ps -a
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS         PORTS                  NAMES
cc79b1b60c9f   nginx     "/docker-entrypoint.…"   2 minutes ago    Created                               hardcore_haslett
05223ecb6ae9   nginx     "/docker-entrypoint.…"   2 minutes ago    Up 2 minutes   127.0.0.2:80->80/tcp   pensive_bardeen
  • Related