Golang应用在Docker容器中的部署

huangapple go评论156阅读模式
英文:

Golang app deployment in docker container

问题

我需要将Go Restful应用部署到Docker容器中。

我的项目结构如下:

go_proj

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

我的Dockerfile应该是什么样子的?
也许我应该在容器中下载库(mux)吗?

英文:

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

得分: 0

我有一些使用Docker部署Go应用程序的经验。

TL;DR

这是我的项目在${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

Dockerfile的内容如下:

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"]

大致如此。

长篇回答

一般来说,我喜欢将Dockerfile放在服务器目录中:

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

如果我在项目中使用非标准的包(如gorilla/mux),我可能会使用vndr来创建一个vendor目录,将所有这些包放在其中并管理它们的版本。

然后,我的Dockerfile可能会像这样:

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

注意:你可能希望自动暴露端口号。例如,添加以下行将在容器内部将端口8080暴露给主机。

PORT 8080

剩下的工作就是从go/src/github.com/mygithub/serverproject目录中构建镜像:

docker build -t serverproject .

然后将其部署到容器中:

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

如果你想向服务器传递参数,可以像下面这样做:

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

注意:如果你没有在Dockerfile中指定PORT并希望将其暴露给主机,请在--name之前将-P替换为-p,并指定<host-port>:<container-port>

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

这将实际上将主机上的端口转发到容器中。

英文:

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 [&quot;go-wrapper&quot;, &quot;run&quot;, &quot;main.go&quot;]

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 [&quot;go-wrapper&quot;, &quot;run&quot;, &quot;main.go&quot;]

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 &lt;host-port&gt;:&lt;container-port&gt;:

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

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

huangapple
  • 本文由 发表于 2017年8月30日 05:23:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/45948537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定