简体   繁体   中英

$GOPATH/go.mod exists but should not on Docker

I'm trying to build a Docker container that contains both MySQL and a Go server.

Just importing the MySQL libraries cause the error.

Any pointers to what I'm doing wrong?

This is the terminal after build:

Successfully built 5d5356e2ca72
Successfully tagged test3:latest
$GOPATH/go.mod exists but should not

But there is no go.mod in the $GOPATH

$ ls -a $GOPATH
.  ..  bin  pkg

This is the go server:

package main

import (
    "database/sql"
    "flag"
    "fmt"
    "log"
    "net/http"
    "strings"

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

type FileSystem struct {
    fs http.FileSystem
}

var listen = flag.String("listen", ":8989", "listen address")
var dir = flag.String("dir", ".", "directory to serve")

func main() {
    db()
    flag.Parse()
    directory := "./"

    fileServer := http.FileServer(FileSystem{http.Dir(directory)})
    http.Handle("/", fileServer)

    fmt.Printf("Web server running. Listening on %q", *listen)
    err := http.ListenAndServe(*listen, http.FileServer(http.Dir(*dir)))
    fmt.Printf("%v\n", err)
}

func (fs FileSystem) Open(path string) (http.File, error) {
    f, err := fs.fs.Open(path)
    if err != nil {
        return nil, err
    }

    s, err := f.Stat()
    if s.IsDir() {
        index := strings.TrimSuffix(path, "/") + "/index.html"
        if _, err := fs.fs.Open(index); err != nil {
            return nil, err
        }
    }

    return f, nil
}

func db() {
    db, err := sql.Open("mysql", "root:root@tcp(192.168.0.33:4200)/mysql")
    if err != nil {
        log.Print(err.Error())
    } else {
        log.Print("DB connected successfully")
    }

    defer db.Close()
}

This is my go.mod

module test3

go 1.14

require github.com/go-sql-driver/mysql v1.5.0

This is the Dockerfile

FROM golang:alpine AS builder

ENV GO111MODULE=auto \
    CGO_ENABLED=0 \
    GOOS=linux \
    GOARCH=amd64   

COPY . .
COPY ./server.go .
COPY ./favicon.ico .
COPY ./assets /assets
EXPOSE 8989
CMD ["go", "run", "./server.go"]

And this is how I build the container:

docker rmi test3 -f
docker build --no-cache -t test3 . 
docker run -p 8989:8989 test3

$GOPATH/go.mod exists but should not

By default the goland:alpine image starts the working directory in the GOPATH (ie /go ) folder where globally installed packages are created with the go install command.

The folder structure looks like:

go/

  • bin/
  • src/
  • pkg/

By directly copying go.mod into the working directory (ie GOPATH, ie /go ), the golang toolkit throws an error, because go.mod is supposed to be in a package folder.

Before the release of Go modules in version 1.11, the convention was to create your packages in the /go/src folder. However, with the release of Go modules you can create golang packages anywhere as long as they include a go.mod file.

So to fix the error, you could copy the golang files ( go.mod , etc) into a folder under /home/app or /go/src/app .

Copy into /home/app

RUN mkdir ../home/app 
WORKDIR /../home/app
COPY . .

Copy into /go/src/app

RUN mkdir src/app
WORKDIR /src/app
COPY . .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM