Lansana Camara
Lansana Camara

Reputation: 9871

Container should communicate to host network, but does not

I have a two HTTP servers on my host machine; one listening on 8080, the other listening on 8081. The 8080 is a webapp, and the 8081 is an API.

I also have a Docker container that should connect to the webapp on 8080 using an automated tool, and that webapp should make HTTP requests to the API that's on 8081.

Here is a visual representation of what I want:

Host machine HTTP 8080
          ⇩            ⇖
          ⇧              Docker container 
Host machine HTTP 8081

The problem I'm having is that the Docker container cannot connect to the website on the host machines 8080. I'm not sure why, because I set the --network=host flag, so shouldn't it be using the host machines network?

This is my Docker image:

## Redacted irrelevant stuff...

EXPOSE 8080 8081

This is how run the container:

docker run -d -p 8080:8080 -p 8081:8081 --network=host --name=app app

Any ideas what's wrong with my setup?

Upvotes: 0

Views: 53

Answers (1)

yamenk
yamenk

Reputation: 51906

So you have two services running directly on the machine and you want to deploy a Docker container that should connect to one of those services. In that case, you shouldn't map those port to the container and you shouldn't expose those ports in the Dockerfile as those ports are not for the container.

  1. Remove the Expose ports from the Dockerfile
  2. Start the container using docker run -d --network=host --name=app app. The container should be able to access the services using localhost:8080.

Upvotes: 1

Related Questions