Я целый день искал, что здесь происходит, и, похоже, все должно работать без проблем согласно документации Docker и golang.
Прежде всего ошибка:
holms@debian ~/D/c/s/app-prebid-config> compose -f compose.yml build app-prebid-config
Building app-prebid-config
Step 1/11 : FROM golang:1.14-alpine as dev
---> 51e47ee4db58
Step 2/11 : RUN apk update && apk upgrade && apk add --no-cache bash git
---> Using cache
---> 8ddb9b995df8
Step 3/11 : RUN mkdir -p /app/prebid-config
---> Using cache
---> 4c1e8f03b27c
Step 4/11 : WORKDIR /app/prebid-config
---> Using cache
---> 25104c791d7e
Step 5/11 : RUN go mod init setupad.com/prebid-config
---> Using cache
---> 0b64b735c1f1
Step 6/11 : RUN go mod download
---> Using cache
---> 7170aa6447c9
Step 7/11 : RUN go mod vendor
---> Using cache
---> 0bbc220e43b9
Step 8/11 : COPY . .
---> Using cache
---> 9d5ab625ccd0
Step 9/11 : RUN go get -d -v ./...
---> Using cache
---> d4057dd0d9a7
Step 10/11 : RUN go install -v ./...
---> Running in c5b7a8cbc33e
main.go:11:8: package aerospike_shared is not in GOROOT (/usr/local/go/src/aerospike_shared)
ERROR: Service 'app-prebid-config' failed to build: The command '/bin/sh -c go install -v ./...' returned a non-zero code: 1
Dockerfile:
holms@debian ~/D/c/s/a/app> cat Dockerfile
FROM golang:1.14-alpine as dev
ENV GO111MODULE=auto
ENV CGO_ENABLED=0
ENV GOOS=linux
RUN apk update && apk upgrade && apk add --no-cache bash git
RUN mkdir -p /app/prebid-config
WORKDIR /app/prebid-config
COPY go.mod .
RUN go mod download
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
RUN go build -a -installsuffix cgo -o app .
FROM alpine:latest as prod
RUN apk --no-cache add ca-certificates
COPY --from=dev /app/app /app
EXPOSE 8080
CMD ["/app"]
Код сам:
holms@debian ~/D/c/s/a/app> tree
.
├── aerospike_shared
│ └── client.go
├── Dockerfile
└── main.go
1 directory, 3 files
main. go
holms@debian ~/D/c/s/a/app> cat main.go
package main
import (
"net/http"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/labstack/gommon/log"
)
import "aerospike_shared"
func main() {
// Echo instance
e := echo.New()
e.Logger.SetLevel(log.ERROR)
e.Use(middleware.Logger())
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
// Connect to Aerospike
a = aerospike_shared.client
// Start server
e.Logger.Fatal(e.Start(":1323"))
}
aerospike_shared / client. go
holms@debian ~/D/c/s/a/app> cat aerospike_shared/client.go
package aerospike_shared
import (
"bytes"
"fmt"
"log"
"math"
"os"
"runtime"
"strconv"
as "github.com/aerospike/aerospike-client-go"
)
var WritePolicy = as.NewWritePolicy(0, 0)
var Policy = as.NewPolicy()
var Host = os.Getenv("AEROSPIKE_HOST")
var Port, PortErr = strconv.Atoi(os.Getenv("AEROSPIKE_PORT"))
var Namespace = "prebid"
var Set = "prebid_configs"
var client *as.Client
func main() {
// use all cpus in the system for concurrency
runtime.GOMAXPROCS(runtime.NumCPU())
log.SetOutput(os.Stdout)
var err error
client, err = as.NewClient(Host, Port)
if err != nil {
PanicOnError(err)
}
}
func PanicOnError(err error) {
if err != nil {
log.Fatalln(err.Error())
}
}