user3764893
user3764893

Reputation: 707

How to access docker container using localhost address

I am trying to access a docker container from another container using localhost address.

The compose file is pretty simple. Both containers ports are exposed. There are no problems when building.

In my host machine I can successfully execute curl http://localhost:8124/ and get a response.

But inside the django_container when trying the same command I get Connection refused error.

I tried adding them in the same network, still result didn't change.

Well if I try to execute with the internal ip of that container like curl 'http://172.27.0.2:8123/' I get the response.

Is this the default behavior? How can I reach clickhouse_container using localhost?

version: '3'

services:
  django:
    container_name: django_container
    build: ./django
    ports:
      - "8007:8000"
    links:
      - clickhouse:clickhouse
    volumes:
      - ./django:/usr/src/run
    command: bash /usr/src/run/run.sh

  clickhouse:
    container_name: clickhouse_container
    build: ./clickhouse
    ports:
      - "9001:9000"
      - "8124:8123"
      - "9010:9009"

Upvotes: 1

Views: 1351

Answers (3)

It depends of config.xml settings. If in config.xml <listen_host> 0.0.0.0</listen_host> you can use clickhouse-client -h your_ip --port 9001

Upvotes: 0

atline
atline

Reputation: 31584

As in @Billy Ferguson's answer, you can visit using localhost in host machine just because: you define a port mapping to route localhost:8124 to clickhouse:8123.

But when from other container(django), you can't. But if you insist, there is a ugly workaround: share host's network namespace with network_mode, but with this the django container will just share all network of host.

services:
  django:
    hostname: djano
    container_name: django
    build: ./django
    ports:
       - "8007:8000"
    links:
       - clickhouse:clickhouse
    volumes:
      - ./django:/usr/src/run
    command: bash /usr/src/run/run.sh
    network_mode: "host"

Upvotes: 1

Billy Ferguson
Billy Ferguson

Reputation: 1439

So with this line here - "8124:8123" you're mapping the port of clickhouse container to localhost 8124. Which allows you to access clickhouse from localhost at port 8124.

If you want to hit clickhouse container from within the dockerhost network you have to use the hostname for the container. This is what I like to do:

version: '3'

services:
  django:
    hostname: djano
    container_name: django
    build: ./django
    ports:
      - "8007:8000"
    links:
      - clickhouse:clickhouse
    volumes:
      - ./django:/usr/src/run
    command: bash /usr/src/run/run.sh

  clickhouse:
    hostname: clickhouse
    container_name: clickhouse
    build: ./clickhouse
    ports:
      - "9001:9000"
      - "8124:8123"
      - "9010:9009"

If you make the changes like I have made above you should be able to access clickhouse from within the django container like this curl http://clickhouse:8123.

Upvotes: 1

Related Questions