英文:
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 ["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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论