0

I must be doing something totally wrong here, but I'm trying to test a simple setup of a docker container bound to a specific IP on the machine. Here's the use case:

On the host:

ifconfig eno1:1 192.168.3.222 netmask 255.255.255.0 up
docker network create --subnet=172.18.0.0/16 mynet123
docker run --net mynet123 --privileged -p 192.168.3.222:4444:4444 --name test -t -i ubuntu:xenial /bin/bash

In the bash session that is now running:

apt-get update
apt-get install netcat
netcat -l 4444

Now back on another shell on the host (or any other computer on the network):

netcat 192.168.3.222 4444

This gets a connection refused.

I must be doing something wrong, because even with I try it with the default network and -p 4444:4444 or with --net=host and using the host's original IP instead of .222 the same thing occurs.

This seems so simple based on everything I've read online, but for some reason I can't make other machines able to access the container via the host.

1 Answer 1

1

I am not sure why you are creating a network with different IP subnet.

I can achieve what you look for without creating a network. Here <target-ip> is the IP on the interface you are looking to bind to.

core@coreos6 ~ $ docker run -it  --privileged -p <target-ip>:4444:4444 alpine:edge /bin/sh
/ # nc -l 4444
hola mundo!

and then, from anywhere on the internet (my target IP is a public ip)

user@otherhost ~ # netcat <target-ip>:4444
hola mundo!

and I can see the "hola mundo!" on the container.

I think your issue is that the --net mynet123 changes the IP of the interface as it is seen by the container. Look at this:

core@coreos6 ~ $ docker run -it  --privileged -p <target-ip>:4444:4444 alpine:edge /bin/sh                                                                      
/ # ip addr                                                                                                                                                       
11: eth0@if12: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:11:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.17.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe11:2/64 scope link
       valid_lft forever preferred_lft forever
/ # ^D
core@coreos6 ~ $ docker network create --subnet=172.18.0.0/16 mynet123
30cab3dff92248d0fab5a1c70362fdc6da9399393def0571fe1b903d4fcab8cf
core@coreos6 ~ $ docker run --net mynet123 -it  --privileged -p <target-ip>:4444:4444 alpine:edge /bin/sh
/ # ip addr
14: eth0@if15: <BROADCAST,MULTICAST,UP,LOWER_UP,M-DOWN> mtu 1500 qdisc noqueue state UP
    link/ether 02:42:ac:12:00:02 brd ff:ff:ff:ff:ff:ff
    inet 172.18.0.2/16 scope global eth0
       valid_lft forever preferred_lft forever
    inet6 fe80::42:acff:fe12:2/64 scope link
       valid_lft forever preferred_lft forever

and this probably messes up the iptables handling that docker does to enable the port forwarding.

1
  • 1
    In my official use case I needed to specify IPs, so I was using a separate network, however your answer led me down the right roads as removing the separate network fixed the problem (after realizing conflicting netcat versions were an issue). So, all in all it's working now. Thanks!
    – Fmstrat
    Jun 27, 2017 at 22:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .