dsbqfrr098575666 2015-05-14 16:42
浏览 20
已采纳

在Docker for Go程序中构建Docker

I'm trying to build a very small Docker container for a Go program. To do that I'm hoping to use a Docker container to build the Go program then (within the container) build another Docker container to run the program.

Here's my build Dockerfile:

FROM google/golang
WORKDIR /gopath/src/mycompany/mygoprog

# Build the Contributors application
RUN mkdir -p /gopath/src/mycompany/mygoprog
ADD ./src/* /gopath/src/mycompany/mygoprog/
RUN CGO_ENABLED=0 GOOS=linux go build -a -tags rs -ldflags '-w' /gopath/src/mycompany/mygoprog/*.go
RUN cp mygoprog /mygoprog
CMD docker build -t foobar /gopath/src/mycompany/mygoprog

My source directory has my Go program (trivial--compiles/runs) and this Dockerfile for the final product:

FROM scratch
ADD /mygoprog mygoprog
ENTRYPOINT ["/mygoprog"]
EXPOSE 9100

These files are organized thusly:

/myproject
   Dockerfile
   /src
      Dockerfile
      mygoprog.go

To launch the build I go to the top directory and run:

docker build .

This runs without errors....and no final image! Output looks like this:

bash-3.2$ docker build .
Sending build context to Docker daemon 6.656 kB
Sending build context to Docker daemon 
Step 0 : FROM google/golang
 ---> 3cc1d7ae0e9c
Step 1 : WORKDIR /gopath/src/mycompany/mygoprog
 ---> Using cache
 ---> 49b9686dcbed
Step 2 : RUN mkdir -p /gopath/src/mycompany/mygoprog
 ---> Using cache
 ---> 0d0139bb8cae
Step 3 : ADD ./src/* /gopath/src/mycompany/mygoprog/
 ---> 964ea5ca6afb
Removing intermediate container e57719a417d5
Step 4 : RUN CGO_ENABLED=0 GOOS=linux go build -a -tags rs -ldflags '-w' /gopath/src/mycompany/mygoprog/*.go
 ---> Running in 608ad65db52c
 ---> 6e91f06b7654
Removing intermediate container 608ad65db52c
Step 5 : RUN cp mygoprog /mygoprog
 ---> Running in 196af3db881b
 ---> 5e90e11ffc7d
Removing intermediate container 196af3db881b
Step 6 : CMD docker build -t foobar /gopath/src/mycompany/mygoprog
 ---> Running in 4794354abf2b
 ---> d91a908ee663
Removing intermediate container 4794354abf2b
Successfully built d91a908ee663

What am I missing so I get a final, runnable Docker with my compiled program inside?

  • 写回答

1条回答 默认 最新

  • drpsrvu85668 2015-05-14 16:56
    关注

    You've produced docker image, but as you haven't specified tag for it is accessible only by hash (d91a908ee663). It is better to specify tag: docker build -t mygoprog . And if you will try to run your image without additional setting it will fail because there is no docker inside of it. Easiest solution here to give access to your docker daemon from container:

    docker run -v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker mygoprog
    

    Docker location could be different in your distro, you also may need --privileged flag.

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

报告相同问题?