How to run golang using docker?

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

How to run golang using docker?

问题

我正在尝试使用Docker与Go语言和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
英文:

I'm trying to use docker with go lang & PostgreSQL

I have this structure for the project:

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

the docker-compose.yml file contains ( I need GO server & 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

The Dockerfile contains:

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" ]

when I run docker-compose up --build I got this error:

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

答案1

得分: 2

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

英文:

Change the build line to build: . line. The compose file and Dockerfile are in the same directory.

huangapple
  • 本文由 发表于 2022年5月31日 04:17:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/72439644.html
匿名

发表评论

匿名网友

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

确定