【Go】Air&Dockerでのエラー `/bin/sh: 1: .../tmpmain.exe: not found` の対処法
eyecatch

2023-02-13

【Go】Air&Dockerでのエラー `/bin/sh: 1: .../tmpmain.exe: not found` の対処法

エラー解決

環境

  • Windows 11 Home
  • Go 1.20
  • Echo v3.3.10
  • Air v1.41.0(※Golangのホットリロードツール)
  • Docker Desktop v4.16.3

エラー

Golangのホットリロードツールである「Air」のコマンド air init で初期設定ファイル .air.toml を自動で出力したあとに、
Docker経由( docker-compose up )で開始コマンド air を実行したところ、途中でエラーが出る。

  • エラー内容(下記例: /bin/sh: 1: /go/src/app/tmpmain.exe: not found)
PS C:\Users\<user_name>\<project_dir>\golang-test> docker-compose up
[+] Running 1/0
 - Container golang-test-app-1  Created                                                                                                                           0.0s 
Attaching to golang-test-app-1
golang-test-app-1  | 
golang-test-app-1  |   __    _   ___
golang-test-app-1  |  / /\  | | | |_)
golang-test-app-1  | /_/--\ |_| |_| \_ , built with Go
golang-test-app-1  |
golang-test-app-1  | [23:56:56] watching .
golang-test-app-1  | [23:56:56] !exclude tmp
golang-test-app-1  | [23:56:56] building...
golang-test-app-1  | [23:56:56] running...
golang-test-app-1  | /bin/sh: 1: /go/src/app/tmpmain.exe: not found


修正前

  • docker-compose.yml
version: '3'
services:
  app:
    build: .
    volumes:
      - ./:/go/src/app
    ports:
      - "8080:8080"
  • Dockerfile
FROM golang:1.20

ENV ROOT=/go/src/app
WORKDIR ${ROOT}

COPY . .
EXPOSE 8080

# Airをインストール → 実行
RUN go install github.com/cosmtrek/air@latest
CMD ["air"]
  • .air.toml
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  args_bin = []
  bin = "tmp\\main.exe"
  cmd = "go build -o ./tmp/main.exe ."
  delay = 0
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  include_file = []
  kill_delay = "0s"
  log = "build-errors.log"
  rerun = false
  rerun_delay = 500
  send_interrupt = false
  stop_on_error = false

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  main_only = false
  time = false

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false
  keep_scroll = true


解決策

.air.toml 内の bin = "tmp\\main.exe"bin = "tmp/main.exe" に修正する。

修正後

  • .air.toml
root = "."
testdata_dir = "testdata"
tmp_dir = "tmp"

[build]
  args_bin = []
  bin = "tmp/main.exe" # ← ココ(修正箇所)
  cmd = "go build -o ./tmp/main.exe ."
  delay = 0
  exclude_dir = ["assets", "tmp", "vendor", "testdata"]
  exclude_file = []
  exclude_regex = ["_test.go"]
  exclude_unchanged = false
  follow_symlink = false
  full_bin = ""
  include_dir = []
  include_ext = ["go", "tpl", "tmpl", "html"]
  include_file = []
  kill_delay = "0s"
  log = "build-errors.log"
  rerun = false
  rerun_delay = 500
  send_interrupt = false
  stop_on_error = false

[color]
  app = ""
  build = "yellow"
  main = "magenta"
  runner = "green"
  watcher = "cyan"

[log]
  main_only = false
  time = true

[misc]
  clean_on_exit = false

[screen]
  clear_on_rebuild = false
  keep_scroll = true


補足

単純に、Windows系とUNIX系のパスの違い(参考)が原因でした...(^-^;)
修正前の状態でも、Docker上ではなくWindowsのローカル上、つまり直接 air コマンドから実行すると当然エラーは出ません。
Macであれば、UNIX系のパス(bin = "tmp/main.exe")で .air.toml を出力してくれるため、特に修正は不要でした。

以上です。 最後まで読んでいただきありがとうございました!

自己紹介

logo

かわいち

都内ではたらくWeb系エンジニアです! 普段から効率良く開発できるように心がけています。 また、趣味でもプログラミングをしており、モノづくりが好きです。 このブログでは、プログラミングに役立つコトを日々発信しています!