简体   繁体   中英

Connection refused to localhost with Docker container (Windows)

I'm working on Windows with Docker Desktop.

I built a Docker container which has this Dockerfile:

FROM ubuntu:20.04
RUN apt-get update && apt-get install -y \
    default-jre \
    default-jdk \
    && rm -rf /var/lib/apt/lists/*
COPY ./src/ /srcMicroservizio
RUN cd /srcMicroservizio && javac -cp .:BouncyCastle1.jar:BouncyCastle2.jar:byteLib.jar:clientMqtt.jar:gson-2.8.6.jar:sqlite-jdbc-3.27.2.1.jar ./analisiDati/* ./db/* ./oggetti/*
EXPOSE 3001
CMD cd /srcMicroservizio && java -cp .:BouncyCastle1.jar:BouncyCastle2.jar:byteLib.jar:clientMqtt.jar:gson-2.8.6.jar:sqlite-jdbc-3.27.2.1.jar analisiDati/AnalisiDati

And then I started it with the following command:

docker build -t analisidati-pissir . && docker run -p 3001:3001/tcp --net=host --name analisidati-pissir-container -it analisidati-pissir

Inside the Java code of the microservice I created a server on port 3001 with:

HttpServer server = HttpServer.create(new InetSocketAddress(3001), 0);

But when I try to contact the server using:

curl -i "localhost:3001"

I obtain the following error:

curl: (7) Failed to connect to localhost port 3001: Connection refused

Why isn't Windows able to contact the container?

I tried to use the container on Ubuntu 20.04 virtual machine and it worked correctly.

Also, if I run the code of the microservice using Eclipse instead of the container it works.

Is this a Docker's bridging problem?

Is there a way to fix it?

The parameter -.network=host (or -.net=host ) is only supported on Linux. Unfortunately Docker does not complain when you use the parameter under Windows, but you will get a "connection refused" error message.

As a test i used the following command:

  • docker run --rm -p 80:80 nginx and with that I could connect to localhost:80
  • Using docker run --rm -p 80:80 -.network=host nginx gave me a "connection refused" error message when trying to access localhost:8080

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM