如何将命令行选项传递给我的dockerized GoLang程序?

时间:2015-04-15 20:19:41

标签: unix go docker

我有一个简单的dockerized golang程序,我希望能够在运行容器时向它发送命令行选项。

我的Dockerfile如下所示:

FROM golang:onbuild
RUN go get [MY PROGRAM]

4 个答案:

答案 0 :(得分:1)

现在你只是在正在运行的容器中获取源代码然后退出。 一旦你构建了程序,你需要用你想要的参数运行你的程序,或者运行你将从中调用程序的shell。 我希望我提供的这个Dockerfile可以作为指导。

我在本地有一个导出GOPATH GOBIN和PATH的bashrc,或者你可以 使用dockerfile中的ENV语句。我也经常退房 我的程序的工作副本,我在其中构建图像并将其复制到容器中。这个设置只是为了给你解决未来问题的想法。

export GOPATH="/root/go"
export GOBIN="/root/go/bin"
export PATH="/root/go/bin:$PATH"

然后我的Dockerfile是

FROM ubuntu:14.04
MAINTAINER foo, bar@baz.org
COPY bashrc /root/.bashrc
COPY MYPROGRAM /root/go/src/MYPROGRAM
ENV GOBIN /root/go/bin
ENV GOPATH /root/go
ENV PATH /root/go/bin:$PATH
ENV HOME /root
WORKDIR /root
RUN \
  apt-get update && \
  apt-get install -y golang git && \
  mkdir -p /root/go/src && \
  mkdir -p /root/go/bin && \
  go get DEPENDENCIES

RUN go install /root/go/src/MYPROGRAM/program.go
ENTRYPOINT ["program"]

现在容器的入口点是你的程序

docker run img args-to-your-prog

您也可以将参数添加到ENTRYPOINT语句中,如

ENTRYPOINT["program", "arg1", ...]

或者您可以使用bash作为入口点并将您的程序作为参数

docker run img program arg1 ...

ENTRYPOINT是正在执行的程序,如果设置了CMD将是 args传递给它。 CMD是容器的默认参数。如果 ENTRYPOINT没有设置它们CMD将是直接执行的命令。

答案 1 :(得分:0)

添加以下行应该有效: ENTRYPOINT ["go", "run", "yourapp"],请参阅Docker DocGo commandline example

然后,您只需在docker run call结束时输入go程序的参数,例如: docker run mygoapp arg1 arg2。然后应该将参数传递给你的go程序。

答案 2 :(得分:0)

只要您的应用程序按照Go约定编译为单个二进制文件,您就应该能够在下面使用这个简单的双线程,在ENTRYPOINT指令中将任何后续标志作为类似数组的参数传递。

FROM golang:onbuild
ENTRYPOINT ["/go/bin/app", "-name=foo", "-title=bar"]

答案 3 :(得分:0)

这是我使用的食谱的简化版本:

[sorens | ~/src/go/hello> ls -al
total 16
drwxr-xr-x   4 sorens  staff  128 Aug  1 21:46 .
drwxr-xr-x  11 sorens  staff  352 Jun 25 13:21 ..
-rw-r--r--@  1 sorens  staff  118 Aug  1 21:43 Dockerfile
-rw-r--r--   1 sorens  staff  153 Aug  1 15:42 hello.go

Dockerfile:

FROM golang:1.12.7
WORKDIR /go/src/hello
COPY . .
RUN go get -d -v ./...
RUN go install -v ./...
ENTRYPOINT ["hello"]

hello.go

package main

import "os"
import "fmt"

func main() {
    name := "world"
    if (len(os.Args) > 1) {
        name = os.Args[1]
    }
    fmt.Printf("hello, %s\n", name)
}

在本地编译并运行代码以查看其工作原理:

[sorens | ~/src/go/hello> go build hello.go
[sorens | ~/src/go/hello> ./hello
hello, world
[sorens | ~/src/go/hello> ./hello there
hello, there

现在,在Docker中构建它:

[sorens | ~/src/go/hello> docker build -t hw .
Sending build context to Docker daemon  2.128MB
Step 1/6 : FROM golang:1.12.7
1.12.7: Pulling from library/golang
5ae19949497e: Pull complete 
ed3d96a2798e: Pull complete 
f12136850781: Pull complete 
1a9ad5d5550b: Pull complete 
efbd5496b163: Pull complete 
c01c378f53ca: Pull complete 
c7aef280980d: Pull complete 
Digest: sha256:f5486a917b57f8b14be4345604bc4654147416a327d6d63271a0c52c907001c4
Status: Downloaded newer image for golang:1.12.7
 ---> be63d15101cb
Step 2/6 : WORKDIR /go/src/hello
 ---> Running in 4f362ab0be79
Removing intermediate container 4f362ab0be79
 ---> 47a247a9b603
Step 3/6 : COPY . .
 ---> b1e53c8f9166
Step 4/6 : RUN go get -d -v ./...
 ---> Running in 309331f04485
Removing intermediate container 309331f04485
 ---> 088f49d8ee5f
Step 5/6 : RUN go install -v ./...
 ---> Running in 83e5937ece78
hello
Removing intermediate container 83e5937ece78
 ---> 8cd1d6ffefa8
Step 6/6 : ENTRYPOINT ["hello"]
 ---> Running in dae06fb3343e
Removing intermediate container dae06fb3343e
 ---> c04a6307cdc8
Successfully built c04a6307cdc8
Successfully tagged hw:latest

并运行它:

[sorens | ~/src/go/hello> docker run -it --rm hw
hello, world
[sorens | ~/src/go/hello> docker run -it --rm hw there
hello, there

我将其发布在github上:https://github.com/sorens/cli-golang-via-docker

相关问题