duanchendu69495 2016-06-17 13:36
浏览 71

将go app部署到docker时遇到问题

Hi I am pretty new to go, and this is my first time working with docker to package an app into a container. I am working on a linux VM where the app is located under dir: /home/core/app/app-name In the dir app-name there is the main.go program and the Dockerfile. The Dockerfile contains this:

FROM golang:latest
RUN mkdir /app
ADD . /home/core/app/app-name
WORKDIR /app/app-name
RUN go build -o main .
CMD ["/app/main"]
EXPOSE 8080

I have tried running from dir /home/core/app/app-name:

docker build -t app-image .

But I got this error:

can't load package: package .: no buildable Go source files in /app/stars-app The command '/bin/sh -c go build -o main .' returned a non-zero code: 1

What am I doing wrong?

Edit: I got was able to build the image on my windows machine with the Dockfile:

FROM golang:latest
Add . /app/app-name
EXPOSE 8080
CMD ["/app/app-name/main"]

And by running:

docker build -t star-image .

I can see the image when I run "docker images", but when I try to run it using:

docker run -p 3000:8080 --name goapp --rm app-name

I get this error:

docker: Error response from daemon: Container command '/app/app-name/main' not found or does not exist..
  • 写回答

1条回答 默认 最新

  • douzhong3038 2016-06-17 16:13
    关注

    This might work for you...

    • The GOPATH for the image is set to /go
    • install your source(s) under /go/src
    • given the gopath is set and sources are within the GOPATH
    • setting the working directory to /app
    • execute the build and the output should be present in the working directory

    Dockerfile

    FROM golang:latest
    ADD ./app /go/src/app
    RUN mkdir /app
    WORKDIR /app
    RUN go build -o main app/app-name
    CMD ["/app/main"]
    EXPOSE 8080
    

    app/app-name/main.go

    package main
    
    import "fmt"
    
    func main() {
        fmt.Printf("hello, world
    ")
    }
    

    docker build -t app-image .
    docker run app-image
    

    output

    hello, world
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目