1

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"
3
  • 1
    There are some similar questions on SO, can you show how your question is different or how other answers to similar questions did not work for you? stackoverflow.com/search?q=%5Bdocker%5D+localhost&mixed=0 Commented May 15, 2019 at 14:45
  • Similar question / duplicate stackoverflow.com/questions/48291342/… Commented May 15, 2019 at 16:34
  • Why do you want to access via localhost inside another container? You should just use the name of the service instead of localhost clickhouse: 8123 Commented May 15, 2019 at 20:59

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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"

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.