Musmus
Musmus

Reputation: 229

communicate with a service inside a docker from the host without using it's IP

I have a process running on a host that needs to communicate with a docker and I want it to be done by some parameter that can't change (like docker name or host name) unlike IP (prefer not to make the IP of the docker static or install external dockers for this).

I'm aware that dockers can resolve addressees by name in a private network and that's what I want but not between dockers but between process running on the host and docker.

couldn't find a solution, can it be done ?

Edit:

I'm not allowed to use host network and open additional ports on the host for security reasons.

Upvotes: 0

Views: 75

Answers (1)

grapes
grapes

Reputation: 8646

You're welcome to choose the way which fits your needs better.

Option 1. Use host's networking. In this case Docker does not create separate net for container and you connect to container's services as if they would run on your host:

docker run --network=host <image_name>

Drawback of this approach - low isolation and thus security. You dont need to expose any ports here - if service listens on 8080, just open localhost:8080 and enjoy.

Second approach is more correct - you expose (somehow forward) internal ports in container and map them onto ports in the host.

docker run -p 8080:80 <image_name>

This will map port 80 from container to port 8080 on the host. As in previous example, you still connect using localhost, e.g. localhost:8080.

Upvotes: 1

Related Questions