Skip to content
This repository has been archived by the owner on Sep 9, 2020. It is now read-only.

Document how to use dep with Docker #1094

Merged
merged 7 commits into from
Sep 19, 2017
34 changes: 34 additions & 0 deletions docs/FAQ.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Summarize the question and quote the reply, linking back to the original comment
* [How does `dep` handle symbolic links?](#how-does-dep-handle-symbolic-links)
* [Does `dep` support relative imports?](#does-dep-support-relative-imports)
* [How do I make `dep` resolve dependencies from my `GOPATH`?](#how-do-i-make-dep-resolve-dependencies-from-my-gopath)
* [How do I use `dep` with Docker?](#how-do-i-use-dep-with-docker)

## Best Practices
* [Should I commit my vendor directory?](#should-i-commit-my-vendor-directory)
Expand Down Expand Up @@ -288,6 +289,39 @@ For a refresher on Go's recommended workspace organization, see the ["How To Wri
`dep init -gopath`, which falls back to network mode when the packages are not
found in `GOPATH`. `dep ensure` doesn't work with projects in `GOPATH`.

## How do I use `dep` with Docker?

`dep ensure -vendor-only` creates the vendor folder from a valid `Gopkg.toml` and `Gopkg.lock` without checking for Go code.
This is especially useful for builds inside docker utilizing cache layers.

Sample dockerfile:

FROM golang:1.8 AS builder

RUN curl -fsSL -o /usr/local/bin/dep https://github.com/golang/dep/releases/download/vX.X.X/dep-linux-amd64 && chmod +x /usr/local/bin/dep

RUN mkdir -p /go/src/github.com/***
WORKDIR /go/src/github.com/***

COPY Gopkg.toml Gopkg.lock ./
# copies the Gopkg.toml and Gopkg.lock to WORKDIR

RUN dep ensure -vendor-only
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment that explains that at this point, vendor/ has been cached.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added more comments to explain

# install the dependencies without checking for go code
COPY . .
# copies any other required code to the WORKDIR
RUN go build -o ***
# the -o flag is used to specify the name of the executable to be installed

FROM alpine:latest
RUN apk --no-cache add ca-certificates

WORKDIR /root/

COPY --from=builder /go/src/github.com/*** .
# copies only the built executable to the new image
ENTRYPOINT ["./***"]


## Best Practices
### Should I commit my vendor directory?
Expand Down