我正在测试Docker和Go项目。这是我的Dockerfile:
FROM golang
ARG app_env
ENV APP_ENV $app_env
COPY ./ /go/src/github.com/user/myProject/app
WORKDIR /go/src/github.com/user/myProject/app
RUN go get ./
RUN go build
CMD if [ ${APP_ENV} = production ]; \
then \
app; \
else \
go get github.com/pilu/fresh && \
fresh; \
fi
EXPOSE 8080
程序本来运行良好。之后我向我的Go程序中添加了一个名为"testpack"的包。
package main
import(
"fmt"
"time"
"testpack"
)
var now = time.Now()
var election = time.Date(2016, time.November, 8, 0, 0, 0, 0, time.UTC)
func main() {
//get duration between election date and now
tillElection := election.Sub(now)
//get duration in nanoseconds
toNanoseconds := tillElection.Nanoseconds()
//calculate hours from toNanoseconds
hours := toNanoseconds/3600000000000
remainder := toNanoseconds%3600000000000
//derive minutes from remainder of hours
minutes := remainder/60000000000
remainder = remainder%60000000000
//derive seconds from remainder of minutes
seconds := remainder/1000000000
//calculate days and get hours left from remainder
days := hours/24
hoursLeft := hours%24
fmt.Printf("\nHow long until the 2016 U.S. Presidential election?\n\n%v Days %v Hours %v Minutes %v Seconds\n\n", days, hoursLeft, minutes, seconds)
}
现在我运行命令 => docker build ./
我遇到了一个错误
包 testpack:无法识别的导入路径“testpack”(导入路径不以主机名开头)
我尝试了这个链接 Error 'import path does not begin with hostname' when building docker with local package,但是无法解决问题
非常感谢您的帮助。
testpack
在应用程序目录中,则不能作为导入路径。 导入路径相对于$GOPATH/src
-- 请参见如何编写Go代码。 - JimB