0

I am learning docker,this is my dockerfile . I start a http service in docker and expose 9999 port,here is the content:

FROM golang:1.20
LABEL authors="tom"

WORKDIR /app

COPY . /app

RUN go build -o main .

EXPOSE 9999

ENTRYPOINT ["/app/main"]

step 1 : I build the dockerfile to generate docker image called "cache-go".

step 2 : I run the docker service by using:docker run --net=host -p 8899:9999 cache-go.

step 3 : I test my web api in docker shell:curl "http://localhost:9999/api?key=Tom" , it's successful.

step 4 : I try to access the web-api in my real host computer :"http://localhost:8899/api?key=Tom". failed

step1 to 3 are right,I tried to open the firewall and enable IpEnablerouting setting,but still failed. I'm confused about the problem,please help me, I will be appreciate for your help.

I tried to open the firewall and enable IpEnablerouting setting,but still failed.

-------------done------------ I edited 127.0.0.1 to 0.0.0.0 and solved the problem. I understand it by reading this blog pythonspeed.com/articles/docker-connection-refused

6
  • Can you provide the complete curl command and its output? You could also run nc -zv localhost 8899 and provide the output here too.
    – Itagyba Abondanza Kuhlmann
    Commented May 12, 2023 at 20:31
  • By the way, are you using WSL to run the docker command? I am confused when you say Windows Host machine. Can you describe in more detail your topology?
    – Itagyba Abondanza Kuhlmann
    Commented May 12, 2023 at 20:40
  • --net host disables Docker's networking layer, and among other things means -p port mappings don't work. You should almost never need this option, and it can cause trouble if you're using Docker Desktop or a similar VM-based solution. Does removing this work?
    – David Maze
    Commented May 12, 2023 at 20:42
  • No,I use windows 10 to run the docker. I developed a simple k-v server on my windows 10,I expose 9999 port in the server to receive a "Get" http method,it respond key's value. The complete curl command in docker shell is :curl "localhost:9999/api?key=Tom" ,It‘s worked. I also build docker image and run docker on windows 10 system.
    – tom
    Commented May 13, 2023 at 6:25
  • I also tried this docker run command is :docker run -p 8899:9999 --network=host my-Image. It's still useless
    – tom
    Commented May 13, 2023 at 6:28

0

You must log in to answer this question.

Browse other questions tagged .