bob
bob

Reputation: 95

How to make docker container accessable via ip address and not from the localhost. (localhost:port -> ip:port)

I want to make my docker container accessable via specific ip address ('10.1.0.5').
I understood that to make the container run a service that will be public and accessable via '10.1.0.5' ip address like a real service and not from the local host I need to run this command:

docker run --name relay -p 10.1.0.5:3000:3000 image

When I do run this command on the terminal I am getting this error:

docker: Error response from daemon: Ports are not available: exposing port TCP 10.1.0.5:3000 -> 0.0.0.0:0: listen tcp 10.1.0.5:3000: bind: can't assign requested address.
ERRO[0000] error waiting for container: context canceled

My host machine is MacOS if that somehow related to the issue.
Can someone tell me what I am missing? or at least tell me why this is happening.

Upvotes: 1

Views: 2198

Answers (1)

George
George

Reputation: 2502

First, You have to set up a Docker Network

docker network create --subnet=10.1.0.5/16 mynetwork

Then you can run the container with IP in the range of the subnet

docker run --net mynetwork --ip 10.1.0.5 -d -p 8080:80 hello-world

You can verify the address by inspecting docker container list with

docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' <NAME/ID>

replace <Name/ID> with your container name or ID.

Upvotes: 1

Related Questions