Docker: Localhost:8080 not responding

Issue

I’m running a docker:

docker run -p 8080:8080 -t admin

The localhost:8080 is not responding, but I run the go run admin/main.go I can reach localhost:8080 I know that there are many question about this issue, and I looked into everything:

Using the right ports: 8080:8080

I’m able to reach the server while running on my local machine: check that the server is serving the right port.

Here is my Dockerfile – exposing the right port 8080:

FROM --platform=linux/amd64 golang:1.19.3-bullseye

# Install grpc
RUN go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2 && \
    go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28

WORKDIR /app
COPY . .
# Install protoc and zip system library
RUN apt-get update && apt-get install -y zip && apt-get install -y tree && \
    mkdir /opt/protoc && cd /opt/protoc && wget https://github.com/protocolbuffers/protobuf/releases/download/v3.7.0/protoc-3.7.0-linux-x86_64.zip && \
    unzip protoc-3.7.0-linux-x86_64.zip

# Copy the grpc proto file and generate the go module
# RUN /opt/protoc/bin/protoc --proto_path=/app --go_out=/app --go_opt=paths=source_relative --go-grpc_out=/app --go-grpc_opt=paths=source_relative /app/proto/textbear.proto /app/proto/server.proto
RUN /opt/protoc/bin/protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative proto/textbear.proto db/proto/db.proto server/proto/server.proto
RUN go mod download
EXPOSE 8080
RUN go build -o /admin admin/main.go
ENTRYPOINT ["/admin"]

I’m probably missing something obvious.

Solution

My admin container depends on the db container network. My admin container was using the localhost address and causing the admin container to get stuck waiting to connect to the db container. I fixed by changing the address from localhost to db and because docker compose will generate the host to all the images, the admin could communicate to db using the db hostname.

Here is my Dockerfile:

version: '3'

services:
  admin:
    image: admin
    ports:
      - "8080:8080"
  db:
    image: db
    ports:
      - "50052:50052"
  server:
    image: server
    ports:
      - "50051:50051"

Answered By – amb1s1

Answer Checked By – Pedro (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.