服务”app”依赖于未定义的服务”db”:无效的Compose项目。

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

service "app" depends on undefined service db: invalid compose project

问题

我在Gin框架中构建了一个Go服务器,现在我想将其部署到GCP。我正在尝试在GCP Compute Engine的虚拟机上运行docker compose up -d命令。该命令在我的本地机器上成功运行,但在虚拟机终端中显示错误。
我在终端中收到的错误是:
service "app" depends on undefined service db: invalid compose project

我的docker-compose.yml代码如下:

version: "3.7"

services:
  database:
    container_name: mywealth-database
    image: postgres:12.8
    restart: always
    environment:
      - POSTGRES_USER=sharauq
      - POSTGRES_PASSWORD=sharauq
      - POSTGRES_DB=mywealth
    ports:
      - 5432:5432
    volumes:
      - db:/var/lib/postgresql/data
  app:
        container_name: app
        restart: always
        build: .
        ports:
            - "8080:8080"
        depends_on: 
            - db

volumes:
  db:

我的Dockerfile如下:

# Start from the latest golang base image.
FROM golang:latest

# Add maintainer information
LABEL maintainer="sharaukadr2001@gmail.com"

# Set the current working directory inside an image.
WORKDIR /app

# Copy Go module dependency requirements file.
COPY go.mod .

# Copy Go Modules expected hashes file.
COPY go.sum .

# Download dependencies.
RUN go mod download

# Copy all sources.
COPY . .

# Build the application.
RUN go build -o /mywealth

# Delete source files.
RUN find . -name "*.go" -type f -delete

# Run the application.
CMD ["/mywealth"]
英文:

I build a Go server in Gin framework and now I want to deploy it to GCP. I am trying to run docker compose up -d on VM in GCP Compute Engine. The command successfully runs in my local machine but shows error in VM terminal
The error I am getting in terminal is:
service "app" depends on undefined service db: invalid compose project

My docker-compose.yml code is:

version: "3.7"

services:
  database:
    container_name: mywealth-database
    image: postgres:12.8
    restart: always
    environment:
      - POSTGRES_USER=sharauq
      - POSTGRES_PASSWORD=sharauq
      - POSTGRES_DB=mywealth
    ports:
      - 5432:5432
    volumes:
      - db:/var/lib/postgresql/data
  app:
        container_name: app
        restart: always
        build: .
        ports:
            - "8080:8080"
        depends_on: 
            - db

volumes:
  db:

My Dockerfile is:

# Start from the latest golang base image.
FROM golang:latest

# Add maintainer information
LABEL maintainer="sharaukadr2001@gmail.com"

# Set the current working directory inside an image.
WORKDIR /app

# Copy Go module dependency requirements file.
COPY go.mod .

# Copy Go Modules expected hashes file.
COPY go.sum .

# Download dependencies.
RUN go mod download

# Copy all sources.
COPY . .

# Build the application.
RUN go build -o /mywealth

# Delete source files.
RUN find . -name "*.go" -type f -delete

# Run the application.
CMD ["/mywealth"]

答案1

得分: 2

你依赖其他服务,而不是卷。

你的数据库服务被称为 database,所以在 docker-compose.yml 文件中 app 服务定义的 depends_on 应该是 database,而不是 db

英文:

You 'depend' on other services. Not on volumes.

Your database service is called database, so the 'depends_on' in the app service definition in the docker-compose.yml file should be database. Not db.

huangapple
  • 本文由 发表于 2022年10月18日 00:55:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/74100665.html
匿名

发表评论

匿名网友

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

确定