在使用MySQL驱动程序构建我的Golang应用程序时,在Docker中出现错误。

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

Error when trying to build my golang application in docker while using the mysql driver

问题

我有一个简单的应用程序,使用github.com/go-sql-driver/mysql连接到MySQL数据库并执行简单的查询。在我的本地机器上一切正常,但是当我尝试使用docker build构建时,我得到以下输出:

[+] Building 4.1s (9/10)
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 104B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/golang:onbuild                                                  1.3s
 => [auth] library/golang:pull token for registry-1.docker.io                                                      0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 5.63kB                                                                                0.0s
 => CACHED [1/2] FROM docker.io/library/golang:onbuild@sha256:c0ec19d49014d604e4f62266afd490016b11ceec103f0b7ef44  0.0s
 => [2/2] COPY . /go/src/app                                                                                       0.1s
 => [3/2] RUN go-wrapper download                                                                                  2.0s
 => ERROR [4/2] RUN go-wrapper install                                                                             0.6s
------
 > [4/2] RUN go-wrapper install:
#8 0.465 + exec go install -v
#8 0.535 github.com/joho/godotenv
#8 0.536 github.com/go-sql-driver/mysql
#8 0.581 # github.com/go-sql-driver/mysql
#8 0.581 ../github.com/go-sql-driver/mysql/driver.go:88: undefined: driver.Connector
#8 0.581 ../github.com/go-sql-driver/mysql/driver.go:99: undefined: driver.Connector
#8 0.581 ../github.com/go-sql-driver/mysql/nulltime.go:36: undefined: sql.NullTime
------
executor failed running [/bin/sh -c go-wrapper install]: exit code: 2

我的go版本是最新的,我使用以下dockerfile:

FROM golang:onbuild

据我所知,这应该会go get所需的所有包。我也尝试过这种方式:

FROM golang:onbuild
RUN go get "github.com/go-sql-driver/mysql"

这个输出结果相同。请注意,在我的代码中,我这样导入包:

import _ "github.com/go-sql-driver/mysql"

我还使用其他来自github的包,它们似乎工作正常。

英文:

I have a simple application that uses github.com/go-sql-driver/mysql to connect to a MySQL database and execute simple queries. This all works fine on my local machine, however when I try to build it using docker build I get the following output:

[+] Building 4.1s (9/10)
 => [internal] load build definition from Dockerfile                                                               0.0s
 => => transferring dockerfile: 104B                                                                               0.0s
 => [internal] load .dockerignore                                                                                  0.0s
 => => transferring context: 2B                                                                                    0.0s
 => [internal] load metadata for docker.io/library/golang:onbuild                                                  1.3s
 => [auth] library/golang:pull token for registry-1.docker.io                                                      0.0s
 => [internal] load build context                                                                                  0.0s
 => => transferring context: 5.63kB                                                                                0.0s
 => CACHED [1/2] FROM docker.io/library/golang:onbuild@sha256:c0ec19d49014d604e4f62266afd490016b11ceec103f0b7ef44  0.0s
 => [2/2] COPY . /go/src/app                                                                                       0.1s
 => [3/2] RUN go-wrapper download                                                                                  2.0s
 => ERROR [4/2] RUN go-wrapper install                                                                             0.6s
------
 > [4/2] RUN go-wrapper install:
#8 0.465 + exec go install -v
#8 0.535 github.com/joho/godotenv
#8 0.536 github.com/go-sql-driver/mysql
#8 0.581 # github.com/go-sql-driver/mysql
#8 0.581 ../github.com/go-sql-driver/mysql/driver.go:88: undefined: driver.Connector
#8 0.581 ../github.com/go-sql-driver/mysql/driver.go:99: undefined: driver.Connector
#8 0.581 ../github.com/go-sql-driver/mysql/nulltime.go:36: undefined: sql.NullTime
------
executor failed running [/bin/sh -c go-wrapper install]: exit code: 2

My go version is up to date and I am using the following dockerfile:

FROM golang:onbuild

To my knowledge this should go get all the packages it requires. I've also tried it this way:

FROM golang:onbuild
RUN go get "github.com/go-sql-driver/mysql"

This had the same output.
Note that in my code I import the package like this:

import _ "github.com/go-sql-driver/mysql"

I also use other packages from github, these seem to work fine.

答案1

得分: 2

Docker社区通常不再使用Dockerfile中的ONBUILD指令,因为它会让派生镜像中将会发生什么变得非常混乱(请参阅关于“这真的是整个Dockerfile吗?”的各种评论)。如果你在Docker Hub上搜索golang:onbuild镜像,你会发现这是Go 1.7或1.8版本;Go模块是在Go 1.11中引入的。

你需要更新到一个更新的基础镜像,这意味着需要手动编写Dockerfile的步骤。对于一个典型的Go应用程序,它应该是这样的:

FROM golang:1.18 AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY ./ ./
RUN go build -o myapp .

FROM ubuntu:20.04
COPY --from=build /app/myapp /usr/local/bin
CMD ["myapp"]

(在最后一个阶段,你可能需要运行RUN apt-get update && apt-get install ...来安装MySQL客户端库或其他工具。)

英文:

The Docker community has generally been steering away from the Dockerfile ONBUILD directive, since it makes it very confusing what will actually happen in derived images (see the various comments around "is that really the entire Dockerfile?"). If you search Docker Hub for the golang:onbuild image you'll discover that this is Go 1.7 or 1.8; Go modules were introduced in Go 1.11.

You'll need to update to a newer base image, and that means writing out the Dockerfile steps by hand. For a typical Go application this would look like

FROM golang:1.18 AS build
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY ./ ./
RUN go build -o myapp .

FROM ubuntu:20.04
COPY --from=build /app/myapp /usr/local/bin
CMD ["myapp"]

(In the final stage you may need to RUN apt-get update && apt-get install ... a MySQL client library or other tools.)

huangapple
  • 本文由 发表于 2022年4月25日 16:23:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/71996513.html
匿名

发表评论

匿名网友

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

确定