在Docker容器中找不到可执行文件。

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

executable not found on docker container

问题

我正在尝试在一个带有Go应用程序的Docker容器中运行CMD命令时遇到了一些问题。

这是我得到的输出:

  1. golang-api | /bin/sh: ./server: not found
  2. golang-api exited with code 127

这是Dockerfile:

  1. FROM golang:1.18-alpine
  2. RUN apk add g++ && apk add make
  3. WORKDIR /app
  4. COPY go.mod go.sum ./
  5. RUN go mod download
  6. COPY . .
  7. RUN make build
  8. EXPOSE 8000
  9. CMD ./server

这是负责构建命令的Makefile:

  1. LINUX_AMD64 = GOOS=linux GOARCH=amd64 CGO_ENABLED=1 GO111MODULE=on
  2. migrate:
  3. cd cmd/migrations/$(FOLDER) && go run main.go
  4. build:
  5. cd cmd && $(LINUX_AMD64) go build -a -v -tags musl -o server

这是docker-compose.yml:

  1. version: '3.1'
  2. services:
  3. api:
  4. build:
  5. dockerfile: Dockerfile
  6. context: .
  7. container_name: golang-api
  8. ports:
  9. - "8000:8000"
  10. restart: unless-stopped
  11. depends_on:
  12. - db
  13. environment:
  14. - POSTGRES_URL=$POSTGRES_URL
  15. db:
  16. image: postgres
  17. restart: always
  18. environment:
  19. POSTGRES_USER: postgres
  20. POSTGRES_PASSWORD: postgres
  21. ports:
  22. - '5432:5432'

完整输出:

  1. Building api
  2. Sending build context to Docker daemon 74.24kB
  3. Step 1/9 : FROM golang:1.18-alpine
  4. ---> 6078a5fce1f5
  5. Step 2/9 : RUN apk add g++ && apk add make
  6. ---> Using cache
  7. ---> 2a85b9182b80
  8. Step 3/9 : WORKDIR /app
  9. ---> Using cache
  10. ---> baf1e6b7047c
  11. Step 4/9 : COPY go.mod go.sum ./
  12. ---> Using cache
  13. ---> 1f2d031bc1b0
  14. Step 5/9 : RUN go mod download
  15. ---> Using cache
  16. ---> 471d6f24e6a9
  17. Step 6/9 : COPY . .
  18. ---> eaa86ff7cb1b
  19. Step 7/9 : RUN make build
  20. ---> Running in d6ae6ce79222
  21. cd cmd && GOOS=linux GOARCH=amd64 CGO_ENABLED=1 GO111MODULE=on go build -a -v -tags musl -o server
  22. internal/goos
  23. internal/goarch
  24. internal/race
  25. internal/unsafeheader
  26. internal/goexperiment
  27. runtime/internal/syscall
  28. internal/cpu
  29. sync/atomic
  30. runtime/internal/atomic
  31. internal/itoa
  32. math/bits
  33. internal/abi
  34. runtime/internal/math
  35. runtime/internal/sys
  36. unicode/utf8
  37. unicode
  38. container/list
  39. crypto/internal/subtle
  40. crypto/subtle
  41. unicode/utf16
  42. vendor/golang.org/x/crypto/cryptobyte/asn1
  43. internal/nettrace
  44. vendor/golang.org/x/crypto/internal/subtle
  45. encoding
  46. github.com/mymachine/my-api/internal/controllers/dto
  47. github.com/mymachine/my-api/internal/controllers/helpers
  48. internal/bytealg
  49. math
  50. runtime
  51. internal/reflectlite
  52. sync
  53. internal/testlog
  54. internal/singleflight
  55. math/rand
  56. runtime/cgo
  57. errors
  58. sort
  59. internal/oserror
  60. strconv
  61. path
  62. vendor/golang.org/x/net/dns/dnsmessage
  63. io
  64. crypto/elliptic/internal/fiat
  65. syscall
  66. golang.org/x/text/internal/tag
  67. hash
  68. bytes
  69. strings
  70. hash/crc32
  71. reflect
  72. crypto
  73. crypto/internal/randutil
  74. crypto/hmac
  75. crypto/rc4
  76. net/http/internal/ascii
  77. vendor/golang.org/x/crypto/hkdf
  78. regexp/syntax
  79. github.com/jackc/chunkreader/v2
  80. bufio
  81. crypto/elliptic/internal/nistec
  82. internal/syscall/unix
  83. time
  84. internal/syscall/execenv
  85. vendor/golang.org/x/text/transform
  86. golang.org/x/crypto/pbkdf2
  87. golang.org/x/text/transform
  88. golang.org/x/text/runes
  89. golang.org/x/text/width
  90. regexp
  91. internal/poll
  92. io/fs
  93. context
  94. github.com/mymachine/my-api/internal/entities
  95. github.com/jackc/pgconn/internal/ctxwatch
  96. embed
  97. os
  98. github.com/jinzhu/inflection
  99. github.com/jinzhu/now
  100. internal/fmtsort
  101. encoding/binary
  102. crypto/ed25519/internal/edwards25519/field
  103. crypto/md5
  104. crypto/sha512
  105. crypto/cipher
  106. crypto/sha256
  107. crypto/sha1
  108. encoding/base64
  109. fmt
  110. internal/godebug
  111. encoding/pem
  112. crypto/ed25519/internal/edwards25519
  113. path/filepath
  114. vendor/golang.org/x/crypto/internal/poly1305
  115. io/ioutil
  116. internal/intern
  117. vendor/golang.org/x/crypto/curve25519/internal/field
  118. vendor/golang.org/x/sys/cpu
  119. net/netip
  120. crypto/aes
  121. crypto/des
  122. vendor/golang.org/x/crypto/chacha20
  123. github.com/jackc/pgio
  124. github.com/jackc/pgpassfile
  125. os/exec
  126. os/signal
  127. net
  128. vendor/golang.org/x/crypto/chacha20poly1305
  129. math/big
  130. encoding/hex
  131. net/url
  132. compress/flate
  133. vendor/golang.org/x/crypto/curve25519
  134. log
  135. vendor/golang.org/x/text/unicode/norm
  136. vendor/golang.org/x/text/unicode/bidi
  137. vendor/golang.org/x/net/http2/hpack
  138. mime
  139. mime/quotedprintable
  140. compress/gzip
  141. net/http/internal
  142. database/sql/driver
  143. github.com/mymachine/my-api/internal/pkg/errors
  144. encoding/json
  145. github.com/jackc/pgservicefile
  146. vendor/golang.org/x/text/secure/bidirule
  147. golang.org/x/text/internal/language
  148. database/sql
  149. golang.org/x/text/unicode/norm
  150. golang.org/x/text/unicode/bidi
  151. vendor/golang.org/x/net/idna
  152. os/user
  153. crypto/rand
  154. encoding/asn1
  155. crypto/dsa
  156. crypto/elliptic
  157. crypto/ed25519
  158. crypto/rsa
  159. github.com/jackc/pgproto3/v2
  160. golang.org/x/text/internal/language/compact
  161. golang.org/x/text/secure/bidirule
  162. github.com/jackc/pgx/v4/internal/sanitize
  163. golang.org/x/text/language
  164. go/token
  165. vendor/golang.org/x/crypto/cryptobyte
  166. crypto/x509/pkix
  167. gorm.io/gorm/utils
  168. gorm.io/gorm/logger
  169. encoding/gob
  170. go/scanner
  171. golang.org/x/text/internal
  172. github.com/joho/godotenv
  173. crypto/ecdsa
  174. go/ast
  175. golang.org/x/text/cases
  176. github.com/joho/godotenv/autoload
  177. golang.org/x/text/secure/precis
  178. gorm.io/gorm/clause
  179. gorm.io/gorm/schema
  180. gorm.io/gorm
  181. net/textproto
  182. vendor/golang.org/x/net/http/httpproxy
  183. github.com/google/uuid
  184. crypto/x509
  185. github.com/jackc/pgtype
  186. mime/multipart
  187. vendor/golang.org/x/net/http/httpguts
  188. crypto/tls
  189. gorm.io/gorm/migrator
  190. gorm.io/gorm/callbacks
  191. net/http/httptrace
  192. github.com/jackc/pgconn
  193. net/http
  194. github.com/jackc/pgconn/stmtcache
  195. github.com/jackc/pgx/v4
  196. github.com/jackc/pgx/v4/stdlib
  197. gorm.io/driver/postgres
  198. github.com/mymachine/my-api/internal/infrastructure/repository
  199. github.com/mymachine/my-api/configs
  200. github.com/mymachine/my-api/internal/services
  201. github.com/mymachine/my-api/internal/services/transaction
  202. github.com/mymachine/my-api/internal/controllers/handlers/health
  203. github.com/gorilla/mux
  204. github.com/mymachine/my-api/internal/controllers/handlers/transaction
  205. github.com/mymachine/my-api/internal/router
  206. github.com/mymachine/my-api/cmd
  207. Removing intermediate container d6ae6ce79222
  208. ---> 621aa6a266de
  209. Step 8/9 : EXPOSE 8000
  210. ---> Running in 3cbd17d32c37
  211. Removing intermediate container 3cbd17d32c37
  212. ---> 754f71a7753e
  213. Step 9/9 : CMD ./server
  214. ---> Running in dfcdc8cd45ce
  215. Removing intermediate container dfcdc8cd45ce
  216. ---> 511dbf89a001
  217. Successfully built 511dbf89a001
  218. Successfully tagged my-api_api:latest
  219. Creating my-api_db_1 ... done
  220. Creating golang-api ... done
  221. Attaching to my-api_db_1, golang-api
  222. golang-api | /bin/sh: ./server: not found
英文:

I'm having some issue trying to run a CMD command inside a docker container with a Go application.

This is the output I've got:

  1. golang-api | /bin/sh: ./server: not found
  2. golang-api exited with code 127

This is the Dockerfile

  1. FROM golang:1.18-alpine
  2. RUN apk add g++ && apk add make
  3. WORKDIR /app
  4. COPY go.mod go.sum ./
  5. RUN go mod download
  6. COPY . .
  7. RUN make build
  8. EXPOSE 8000
  9. CMD ./server

And this is the Makefile responsible for the build command:

  1. LINUX_AMD64 = GOOS=linux GOARCH=amd64 CGO_ENABLED=1 GO111MODULE=on
  2. migrate:
  3. cd cmd/migrations/$(FOLDER) && go run main.go
  4. build:
  5. cd cmd && $(LINUX_AMD64) go build -a -v -tags musl -o server

And the docker-compose.yml

  1. version: '3.1'
  2. services:
  3. api:
  4. build:
  5. dockerfile: Dockerfile
  6. context: .
  7. container_name: golang-api
  8. ports:
  9. - "8000:8000"
  10. restart: unless-stopped
  11. depends_on:
  12. - db
  13. environment:
  14. - POSTGRES_URL=$POSTGRES_URL
  15. db:
  16. image: postgres
  17. restart: always
  18. environment:
  19. POSTGRES_USER: postgres
  20. POSTGRES_PASSWORD: postgres
  21. ports:
  22. - '5432:5432'

Complete output:

  1. Building api
  2. Sending build context to Docker daemon 74.24kB
  3. Step 1/9 : FROM golang:1.18-alpine
  4. ---> 6078a5fce1f5
  5. Step 2/9 : RUN apk add g++ && apk add make
  6. ---> Using cache
  7. ---> 2a85b9182b80
  8. Step 3/9 : WORKDIR /app
  9. ---> Using cache
  10. ---> baf1e6b7047c
  11. Step 4/9 : COPY go.mod go.sum ./
  12. ---> Using cache
  13. ---> 1f2d031bc1b0
  14. Step 5/9 : RUN go mod download
  15. ---> Using cache
  16. ---> 471d6f24e6a9
  17. Step 6/9 : COPY . .
  18. ---> eaa86ff7cb1b
  19. Step 7/9 : RUN make build
  20. ---> Running in d6ae6ce79222
  21. cd cmd && GOOS=linux GOARCH=amd64 CGO_ENABLED=1 GO111MODULE=on go build -a -v -tags musl -o server
  22. internal/goos
  23. internal/goarch
  24. internal/race
  25. internal/unsafeheader
  26. internal/goexperiment
  27. runtime/internal/syscall
  28. internal/cpu
  29. sync/atomic
  30. runtime/internal/atomic
  31. internal/itoa
  32. math/bits
  33. internal/abi
  34. runtime/internal/math
  35. runtime/internal/sys
  36. unicode/utf8
  37. unicode
  38. container/list
  39. crypto/internal/subtle
  40. crypto/subtle
  41. unicode/utf16
  42. vendor/golang.org/x/crypto/cryptobyte/asn1
  43. internal/nettrace
  44. vendor/golang.org/x/crypto/internal/subtle
  45. encoding
  46. github.com/mymachine/my-api/internal/controllers/dto
  47. github.com/mymachine/my-api/internal/controllers/helpers
  48. internal/bytealg
  49. math
  50. runtime
  51. internal/reflectlite
  52. sync
  53. internal/testlog
  54. internal/singleflight
  55. math/rand
  56. runtime/cgo
  57. errors
  58. sort
  59. internal/oserror
  60. strconv
  61. path
  62. vendor/golang.org/x/net/dns/dnsmessage
  63. io
  64. crypto/elliptic/internal/fiat
  65. syscall
  66. golang.org/x/text/internal/tag
  67. hash
  68. bytes
  69. strings
  70. hash/crc32
  71. reflect
  72. crypto
  73. crypto/internal/randutil
  74. crypto/hmac
  75. crypto/rc4
  76. net/http/internal/ascii
  77. vendor/golang.org/x/crypto/hkdf
  78. regexp/syntax
  79. github.com/jackc/chunkreader/v2
  80. bufio
  81. crypto/elliptic/internal/nistec
  82. internal/syscall/unix
  83. time
  84. internal/syscall/execenv
  85. vendor/golang.org/x/text/transform
  86. golang.org/x/crypto/pbkdf2
  87. golang.org/x/text/transform
  88. golang.org/x/text/runes
  89. golang.org/x/text/width
  90. regexp
  91. internal/poll
  92. io/fs
  93. context
  94. github.com/mymachine/my-api/internal/entities
  95. github.com/jackc/pgconn/internal/ctxwatch
  96. embed
  97. os
  98. github.com/jinzhu/inflection
  99. github.com/jinzhu/now
  100. internal/fmtsort
  101. encoding/binary
  102. crypto/ed25519/internal/edwards25519/field
  103. crypto/md5
  104. crypto/sha512
  105. crypto/cipher
  106. crypto/sha256
  107. crypto/sha1
  108. encoding/base64
  109. fmt
  110. internal/godebug
  111. encoding/pem
  112. crypto/ed25519/internal/edwards25519
  113. path/filepath
  114. vendor/golang.org/x/crypto/internal/poly1305
  115. io/ioutil
  116. internal/intern
  117. vendor/golang.org/x/crypto/curve25519/internal/field
  118. vendor/golang.org/x/sys/cpu
  119. net/netip
  120. crypto/aes
  121. crypto/des
  122. vendor/golang.org/x/crypto/chacha20
  123. github.com/jackc/pgio
  124. github.com/jackc/pgpassfile
  125. os/exec
  126. os/signal
  127. net
  128. vendor/golang.org/x/crypto/chacha20poly1305
  129. math/big
  130. encoding/hex
  131. net/url
  132. compress/flate
  133. vendor/golang.org/x/crypto/curve25519
  134. log
  135. vendor/golang.org/x/text/unicode/norm
  136. vendor/golang.org/x/text/unicode/bidi
  137. vendor/golang.org/x/net/http2/hpack
  138. mime
  139. mime/quotedprintable
  140. compress/gzip
  141. net/http/internal
  142. database/sql/driver
  143. github.com/mymachine/my-api/internal/pkg/errors
  144. encoding/json
  145. github.com/jackc/pgservicefile
  146. vendor/golang.org/x/text/secure/bidirule
  147. golang.org/x/text/internal/language
  148. database/sql
  149. golang.org/x/text/unicode/norm
  150. golang.org/x/text/unicode/bidi
  151. vendor/golang.org/x/net/idna
  152. os/user
  153. crypto/rand
  154. encoding/asn1
  155. crypto/dsa
  156. crypto/elliptic
  157. crypto/ed25519
  158. crypto/rsa
  159. github.com/jackc/pgproto3/v2
  160. golang.org/x/text/internal/language/compact
  161. golang.org/x/text/secure/bidirule
  162. github.com/jackc/pgx/v4/internal/sanitize
  163. golang.org/x/text/language
  164. go/token
  165. vendor/golang.org/x/crypto/cryptobyte
  166. crypto/x509/pkix
  167. gorm.io/gorm/utils
  168. gorm.io/gorm/logger
  169. encoding/gob
  170. go/scanner
  171. golang.org/x/text/internal
  172. github.com/joho/godotenv
  173. crypto/ecdsa
  174. go/ast
  175. golang.org/x/text/cases
  176. github.com/joho/godotenv/autoload
  177. golang.org/x/text/secure/precis
  178. gorm.io/gorm/clause
  179. gorm.io/gorm/schema
  180. gorm.io/gorm
  181. net/textproto
  182. vendor/golang.org/x/net/http/httpproxy
  183. github.com/google/uuid
  184. crypto/x509
  185. github.com/jackc/pgtype
  186. mime/multipart
  187. vendor/golang.org/x/net/http/httpguts
  188. crypto/tls
  189. gorm.io/gorm/migrator
  190. gorm.io/gorm/callbacks
  191. net/http/httptrace
  192. github.com/jackc/pgconn
  193. net/http
  194. github.com/jackc/pgconn/stmtcache
  195. github.com/jackc/pgx/v4
  196. github.com/jackc/pgx/v4/stdlib
  197. gorm.io/driver/postgres
  198. github.com/mymachine/my-api/internal/infrastructure/repository
  199. github.com/mymachine/my-api/configs
  200. github.com/mymachine/my-api/internal/services
  201. github.com/mymachine/my-api/internal/services/transaction
  202. github.com/mymachine/my-api/internal/controllers/handlers/health
  203. github.com/gorilla/mux
  204. github.com/mymachine/my-api/internal/controllers/handlers/transaction
  205. github.com/mymachine/my-api/internal/router
  206. github.com/mymachine/my-api/cmd
  207. Removing intermediate container d6ae6ce79222
  208. ---> 621aa6a266de
  209. Step 8/9 : EXPOSE 8000
  210. ---> Running in 3cbd17d32c37
  211. Removing intermediate container 3cbd17d32c37
  212. ---> 754f71a7753e
  213. Step 9/9 : CMD ./server
  214. ---> Running in dfcdc8cd45ce
  215. Removing intermediate container dfcdc8cd45ce
  216. ---> 511dbf89a001
  217. Successfully built 511dbf89a001
  218. Successfully tagged my-api_api:latest
  219. Creating my-api_db_1 ... done
  220. Creating golang-api ... done
  221. Attaching to my-api_db_1, golang-api
  222. golang-api | /bin/sh: ./server: not found

答案1

得分: 0

问题已解决。

不知何故,RUN cd cmd 命令似乎没有在预期的目录中生成文件。

尝试使用 RUN go build cmd/main.go 而不是使用 makefile,这也是评论中 @paltaa 指出的方法。

英文:

The problem was solved.

Somehow,the RUN cd cmd command seemed not generating the file in the expected directory.

Trying with RUN go build cmd/main.go instead of using the makefile worked, as well pointed by @paltaa in the comments.

huangapple
  • 本文由 发表于 2022年11月25日 10:09:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/74567721.html
匿名

发表评论

匿名网友

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

确定