Golang项目和使用Docker Compose启动的Postgres镜像既没有失败,也没有工作。

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

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

问题

我不确定我在这里做错了什么。我正在尝试创建一个带有持久化数据库的 Golang/Postgres Docker 化项目。以下是文件内容。当我运行 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 正在运行,并且端口正在监听。

我在 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:

希望对你有所帮助!

英文:

not sure what I did wrong here. I'm trying to make a golang/postgres dockerized project with a persistent db. Below are the files. 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 see any error messages 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. I tried removing the postgres part entirely and I'm still getting the same problem. I can see that docker is up and running and the port is listening

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

I'm using this on 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:

答案1

得分: 1

如@DavidMaze和@Brits在评论中解释的那样,Docker容器正在运行,但是端口需要在容器和应用程序中进行映射。例如,在main.go文件中,这个方法http.ListenAndServe(":8084", nil)需要与容器端口进行映射(在这种情况下是:8084)。

以下是一个示例的docker-compose.yml文件:

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/。这将向主机机器发出请求,并且该请求将被Docker捕获,Docker在端口:8083上监听,然后将请求传递给容器,容器在:8084上监听,正如在docker-compose.yml中指定的那样。如果端口映射不正确,curl将返回curl: (56) Recv failure: Connection reset by peer。感谢您的学习经验,我非常感谢您的帮助。

英文:

As @DavidMaze and @Brits explained in the comments the docker container is up and running, but the ports need to be mapped in both the container and the application. For example in main.go this method http.ListenAndServe(":8084", nil) in the app would need to map with the container port (in this case :8084)

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

a curl request could be made on the host port where docker is listening (in this case it's :8083). For example curl http://localhost:8083/. This would make a request to the host machine and that request would be captured by docker which is listening on port :8083 then transmit the request to the container which is listening on :8084 as specified in the docker-compose.yml. If the port mapping isn't correct then curl will return curl: (56) Recv failure: Connection reset by peer.Thank you for the learning experience and I appreciate all your help.

huangapple
  • 本文由 发表于 2022年8月29日 21:34:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/73529670.html
匿名

发表评论

匿名网友

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

确定