0

So I'm running a private registry in a remote server e.g. 51.15.235.12:5000, and locally pushing docker images to that registry. I'm trying to start a container using the host IP with network_mode host, but the ip of the container is something like 172.17.0.1.

This is the docker-compose:

version: '2'
services:
rsi:
    container_name: rsi
    image: localhost:5000/rsi:1.0
    restart: on-failure:10
    depends_on:
        - geoserver
        # - rsi-db
    network_mode: host

# Geoserver
geoserver:
    container_name: geoserver
    restart: always
    image: localhost:5000/geoserver:1.0
    network_mode: host
    logging:
        driver: "json-file"
        options:
            max-size: "10m"
            max-file: "10"
    environment:
        - GEOSERVER_LOG_LOCATION=/opt/geoserver/data_dir/logs/geoserver.log

The localhost-IP here is 51.15.235.12, but when I enter the rsi-container I'm getting this when I run ifconfig:

docker0   Link encap:Ethernet  HWaddr 02:42:E5:09:B4:E9  
      inet addr:172.17.0.1  Bcast:172.17.255.255  Mask:255.255.0.0

Why isn't the docker0 interface IP set to the local host IP i.e. 51.15.235.12?

Jesper
  • 2,044
  • 3
  • 21
  • 50

1 Answers1

2

When you use --net host you share the host's entire network setup and all of its interfaces. The container doesn't have its own IP address. You're in the exact same network environment as if, say, you ran Apache on the host: the container/process doesn't have its own IP address, it's just something listening on a port on the host.

The Docker daemon creates a docker0 interface as part of its internal networking setup. Since you're using the host network stack, you'll see it in the list of interfaces. But that's not "the container's IP". localhost (always 127.0.0.1, which is magic) will point at the host system.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • Okay, thank you for explaining this. Right now I've setup a postgresql to which I'm trying to connect to within the container, and for some reason i'm unable to connect to the db using localhost. So postgres://user@localhost isn't working, but postgres://user@51.15.235.12 is. I thought the problem was with the docker0 interface – Jesper Aug 04 '18 at 11:05
  • Right, you can't connect to the database as `localhost` from within a container. There are many many SO questions describing this already; see for example [this question](https://stackoverflow.com/questions/43856554/connect-to-a-postgresql-database-on-a-docker-container) about connecting from a container to a PostgreSQL instance in another container, and [this question](https://stackoverflow.com/questions/50278632/what-does-localhost-means-inside-a-docker-container) about what "localhost" actually means. – David Maze Aug 04 '18 at 15:31