如何使用 docker 运行 golang?

How to run golang using docker?

我正在尝试将 docker 与 go lang 和 PostgreSQL 一起使用

我有这个项目结构:

-project_name
   - src
     - app
     - config
     - main.go
     - .env
     - docker-compose.yml 
     - Dockerfile

docker-compose.yml 文件包含(我需要 GO 服务器和 PostgreSQL)

version: '3.9'
services:
  app:
    container_name: logger_app
    build: ..
    ports:
      - "12000:12000"
    restart: on-failure
    volumes:
      - .:/app
    depends_on:
      - postgres
    networks:
      - MYAPP

  postgres:
    image: postgres:latest
    container_name: postgres_db
    environment:
      - POSTGRES_USER=${DB_USER}
      - POSTGRES_PASSWORD=${DB_PASSWORD}
      - POSTGRES_DB=${DB_NAME}
      - DATABASE_HOST=${DB_HOST}
    ports:
      - '5432:5432'
    networks:
      - MYAPP

networks:
  MYAPP:
    driver: bridge

Dockerfile 包含:

FROM golang:1.18 as builder

RUN mkdir /app
WORKDIR /app

COPY . .

RUN go get -d -v ./...

RUN go install -v ./...


RUN go build -o /build

EXPOSE 12000

CMD [ "/build" ]

当我 运行 docker-compose up --build 我得到这个错误:

failed to solve with frontend dockerfile.v0: failed to read dockerfile: open /var/lib/docker/tmp/buildkit-mount868285600/Dockerfile: no such file or directory
ERROR: Service 'app' failed to build : Build failed

将构建行更改为 build: . 行。 compose文件和Dockerfile在同一个目录下。