简体   繁体   中英

Get all dependent packages of Go inside docker

I can get all my dependent packages when I do the following:

$ cd myrepo
$ go get -d ./...

But what is the best way to do this in docker? I don't want all the repo's inside my docker-app so I'm searching for a way to execute this inside a docker container to some volume (or something like that) and reuse it.

I was thinking about something like. Dockerfile

FROM golang:1.8

WORKDIR /app
ADD ./src

Build image

$ docker build -t myapp .

$ docker run myapp go get -d ./...

How can I reuse the dependencies without pulling/downloading them every time? I want them in a sort of volume. I know docker volumes a bit but I don't know how to use it in this case.

The best way to do this is to use the builder pattern. In the first image you download all the dependencies and build the executable. In the second you copy the executable into a new image. You can use the scratch image as a base, but alpine is maybe better because it is also small but provides a shell and a packet manager for something like certificates for https.

If you first copy the sources and the install the dependencies, they will be downloaded with every source change, so it ist better to use something like go dep. With it you can copy the Gopkg file, install your dependencies and then copy the sources.

Official article https://docs.docker.com/engine/userguide/eng-image/multistage-build/ and in more detail https://blog.alexellis.io/mutli-stage-docker-builds/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM