在docker-compose中查找(dlv):没有这样的文件或目录:未知

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

delve (dlv) in docker-compose: no such file or directory: unknown

问题

我尝试使用docker-compose运行带有delvego二进制文件,并且遇到了一个错误:

ERROR: for nft-overseer  Cannot start service nft-overseer: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "dlv --listen=:${dlv_port} --headless=true --api-version=2 --accept-multiclient exec ./main": stat dlv --listen=:${dlv_port} --headless=true --api-version=2 --accept-multiclient exec ./main: no such file or directory: unknown

Dockerfile(简化版):

FROM golang:1.19.1-alpine3.16 as builder

RUN apk --no-cache --update --upgrade add git alpine-sdk

ENV GO111MODULE on
ENV CGO_ENABLED 1

WORKDIR /app
COPY . .
RUN go mod download
RUN go build -gcflags "all=-N -l" ./cmd/main

FROM golang:1.19.1-alpine3.16

RUN apk --no-cache --update --upgrade add curl
RUN go install github.com/go-delve/delve/cmd/dlv@v1.20.1

ARG config_file
ARG dlv_port

WORKDIR /app
COPY configs/${config_file:-config.yaml} configs/config.yaml
COPY --from=0 /app/main .

CMD ["dlv --listen=${dlv_port} --headless=true --api-version=2 --accept-multiclient exec ./main"]

docker-compose.yml(简化版):

services:
  my-service:
    container_name: my-service
    build:
      context: .
      dockerfile: ${DOCKER_FILE:-Dockerfile} # 使用Dockerfile_dlv
      args:
        config_file: ${CONFIG:-config.yaml}
        dlv_port: ${DLV_PORT}
    ports:
      - "10880:8080"
      - "10881:8081"
      - "10882:8082"
      - 10883:${DLV_PORT:-2000}
    healthcheck:
      test: ["CMD", "[[ ! -z ${DLV_PORT} ]] || curl", "-f", "http://localhost:8080/health"]
      interval: 10s
      timeout: 5s
      retries: 3
    restart: always

运行命令:

CONFIG=$CONFIG DLV_PORT=2000 DOCKER_FILE=Dockerfile_dlv docker-compose up --build --force-recreate -d

在容器中存在dlvmain文件:

...

Step 23/28 : RUN pwd
2976 ---> Running in 1d8709c338c3
2977/app
2978Removing intermediate container 1d8709c338c3
2979 ---> 95514a03baea
2980Step 24/28 : RUN ls -l
2981 ---> Running in f65052ccf5e6
2982total 30536
2983drwxr-xr-x    2 root     root          4096 Jan 13 05:57 configs
2984-rwxr-xr-x    1 root     root      31261568 Jan 13 07:19 main

...

Step 25/28 : RUN dlv
2988 ---> Running in 15b54660a772
2989Delve is a source level debugger for Go programs.
2990Delve enables you to interact with your program by controlling the execution of the process,
2991evaluating variables, and providing information of thread / goroutine state, CPU register state and more.
2992The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.
2993Pass flags to the program you are debugging using `--`, for example:
2994`dlv exec ./hello -- server --config conf/config.toml`

...
英文:

I try to run go binary with delve use docker-compose and get an error:

ERROR: for nft-overseer  Cannot start service nft-overseer: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "dlv --listen=:${dlv_port} --headless=true --api-version=2 --accept-multiclient exec ./main": stat dlv --listen=:${dlv_port} --headless=true --api-version=2 --accept-multiclient exec ./main: no such file or directory: unknown

Dockerfile (simplified)

FROM golang:1.19.1-alpine3.16 as builder

RUN apk --no-cache --update --upgrade add git alpine-sdk

ENV GO111MODULE on
ENV CGO_ENABLED 1

WORKDIR /app
COPY . .
RUN go mod download
RUN go build -gcflags "all=-N -l" ./cmd/main

FROM golang:1.19.1-alpine3.16

RUN apk --no-cache --update --upgrade add curl
RUN go install github.com/go-delve/delve/cmd/dlv@v1.20.1

ARG config_file
ARG dlv_port

WORKDIR /app
COPY configs/${config_file:-config.yaml} configs/config.yaml
COPY --from=0 /app/main .

CMD ["dlv --listen=${dlv_port} --headless=true --api-version=2 --accept-multiclient exec ./main"]

docker-compose.yml (simplified)

services:
  my-service:
    container_name: my-service
    build:
      context: .
      dockerfile: ${DOCKER_FILE:-Dockerfile} # 👈🏻 Dockerfile_dlv
      args:
        config_file: ${CONFIG:-config.yaml}
        dlv_port: ${DLV_PORT}
    ports:
      - "10880:8080"
      - "10881:8081"
      - "10882:8082"
      - 10883:${DLV_PORT:-2000}
    healthcheck:
      test: [ "CMD", "[[ ! -z ${DLV_PORT} ]] || curl", "-f", "http://localhost:8080/health" ]
      interval: 10s
      timeout: 5s
      retries: 3
    restart: always
CONFIG=$CONFIG DLV_PORT=2000 DOCKER_FILE=Dockerfile_dlv docker-compose up --build --force-recreate -d

dlv and main exists in container

...

Step 23/28 : RUN pwd
2976 ---> Running in 1d8709c338c3
2977/app
2978Removing intermediate container 1d8709c338c3
2979 ---> 95514a03baea
2980Step 24/28 : RUN ls -l
2981 ---> Running in f65052ccf5e6
2982total 30536
2983drwxr-xr-x    2 root     root          4096 Jan 13 05:57 configs
2984-rwxr-xr-x    1 root     root      31261568 Jan 13 07:19 main

...

Step 25/28 : RUN dlv
2988 ---> Running in 15b54660a772
2989Delve is a source level debugger for Go programs.
2990Delve enables you to interact with your program by controlling the execution of the process,
2991evaluating variables, and providing information of thread / goroutine state, CPU register state and more.
2992The goal of this tool is to provide a simple yet powerful interface for debugging Go programs.
2993Pass flags to the program you are debugging using `--`, for example:
2994`dlv exec ./hello -- server --config conf/config.toml`

...

答案1

得分: 2

你需要将 CMD 的部分分开:

CMD ["dlv", "--listen=${dlv_port}", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "./main"]

目前它正在寻找一个名为 "dlv --listen=${dlv_port} --headless=true --api-version=2 --accept-multiclient exec ./main" 的文件。

英文:

You need to separate the parts of the CMD:

CMD ["dlv", "--listen=${dlv_port}", "--headless=true", "--api-version=2", "--accept-multiclient", "exec", "./main"]

Right now it's looking for a file "dlv --listen=${dlv_port} --headless=true --api-version=2 --accept-multiclient exec ./main".

huangapple
  • 本文由 发表于 2023年1月13日 15:37:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75106112.html
匿名

发表评论

匿名网友

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

确定