簡體   English   中英

使用 docker 組合的 Golang 項目和 postgres 圖像不會失敗,但也不起作用

[英]Golang project and postgres image with docker compose up doesn't fail, but doesn't work either

不知道我在這里做錯了什么。 我正在嘗試使用持久數據庫制作一個 golang/postgres dockerized 項目。 以下是文件。 When I run go run main.go then curl http://localhost:8081/ I get the expected output, but when I try this with docker compose up I'm having issues, but everything seems to be working because I don't查看任何錯誤消息postgres-1 | 2022-08-29 05:31:59.703 UTC [1] LOG: database system is ready to accept connections postgres-1 | 2022-08-29 05:31:59.703 UTC [1] LOG: database system is ready to accept connections , but when I try curl http://localhost:8081/ I'm getting an error curl: (56) Recv failure: Connection reset by peer 我嘗試完全刪除 postgres 部分,但我仍然遇到同樣的問題。 我可以看到 docker 已啟動並正在運行並且端口正在偵聽

sudo lsof -i -P -n | grep 8081
docker-pr 592069            root    4u  IPv4 1760430      0t0  TCP *:8081 (LISTEN)

我在 Ubuntu 22.04 上使用它

main.go:

package main

import (
    "fmt"
    "log"
    "net/http"
)

func main() {

    http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "this can be anything")
    })

    http.HandleFunc("/try-it", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintf(w, "it worked!")
    })

    log.Fatal(http.ListenAndServe(":8081", nil))

}

Dockerfile:

FROM golang:1.19-alpine3.16

WORKDIR /app

COPY go.mod ./
RUN go mod download

COPY . .

RUN go build -o main .

RUN go build -v -o /usr/local/bin/app ./...

EXPOSE 8081
CMD [ "./main" ]

docker-compose.yml:

version: '3.9'
services:
  api:
    restart: unless-stopped
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8081:8080"
    depends_on:
      - postgres
    networks:
      - backend
  postgres:
    image: postgres
    restart: unless-stopped
    ports:
      - "5001:5432"
    volumes:
      - psqlVolume:/var/lib/postgresql/data
    environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_DB=${POSTGRES_DB}
    networks:
      - backend

networks:
  backend:

volumes:
  psqlVolume:

正如@DavidMaze 和@Brits 在評論中解釋的那樣,docker 容器已啟動並正在運行,但需要在容器和應用程序中映射端口。 例如在 main.go 這個方法 http.ListenAndServe(": :8084 http.ListenAndServe(":8084", nil)在應用程序中需要 Z1D78DC8ED51214E518B5114FE24490808 與容器

version: '3.9'
services:
  api:
    image: api
    restart: unless-stopped
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8083:8084" #host_port:container_port

可以在 docker 正在偵聽的主機端口上發出 curl 請求(在本例中為:8083 )。 例如curl http://localhost:8083/ 這將向主機發出請求,該請求將由正在偵聽端口:8083的 docker 捕獲,然后將請求傳輸到正在偵聽:8084的容器,如docker-compose.yml中指定的那樣。 如果端口映射不正確,則 curl 將返回curl: (56) Recv failure: Connection reset by peer 。感謝您的學習經驗,感謝您的幫助。

嘗試使用

postgres:
image: postgres:12.4-alpine
shm_size: 256m
environment:
      - POSTGRES_USER=${POSTGRES_USER}
      - POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
      - POSTGRES_HOST_AUTH_METHOD=trust
ports:
      - "5001:5432"
healthcheck:
      test: ["CMD-SHELL", "pg_isready -U postgres"]
      interval: 10s
      timeout: 5s
      retries: 5
api:
image: api
restart: unless-stopped
build:
context: .
dockerfile: Dockerfile
stop_signal: SIGTERM
ports:
    - "8081:8080"
deploy:
   mode: replicated
   replicas: 1
depends_on:
   - postgres
networks:
   - backend

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM