Docker:本地主机:8080无响应

huangapple go评论83阅读模式
英文:

Docker: Localhost:8080 not responding

问题

我正在运行一个 Docker 容器:

docker run -p 8080:8080 -t admin

但是 localhost:8080 没有响应,但是当我运行 go run admin/main.go 时,我可以访问 localhost:8080。我知道关于这个问题有很多疑问,我已经查看了所有内容:

确保使用了正确的端口:8080:8080

在本地机器上运行时,我可以访问服务器:检查服务器是否在正确的端口上提供服务。

这是我的 Dockerfile - 暴露了正确的端口 8080:

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

# 安装 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 . .
# 安装 protoc 和 zip 系统库
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

# 复制 grpc proto 文件并生成 go 模块
# 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 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.

答案1

得分: 0

我的管理容器依赖于数据库容器网络。我的管理容器使用了localhost地址,导致管理容器在等待连接到数据库容器时被卡住。我通过将地址从localhost更改为db来修复了这个问题,因为Docker Compose会为所有的镜像生成主机名,所以管理容器可以使用db主机名与数据库通信。

这是我的Dockerfile:

version: '3'

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

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"

huangapple
  • 本文由 发表于 2022年11月13日 13:27:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/74418685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定