drol55885602 2017-08-29 21:23
浏览 177
已采纳

在Docker容器中部署Golang应用程序

i need to deploy go restful app into Docker container.

My project has the following structure

go_proj

  |- bin
  |- src |
  |      |- com.example |
  |      |              |- web_service.go
  |      |
  |      |- github.com.gorilla.mux |
  |                                | - ...
  |- Dockerfile

How should my Dockerfile look like? And maybe I should download the library(mux) from the container?

  • 写回答

1条回答 默认 最新

  • dptsivmg82908 2017-08-30 08:13
    关注

    I got some experience with deploying Go apps using Docker.

    TL;DR

    This is how my project looks like in ${GOPATH}/src/github.com/githubhandle/project

    main.go
    vendor.conf
    vendor/
        github.com/gorilla/mux
        github.com/sirupsen/logrus
        github.com/bshuster-repo/logrus-logstash-hook
        ...
    Dockerfile
    

    And the Dockerfile looks like this:

    FROM golang:1.8
    WORKDIR /go/src/app
    COPY . .
    RUN go-wrapper download
    RUN go-wrapper install
    EXPOSE [8080]
    ENTRYPOINT ["go-wrapper", "run", "main.go"]
    

    Pretty much

    Long answer

    In general, I like to put my Dockerfile inside the server directory:

    go/
        src/
            github.com/
                mygithub/
                    serverproject/
                        main.go
                        Dockerfile
    

    If I am using non standard packages in my projects such as (gorilla/mux) I would probably use vndr to create a vendor directory where I put all these packages and manage their versions.

    After that my Dockerfile would probably look like that:

    FROM golang:1.8
    WORKDIR /go/src/app
    COPY . .
    RUN go-wrapper download
    RUN go-wrapper install
    ENTRYPOINT ["go-wrapper", "run", "main.go"]
    

    Note You may want to automatically expose port number. For example, adding this line will expose port 8080 inside the container to the host.

    PORT 8080
    

    What is left to do is to build the image from within go/src/github.com/mygithub/serverproject:

    docker build -t serverproject .
    

    And to deploy it into a container:

    docker run -P --rm --name myserver1 serverproject
    

    If you want to pass arguments to the server you can do that like the following:

    docker run -P --rm --name myserver1 serverproject --arg1 --arg2=bla
    

    Note in case you didn't specify PORT in your Dockerfile and would like to expose it to the host, replace -P with -p before --name and specify <host-port>:<container-port>:

    docker run --rm -p 8080:8080 ...
    

    This will actually port forward from your host to the container.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来