Search code examples
dockergomysql-connector

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


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.


Solution

  • 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.)