Go build fails when building docker image










3















I'm a little new to golang and I'm still trying to get my head around the difference between go run main.go and go build [-o] main.go.



I've build a little gin app to try out locally with docker and kubernetes.



package main

import (
"net/http"

"github.com/gin-gonic/gin"
)

func main()

r := gin.Default()

r.GET("/healthz", func(c *gin.Context)
c.String(http.StatusOK, "")
)

r.GET("/readinez", func(c *gin.Context)
c.String(http.StatusOK, "")
)

r.Run() // listen and serve on 0.0.0.0:8080



The app runs perfectly fine with go run main.go.



My Dockerfile:



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


It fails:



enter image description here



It is definitely in there and it also works when I go run main.go. What is the difference to build?



I'm not sure what to do here. Coming from a node background. This does drive a noobie somewhat mad... Sure there is an easy solution.










share|improve this question




























    3















    I'm a little new to golang and I'm still trying to get my head around the difference between go run main.go and go build [-o] main.go.



    I've build a little gin app to try out locally with docker and kubernetes.



    package main

    import (
    "net/http"

    "github.com/gin-gonic/gin"
    )

    func main()

    r := gin.Default()

    r.GET("/healthz", func(c *gin.Context)
    c.String(http.StatusOK, "")
    )

    r.GET("/readinez", func(c *gin.Context)
    c.String(http.StatusOK, "")
    )

    r.Run() // listen and serve on 0.0.0.0:8080



    The app runs perfectly fine with go run main.go.



    My Dockerfile:



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


    It fails:



    enter image description here



    It is definitely in there and it also works when I go run main.go. What is the difference to build?



    I'm not sure what to do here. Coming from a node background. This does drive a noobie somewhat mad... Sure there is an easy solution.










    share|improve this question


























      3












      3








      3








      I'm a little new to golang and I'm still trying to get my head around the difference between go run main.go and go build [-o] main.go.



      I've build a little gin app to try out locally with docker and kubernetes.



      package main

      import (
      "net/http"

      "github.com/gin-gonic/gin"
      )

      func main()

      r := gin.Default()

      r.GET("/healthz", func(c *gin.Context)
      c.String(http.StatusOK, "")
      )

      r.GET("/readinez", func(c *gin.Context)
      c.String(http.StatusOK, "")
      )

      r.Run() // listen and serve on 0.0.0.0:8080



      The app runs perfectly fine with go run main.go.



      My Dockerfile:



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


      It fails:



      enter image description here



      It is definitely in there and it also works when I go run main.go. What is the difference to build?



      I'm not sure what to do here. Coming from a node background. This does drive a noobie somewhat mad... Sure there is an easy solution.










      share|improve this question
















      I'm a little new to golang and I'm still trying to get my head around the difference between go run main.go and go build [-o] main.go.



      I've build a little gin app to try out locally with docker and kubernetes.



      package main

      import (
      "net/http"

      "github.com/gin-gonic/gin"
      )

      func main()

      r := gin.Default()

      r.GET("/healthz", func(c *gin.Context)
      c.String(http.StatusOK, "")
      )

      r.GET("/readinez", func(c *gin.Context)
      c.String(http.StatusOK, "")
      )

      r.Run() // listen and serve on 0.0.0.0:8080



      The app runs perfectly fine with go run main.go.



      My Dockerfile:



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


      It fails:



      enter image description here



      It is definitely in there and it also works when I go run main.go. What is the difference to build?



      I'm not sure what to do here. Coming from a node background. This does drive a noobie somewhat mad... Sure there is an easy solution.







      docker go






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 11:57









      mbuechmann

      2,88721426




      2,88721426










      asked Nov 25 '17 at 14:01









      TinoTino

      1,28331948




      1,28331948






















          3 Answers
          3






          active

          oldest

          votes


















          2














          The program succeeds on your machine because you probably have the gin package installed. You can't assume a container will have it, and should install it explicitly. Just add the following line to your dockerfile before the go build line:



          RUN go get github.com/gin-gonic/gin





          share|improve this answer

























          • Thx that worked. How can one set this up more efficiently in the go world? Its not really practical to add every package manually to docker. Any good ideas articles/tutorials I could read?

            – Tino
            Nov 25 '17 at 14:29






          • 2





            One way is to vendor the gin-gonic package. Basicly means to copy the package over to your folder as a dependency. Please do look at dep and glide which should be the succesors of govendoring.

            – Hace
            Nov 25 '17 at 15:42






          • 1





            Incidentally, you can just run a naked go get to pull all dependencies. That said, you (OP) may want to look into vendoring (blog.gopheracademy.com/advent-2015/vendor-folder). It solves both this issue and problems that arise from differing dependency versions. (Edit: damn, beat me to it, Hace)

            – Kaedys
            Nov 25 '17 at 15:42



















          0














          It may have failed because you used gin, and the library cannot be found inside the container. Try to use glide or godep to vendoring third party library.






          share|improve this answer






























            0














            go get github.com/gin-gonic/gin


            Then it should work.






            share|improve this answer






















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47486811%2fgo-build-fails-when-building-docker-image%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              2














              The program succeeds on your machine because you probably have the gin package installed. You can't assume a container will have it, and should install it explicitly. Just add the following line to your dockerfile before the go build line:



              RUN go get github.com/gin-gonic/gin





              share|improve this answer

























              • Thx that worked. How can one set this up more efficiently in the go world? Its not really practical to add every package manually to docker. Any good ideas articles/tutorials I could read?

                – Tino
                Nov 25 '17 at 14:29






              • 2





                One way is to vendor the gin-gonic package. Basicly means to copy the package over to your folder as a dependency. Please do look at dep and glide which should be the succesors of govendoring.

                – Hace
                Nov 25 '17 at 15:42






              • 1





                Incidentally, you can just run a naked go get to pull all dependencies. That said, you (OP) may want to look into vendoring (blog.gopheracademy.com/advent-2015/vendor-folder). It solves both this issue and problems that arise from differing dependency versions. (Edit: damn, beat me to it, Hace)

                – Kaedys
                Nov 25 '17 at 15:42
















              2














              The program succeeds on your machine because you probably have the gin package installed. You can't assume a container will have it, and should install it explicitly. Just add the following line to your dockerfile before the go build line:



              RUN go get github.com/gin-gonic/gin





              share|improve this answer

























              • Thx that worked. How can one set this up more efficiently in the go world? Its not really practical to add every package manually to docker. Any good ideas articles/tutorials I could read?

                – Tino
                Nov 25 '17 at 14:29






              • 2





                One way is to vendor the gin-gonic package. Basicly means to copy the package over to your folder as a dependency. Please do look at dep and glide which should be the succesors of govendoring.

                – Hace
                Nov 25 '17 at 15:42






              • 1





                Incidentally, you can just run a naked go get to pull all dependencies. That said, you (OP) may want to look into vendoring (blog.gopheracademy.com/advent-2015/vendor-folder). It solves both this issue and problems that arise from differing dependency versions. (Edit: damn, beat me to it, Hace)

                – Kaedys
                Nov 25 '17 at 15:42














              2












              2








              2







              The program succeeds on your machine because you probably have the gin package installed. You can't assume a container will have it, and should install it explicitly. Just add the following line to your dockerfile before the go build line:



              RUN go get github.com/gin-gonic/gin





              share|improve this answer















              The program succeeds on your machine because you probably have the gin package installed. You can't assume a container will have it, and should install it explicitly. Just add the following line to your dockerfile before the go build line:



              RUN go get github.com/gin-gonic/gin






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 25 '17 at 14:29

























              answered Nov 25 '17 at 14:26









              MureinikMureinik

              183k22135200




              183k22135200












              • Thx that worked. How can one set this up more efficiently in the go world? Its not really practical to add every package manually to docker. Any good ideas articles/tutorials I could read?

                – Tino
                Nov 25 '17 at 14:29






              • 2





                One way is to vendor the gin-gonic package. Basicly means to copy the package over to your folder as a dependency. Please do look at dep and glide which should be the succesors of govendoring.

                – Hace
                Nov 25 '17 at 15:42






              • 1





                Incidentally, you can just run a naked go get to pull all dependencies. That said, you (OP) may want to look into vendoring (blog.gopheracademy.com/advent-2015/vendor-folder). It solves both this issue and problems that arise from differing dependency versions. (Edit: damn, beat me to it, Hace)

                – Kaedys
                Nov 25 '17 at 15:42


















              • Thx that worked. How can one set this up more efficiently in the go world? Its not really practical to add every package manually to docker. Any good ideas articles/tutorials I could read?

                – Tino
                Nov 25 '17 at 14:29






              • 2





                One way is to vendor the gin-gonic package. Basicly means to copy the package over to your folder as a dependency. Please do look at dep and glide which should be the succesors of govendoring.

                – Hace
                Nov 25 '17 at 15:42






              • 1





                Incidentally, you can just run a naked go get to pull all dependencies. That said, you (OP) may want to look into vendoring (blog.gopheracademy.com/advent-2015/vendor-folder). It solves both this issue and problems that arise from differing dependency versions. (Edit: damn, beat me to it, Hace)

                – Kaedys
                Nov 25 '17 at 15:42

















              Thx that worked. How can one set this up more efficiently in the go world? Its not really practical to add every package manually to docker. Any good ideas articles/tutorials I could read?

              – Tino
              Nov 25 '17 at 14:29





              Thx that worked. How can one set this up more efficiently in the go world? Its not really practical to add every package manually to docker. Any good ideas articles/tutorials I could read?

              – Tino
              Nov 25 '17 at 14:29




              2




              2





              One way is to vendor the gin-gonic package. Basicly means to copy the package over to your folder as a dependency. Please do look at dep and glide which should be the succesors of govendoring.

              – Hace
              Nov 25 '17 at 15:42





              One way is to vendor the gin-gonic package. Basicly means to copy the package over to your folder as a dependency. Please do look at dep and glide which should be the succesors of govendoring.

              – Hace
              Nov 25 '17 at 15:42




              1




              1





              Incidentally, you can just run a naked go get to pull all dependencies. That said, you (OP) may want to look into vendoring (blog.gopheracademy.com/advent-2015/vendor-folder). It solves both this issue and problems that arise from differing dependency versions. (Edit: damn, beat me to it, Hace)

              – Kaedys
              Nov 25 '17 at 15:42






              Incidentally, you can just run a naked go get to pull all dependencies. That said, you (OP) may want to look into vendoring (blog.gopheracademy.com/advent-2015/vendor-folder). It solves both this issue and problems that arise from differing dependency versions. (Edit: damn, beat me to it, Hace)

              – Kaedys
              Nov 25 '17 at 15:42














              0














              It may have failed because you used gin, and the library cannot be found inside the container. Try to use glide or godep to vendoring third party library.






              share|improve this answer



























                0














                It may have failed because you used gin, and the library cannot be found inside the container. Try to use glide or godep to vendoring third party library.






                share|improve this answer

























                  0












                  0








                  0







                  It may have failed because you used gin, and the library cannot be found inside the container. Try to use glide or godep to vendoring third party library.






                  share|improve this answer













                  It may have failed because you used gin, and the library cannot be found inside the container. Try to use glide or godep to vendoring third party library.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '17 at 14:20









                  ngurajekangurajeka

                  463




                  463





















                      0














                      go get github.com/gin-gonic/gin


                      Then it should work.






                      share|improve this answer



























                        0














                        go get github.com/gin-gonic/gin


                        Then it should work.






                        share|improve this answer

























                          0












                          0








                          0







                          go get github.com/gin-gonic/gin


                          Then it should work.






                          share|improve this answer













                          go get github.com/gin-gonic/gin


                          Then it should work.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 25 '17 at 14:26









                          Huang MingheHuang Minghe

                          237411




                          237411



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f47486811%2fgo-build-fails-when-building-docker-image%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              這個網誌中的熱門文章

                              Barbados

                              How to read a connectionString WITH PROVIDER in .NET Core?

                              Node.js Script on GitHub Pages or Amazon S3