相关文章推荐
挂过科的烤面包  ·  Centos ...·  1 年前    · 
慷慨大方的斑马  ·  python - Error ...·  1 年前    · 
帅气的地瓜  ·  entitymanagerfactory ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

What's the easiest way to make containers started with docker-compose accessible from the host machine?

I have a simple docker-compose.yaml:

services:
  redis-cluster1:
    image: grokzen/redis-cluster:5.0.12
    ports:
      - "7000-7007:7000-7007"
  redis-cluster2:
    image: grokzen/redis-cluster:5.0.12
    ports:
      - "7010-7017:7000-7007"
  redis-cluster3:
    image: grokzen/redis-cluster:5.0.12
    ports:
      - "7020-7027:7000-7007"

and want to be able to hit those clusters from a non-docker process on the machine.

I've just read three articles and still can't get this to work.

Using 'docker container inspect' I'm able to see the IP of one of them:

"IPAddress": "172.19.0.3"

But it is not pingable.

Is this possible with the default bridge? This is on MacOS; can't use Linux-specific features.

Exactly as you've done it, with ports:. If you're making a call from outside a container on the same host you should be able to connect to localhost:7010 for the second container, for example. – David Maze Apr 5 at 22:34

By default when you create a new network of containers with docker-compose these are connected to a host network which allows each container to reach the external network (documentation). If you specify (like in your code) the port attribute for a container, this will tell to compose to expose the given port of the container to the network (Notice: this will expose such port not only to your localhost, but also to other machines connecting to your host ip). To sum up, with the proposed configuration you can reach for example redis-cluster1 on ports 7000-7007 from your localhost.

But it doesn't work. I think this actually has to do with my firm's whacky network configuration... pinging localhost doesn't work (all timeouts). So I guess it is a local issue. – Bender Rodriguez Apr 5 at 23:31 you cannot ping localhost from your same machine. You should connect to the mentioned ports services – Niccolò Borgioli Apr 6 at 18:48 What do you mean by "service" in the context of an external (non-dockerized) process running on the host? There is no such concept. We have endpoints which is a host or IP + port, of course. We need either a host name or IP to connect to. (Pinging localhost works fine in general, such as on my personal laptop, but not on my firm's network.) – Bender Rodriguez Apr 6 at 20:54 Some firewalls (like probably you firm one) block ICMP (ping) packets for security reasons. So, in some configurations you might be not able to ping a given host. When talking about docker, usually each container is used to manage a single service or micro-service. In this context with service I mean the ensemble of the application and the container that I am referring to. – Niccolò Borgioli Apr 7 at 14:22 I understood what you meant via "service," but my point is that the Docker service name isn't relevant outside of Docker. – Bender Rodriguez Apr 8 at 15:26

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.