I am trying to bind a desired IP and port to a docker container and expose it to the network so that it can be accessed through the other machines in the network irrespective of the machine it is running on. I am very new to docker. I am able to achieve the port binding to the localhost but in that way it becomes machine dependent i.e we need to access the container through that machine's IP. My tests are running in two different networks so I want to make it machine independent. SO what I am trying to do is : To bind the container to any random desired IP(Not sure whether this can be achieved as the IP should be always unique in the network). Please let me know whether this is achievable and if Yes how. Thanks a lot in advance.
Asked
Active
Viewed 664 times
-1
-
How does your `docker run`statement look like? – Thomas Stets Jul 24 '17 at 09:38
-
docker run -p
:: – Prabhat Jul 24 '17 at 10:07 -
Actually I want to achieve this by Spotiy docker ciient. but using command line also I am not able to do. Looks like something needs to be done with networking. – Prabhat Jul 24 '17 at 10:10
-
docker run -d -p 192.168.99.100::15672 761ae3adcd1a 5497f9102c85b01b39292478552a5821c955da84073d724330bf89bdba875858 docker: Error response from daemon: driver failed programming external connectivity on endpoint hardcore_jang (21296461920cea98e0808407a3755542687f1be53591b7231b918071ac6b7d97): Error starting userland proxy: listen tcp 192.168.99.100:32777: bind: cannot assign requested address. – Prabhat Jul 24 '17 at 10:10
-
@Prabhat Edit your question instead of filling the comment section with instructions. – Grimmy Jul 24 '17 at 10:28
2 Answers
0
You can try the below 2 ways for the solution.
Create a network and run your container on that network
docker network create --subnet=193.168.3.0/24 network_name
docker run --net network_name -p 193.168.3.18:15672 image
Create a yml file "docker-compose.yml" and add the below in it
version: "2" services: containername: image: your-image-name:version restart: always expose: - "8080" networks: overlay: ipv4_address: 193.168.3.18 ports: - 8080:8080 networks: overlay: driver: bridge ipam: config: - subnet: 193.168.3.0/24
Save the file and run the below command
docker-compose -f docker-compose.yml up -d
You can use version 3 if you want swarm facility. Once the above command is run, a container with name "dir-name-of-yml_containername_1" is created. Here you are creating a network with subnet "193.168.3.0/24" and using IP "193.168.3.18" to create the container.

Altafhusen
- 1
- 2
-
Thank you so much.. I tried with option 1. docker network create --subnet=193.168.3.0/24 rabbit_network 90d730bc864a3871fc0353d6eaf6c37ac738b6b1a8174e67cdb037b3a21f8862 docker run --net rabbit_network -p 193.168.3.18:15672 rabbitmq-st docker: Invalid hostPort: 193.168.3.18. See 'docker run --help'. – Prabhat Jul 26 '17 at 06:07
-
Then I tried this way : docker run --net rabbit_network -p 193.168.3.18::15672 rabbitmq-st docker: Error response from daemon: driver failed programming external connectivity on endpoint goofy_shannon (242d4783980fdc7dd06630474c56de710d4009d4c30964cb683987e0e698945d): Error starting userland proxy: listen tcp 193.168.3.18:32777: bind: cannot assign requested address. ERRO[0002] error waiting for container: context canceled – Prabhat Jul 26 '17 at 06:11
-
Did you try solution 2? You need to delete network created in solution 1 before trying solution 2. `docker network remove rabbit_network` – Altafhusen Jul 26 '17 at 10:23