my-appname| /bin/sh: 1: /app/tmpmain.exe: not found | air

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

my-appname| /bin/sh: 1: /app/tmpmain.exe: not found | air

问题

我遇到了一个问题,我试图在Docker中使用air运行我的Go Fiber项目,但是出现了以下错误:
uni-blog | /bin/sh: 1: /app/tmpmain.exe: not found

我使用的环境是:

  • Windows 11
  • Docker Desktop
  • 最新的Golang版本
  • air 1.27.10
  • 最新的Fiber版本

这是我的docker-compose和Dockerfile:

docker-compose.yml:

# docker-compose up -d --build
version: "3.8"

services:
  app:
    container_name: uni-blog
    image: app-dev
    build:
      context: .
      target: development
    volumes:
      - ./:/app
    ports:
      - 3000:3000

Dockerfile:

FROM golang:1.17 as development

RUN apt update && apt upgrade -y && \
  apt install -y git \
  make openssh-client

RUN curl -fLo install.sh https://raw.githubusercontent.com/cosmtrek/air/master/install.sh \
    && chmod +x install.sh && sh install.sh && cp ./bin/air /bin/air

RUN air -v

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

EXPOSE 3000

CMD air

我还尝试按照README中的说明安装air,但仍然出现这个错误。请帮忙看看。

提前感谢!

my-appname| /bin/sh: 1: /app/tmpmain.exe: not found | air

my-appname| /bin/sh: 1: /app/tmpmain.exe: not found | air

英文:

I'm facing an issue, am trying to run my go fiber project inside docker with air but am getting this error
uni-blog | /bin/sh: 1: /app/tmpmain.exe: not found

am using
Windows 11
Docker desktop
golang latest
air 1.27.10
fiber latest

Here is my docker compose and dockerfile

# docker-compose up -d --build
version: "3.8"

services:
  app:
    container_name: uni-blog
    image: app-dev
    build:
      context: .
      target: development
    volumes:
      - ./:/app
    ports:
      - 3000:3000

FROM golang:1.17 as development

RUN apt update && apt upgrade -y && \
  apt install -y git \
  make openssh-client

RUN curl -fLo install.sh https://raw.githubusercontent.com/cosmtrek/air/master/install.sh \
    && chmod +x install.sh && sh install.sh && cp ./bin/air /bin/air

RUN air -v

WORKDIR /app

COPY go.mod go.sum ./

RUN go mod download

COPY . .

EXPOSE 3000

CMD air

I also tried installing air following the readME instructions still it gives me this error
Please help

Thanks in advance

my-appname| /bin/sh: 1: /app/tmpmain.exe: not found | air

my-appname| /bin/sh: 1: /app/tmpmain.exe: not found | air

答案1

得分: 1

你的 volumes: 挂载替换了镜像中的 /app 目录,使用主机上的内容。如果二进制文件是在 Dockerfile 中构建的,那么这个 volumes: 挂载会隐藏它;如果你在主机上的相同位置没有匹配的兼容二进制文件,你会得到一个类似你所看到的错误。

我建议移除这个 volumes: 块,这样你就可以运行镜像中内置的二进制文件了。docker-compose.yml 文件可以简化为以下内容:

version: '3.8'
services:
  app:
    build: .
    ports:
      - '3000:3000'
英文:

The volumes: mount you have replaces the /app directory in the image with content from the host. If the binary is built in the Dockerfile, that volumes: mount hides it; if you don't have a matching compatible binary on the host in the same place, you'll get an error like what you see.

I'd remove that volumes: block so you're actually running the binary that's built into the image. The docker-compose.yml file can be reduced to as little as:

version: '3.8'
services:
  app:
    build: .
    ports:
      - '3000:3000'

答案2

得分: 1

如果你看一下错误信息,你会注意到在 tmp/main.exe 之间有一个拼写错误:

/bin/sh: 1: /app/tmpmain.exe: not found

这个错误来自于 .air.toml 配置文件:

bin = "tmp\\main.exe"

在项目根目录下创建 .air.toml 文件,内容如下:

root = "."
tmp_dir = "tmp"

[build]

  # 构建可执行文件
  cmd = "go build -o ./tmp/main.exe ."

  # 读取可执行文件
  bin = "tmp/main.exe"

  # 监听这些文件的变化
  include_ext = ["go", "yml"]

  # 忽略这些文件的变化
  exclude_dir = ["tmp"]

  # 防止构建触发得太快
  delay = 1000 # 毫秒

[misc]
  clean_on_exit = true
英文:

If you look the error, you ca notice there is a typo between tmp/main.exe:

/bin/sh: 1: /app/tmpmain.exe: not found

This is coming from .air.toml config file:

    bin = "tmp\\main.exe"

Create .air.toml file in project root like so:

root = "."
tmp_dir = "tmp"

[build]

  # Build binary.
  cmd = "go build -o ./tmp/main.exe ."

  # Read binary.
  bin = "tmp/main.exe"

  # Watch changes in those files
  include_ext = [ "go", "yml"]

  # Ignore changes in these files
  exclude_dir = ["tmp"]

  # Stop builds from triggering too fast
  delay = 1000 # ms

[misc]
  clean_on_exit = true

huangapple
  • 本文由 发表于 2022年2月10日 17:29:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/71062642.html
匿名

发表评论

匿名网友

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

确定