mornindew
mornindew

Reputation: 2221

Docker Port Not Exposing Golang

I am building a golang WebService in docker. The build seems fine but I am unable to expose the port for external (outside of container) access. When I curl from the command line (inside the container) the app appears to work fine.

I saw quite a few posts of similar problems but unfortunately many were not resolved or didn't seem applicable.

FROM golang:alpine
RUN mkdir /go/src/webservice_refArch
ADD . /go/src/webservice_refArch
WORKDIR /go/src/webservice_refArch
RUN apk add curl
RUN cd /go/src/webservice_refArch/ && go get ./...
RUN cd /go/src/webservice_refArch/cmd/reference-w-s-server && go build -o ../../server
EXPOSE 7878
ENTRYPOINT ["./server", "--port=7878"]

I have tried both:

Upvotes: 0

Views: 4374

Answers (3)

Praveen Kumar Patidar
Praveen Kumar Patidar

Reputation: 77

I was facing the same issue. Then what I did is change the ListernHost from localhost to 0.0.0.0 and it worked.

To debug this tried curl inside the container it was working fine but outside the container the response of the curl was blank. The port mapped but the content was not served outside the container. Once you change the "localhost" to 0.0.0.0 it will work.

Upvotes: 6

Johnny
Johnny

Reputation: 363

if you are trying to access the port inside your docker container from your local machine, you need map it to the desired port on your local machine docker run -p 7878:7878 IMAGE Then you should be able to access it on your host

Upvotes: 0

atline
atline

Reputation: 31584

See https://docs.docker.com/engine/reference/run/#expose-incoming-ports, just expose port in dockerfile is not enough.

You can add -p 7878:7878 when start container, or use -P to let docker set a automatical host port mapping for you.

If you do not want to do above, you can also add --net=host when start the container, then container will use host's network, if also works for you.

Upvotes: 2

Related Questions