Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow building from source whilst still keeping container size down #10

Merged
merged 7 commits into from
Nov 15, 2018

Conversation

lag-linaro
Copy link
Contributor

Fixes: #7
Fixes: docker-library/official-images#4718

Commit 3246cae (Notary 0.6.1, and rather than commit binaries,
build using multistage dockerfiles.) provided support for building
binaries from source, instead of introducing them into the repo. It
also introduced multi-stage builds into the project.

Multi-stage builds allow us to supply all of the build requirements
into disposable build-containers. Once finished with, only the
required build artifacts are copied out into a nice succinct run-
container. This results in a container with a much smaller
footprint.

The issue is; Docker's Official Images platform does not yet support
multi-stage builds. So until they do, we need to find a source of
compromise.

Here we start with a small base (Alpine), install only the build
requirements we need, build from source, then once complete clean-up
any unnecessary packages. This both allows for building from source
AND keeps the final run-container nice and small.

Signed-off-by: Lee Jones [email protected]

@@ -1,25 +1,35 @@
FROM golang:1.10.3-alpine
FROM alpine:latest
Copy link

Choose a reason for hiding this comment

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

This should be a specific version of Alpine, like 3.8, to prevent possible incompatibilities (like when they switch ssl back to openssl).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. Will fix.

grep -A 5 ${TAG} | grep sha | \
awk '{print substr($2,2,8)}'` \
-X ${NOTARYPKG}/version.NotaryVersion=`cat NOTARY_VERSION`" \
${NOTARYPKG}/cmd/notary-server && \
Copy link
Contributor

Choose a reason for hiding this comment

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

This scraping via wget seems really hacky -- why are we going to all this trouble?

If it's important for version.GitCommit to be set to a value that isn't simple like $TAG (which is technically a valid Git ref, and is the ref we're building from), then why not instead install git, do a git clone --depth 1, and then use the existing Makefile to let the upstream build set the values appropriately itself?

Here's an example Dockerfile I've successfully tested (on both amd64 and arm64) that runs and generates a ~21MB image (~4MB for alpine:3.8 and ~17MB for notary-server), and provides the expected values in notary-server -debug:

FROM alpine:3.8

RUN adduser -D -H -g "" notary

ENV PATH=$PATH:/notary/server
WORKDIR /notary/server

ENV NOTARYPKG github.com/theupdateframework/notary
ENV TAG v0.6.1

RUN set -eux; \
	\
	apk add --no-cache --virtual .build-deps git go make musl-dev; \
	\
	export GOPATH=/go GOCACHE=/go/cache; \
	mkdir -p "$GOPATH/src/$NOTARYPKG"; \
	git clone -b "$TAG" --depth 1 "https://$NOTARYPKG" "$GOPATH/src/$NOTARYPKG"; \
	\
	make -C "$GOPATH/src/$NOTARYPKG" PREFIX=. ./bin/static/notary-server; \
	cp -vL "$GOPATH/src/$NOTARYPKG/bin/static/notary-server" ./; \
	rm -rf "$GOPATH"; \
	\
	apk del --no-network .build-deps

EXPOSE 4443

COPY ./server-config.json .
COPY ./entrypoint.sh .

USER notary

ENTRYPOINT [ "entrypoint.sh" ]
CMD [ "notary-server", "--help" ]

I also don't understand why this and #11 are separate PRs (since it's effectively the same exact change), but the same approach will likely work in exactly the same way over on notary-signer.

Copy link
Contributor

Choose a reason for hiding this comment

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

... the same approach will likely work in exactly the same way over on notary-signer.

Confirmed -- copied, replaced all server with signer and everything appears to work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One step at a time. This PR moves everything in the right direction (removes multi-stage builds) - it does not make things perfect. In the projects I normally work in, commits are expected to contain only one functional change.

They are separate PRs because I wanted to see if the change would be well received before spending even more effort on something that would have been NACKed. Ultimately I'm happy to merge them.

@lag-linaro lag-linaro force-pushed the remove-multi-stage-builds branch from 464cabd to fe39657 Compare November 5, 2018 08:43
Lee Jones added 3 commits November 5, 2018 08:44
…size down

Commit 3246cae (Notary 0.6.1, and rather than commit binaries,
build using multistage dockerfiles.) provided support for building
binaries from source, instead of introducing them into the repo. It
also introduced multi-stage builds into the project.

Multi-stage builds allow us to supply all of the build requirements
into disposable build-containers.  Once finished with, only the
required build artifacts are copied out into a nice succinct run-
container.  This results in a container with a much smaller
footprint.

The issue is; Docker's Official Images platform does not yet support
multi-stage builds.  So until they do, we need to find a source of
compromise.

Here we start with a small base (Alpine), install only the build
requirements we need, build from source, then once complete clean-up
any unnecessary packages.  This both allows for building from source
AND keeps the final run-container nice and small.

Signed-off-by: Lee Jones <[email protected]>
…size down

Commit 3246cae (Notary 0.6.1, and rather than commit binaries,
build using multistage dockerfiles.) provided support for building
binaries from source, instead of introducing them into the repo. It
also introduced multi-stage builds into the project.

Multi-stage builds allow us to supply all of the build requirements
into disposable build-containers.  Once finished with, only the
required build artifacts are copied out into a nice succinct run-
container.  This results in a container with a much smaller
footprint.

The issue is; Docker's Official Images platform does not yet support
multi-stage builds.  So until they do, we need to find a source of
compromise.

Here we start with a small base (Alpine), install only the build
requirements we need, build from source, then once complete clean-up
any unnecessary packages.  This both allows for building from source
AND keeps the final run-container nice and small.

Signed-off-by: Lee Jones <[email protected]>
@yosifkit
Copy link

yosifkit commented Nov 6, 2018

LGTM from the official-images side.

@lag-linaro
Copy link
Contributor Author

Thanks for your time @yosifkit. Much appreciated.

@lag-linaro lag-linaro mentioned this pull request Nov 6, 2018
@lag-linaro
Copy link
Contributor Author

@justincormack now that the Official Images guys are happy, would you mind re-reviewing please?

@@ -1,25 +1,28 @@
FROM golang:1.10.3-alpine
FROM alpine:3.8
Copy link
Member

Choose a reason for hiding this comment

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

I think its better to use eg golang/1.10.5-alpine3.8 so we pin the Go version and the Alpine version, rather than installing the Go that Alpine happens to have at present.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

By doing so you will increase the size of the image from 21MB to 268MB.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We could do 'apk add go=1.10.5' if you'd prefer?

Copy link
Member

Choose a reason for hiding this comment

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

Ah ugh yes. I am just trying to get Alpine updated with the latest Go 1.10 patch release, as it is currently only on 1.10.1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That would be a better solution.

Although it doesn't seem to affect Notary. It tested just fine with Apline 3.8.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So when installing this PR I see that Apline 3.8 installs Go version 1.10.5-r0.

(19/22) Installing go (1.10.5-r0)

Is that not adequate?

Copy link
Member

@justincormack justincormack Nov 15, 2018

Choose a reason for hiding this comment

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

Yes, Its fixed now, I got an updated version pushed yesterday, all good...

@justincormack justincormack merged commit 6db2078 into docker:master Nov 15, 2018
@lag-linaro
Copy link
Contributor Author

lag-linaro commented Nov 15, 2018

Thanks @justincormack, you're a star.

Do you want me to raise a PR on Official Images saying that we now support AArch64?

I'm happy to do it if you do not have time. Can tag you as a reviewer too.

@lag-linaro
Copy link
Contributor Author

It's also required to bump to 0.6.1.

Official Images is still at 0.5.0.

@justincormack
Copy link
Member

@lag-linaro I think I need to re-tag the v0.6.1 release that we did in this repo, as it has the previous version in. And not sure exactly what we need in the replacement for docker-library/official-images#4718 maybe @tianon or @yosifkit can help...

@lag-linaro
Copy link
Contributor Author

@justincormack this and #11 PRs fix @tianon and @yosifkit reviews.

We can now re-submit docker-library/official-images#4718.

This is what I was proposing in #10 (comment).

@justincormack
Copy link
Member

I think docker-library/official-images#4718 would at least need to list the architectures to build for. Let me re-tag this repo as well now.

@lag-linaro
Copy link
Contributor Author

Absolutely. In order to provide additional arches, we need to supply an Architectures: tag.

@lag-linaro
Copy link
Contributor Author

See HERE

Let me know if you'd like me to submit it.

@justincormack
Copy link
Member

Ok, I pushed a new v0.6.1 tag to this repo and deleted the old one, so yes that should be good to submit I think.

@lag-linaro
Copy link
Contributor Author

Great. That's sent.

It will help expedite the process if you could review and provide your LGTM.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants