不确定我在这里做错了什么。我正在尝试使用持久数据库创建一个 golang/postgres dockerized 项目。以下是文件。当我运行时go run main.go,curl http://localhost:8081/我得到了预期的输出,但是当我尝试这个时docker compose up我遇到了问题,但一切似乎都在工作,因为我没有看到任何错误消息postgres-1 | 2022-08-29 05:31:59.703 UTC [1] LOG: database system is ready to accept connections,但是当我尝试时curl http://localhost:8081/我遇到了错误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 上使用它
主要去:
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))
}
文件:
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:
翻阅古今
相关分类