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

do not patch Dockerfiles in CI #1537

Merged
merged 1 commit into from
Dec 11, 2018
Merged

Conversation

thaJeztah
Copy link
Member

@thaJeztah thaJeztah commented Nov 29, 2018

When building the Dockerfiles for development, those images are mainly used to create a reproducible build-environment. The source code is bind-mounted into the image at runtime; there is no need to create an image with the actual source code, and copying the source code into the image would lead to a new image being created for each code-change (possibly leading up to many "dangling" images for previous code-changes).

However, when building (and using) the development images in CI, bind-mounting is not an option, because the daemon is running remotely.

To make this work, the circle-ci script patched the Dockerfiles when CI is run; adding a COPY to the respective Dockerfiles.

Patching Dockerfiles is not really a "best practice" and, even though the source code does not and up in the image, the source would still be sent to the daemon for each build (unless BuildKit is used).

This patch updates the makefiles, circle-ci script, and Dockerfiles;

  • When building the Dockerfiles locally, pipe the Dockerfile through stdin.
    Doing so, prevents the build-context from being sent to the daemon. This speeds
    up the build, and doesn't fill up the Docker "temp" directory with content that's
    not used
  • Now that no content is sent, add the COPY instructions to the Dockerfiles, and
    remove the code in the circle-ci script to "live patch" the Dockerfiles.

Before this patch is applied (with cache):

$ time make -f docker.Makefile build_shell_validate_image
docker build -t docker-cli-shell-validate -f ./dockerfiles/Dockerfile.shellcheck .
Sending build context to Docker daemon     41MB
Step 1/2 : FROM    debian:stretch-slim
...
Successfully built 81e14e8ad856
Successfully tagged docker-cli-shell-validate:latest

2.75 real         0.45 user         0.56 sys

After this patch is applied (with cache)::

$ time make -f docker.Makefile build_shell_validate_image
cat ./dockerfiles/Dockerfile.shellcheck | docker build -t docker-cli-shell-validate -
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM    debian:stretch-slim
...
Successfully built 81e14e8ad856
Successfully tagged docker-cli-shell-validate:latest

0.33 real         0.07 user         0.08 sys

Signed-off-by: Sebastiaan van Stijn [email protected]

- What I did

- How I did it

- How to verify it

- Description for the changelog

- A picture of a cute animal (not mandatory but encouraged)

@thaJeztah
Copy link
Member Author

ping @ijc @vdemeester @dnephin PTAL

@codecov-io
Copy link

codecov-io commented Nov 29, 2018

Codecov Report

Merging #1537 into master will decrease coverage by <.01%.
The diff coverage is n/a.

@@            Coverage Diff             @@
##           master    #1537      +/-   ##
==========================================
- Coverage   55.22%   55.22%   -0.01%     
==========================================
  Files         289      289              
  Lines       19389    19384       -5     
==========================================
- Hits        10708    10705       -3     
+ Misses       7984     7983       -1     
+ Partials      697      696       -1

Copy link
Collaborator

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

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

LGTM 🐯

@thaJeztah

This comment has been minimized.

When building the Dockerfiles for development, those images are mainly used to
create a reproducible build-environment. The source code is bind-mounted into
the image at runtime; there is no need to create an image with the actual
source code, and copying the source code into the image would lead to a new
image being created for each code-change (possibly leading up to many "dangling"
images for previous code-changes).

However, when building (and using) the development images in CI, bind-mounting
is not an option, because the daemon is running remotely.

To make this work, the circle-ci script patched the Dockerfiles when CI is run;
adding a `COPY` to the respective Dockerfiles.

Patching Dockerfiles is not really a "best practice" and, even though the source
code does not and up in the image, the source would still be _sent_ to the daemon
for each build (unless BuildKit is used).

This patch updates the makefiles, circle-ci script, and Dockerfiles;

- When building the Dockerfiles locally, pipe the Dockerfile through stdin.
  Doing so, prevents the build-context from being sent to the daemon. This speeds
  up the build, and doesn't fill up the Docker "temp" directory with content that's
  not used
- Now that no content is sent, add the COPY instructions to the Dockerfiles, and
  remove the code in the circle-ci script to "live patch" the Dockerfiles.

Before this patch is applied (with cache):

```
$ time make -f docker.Makefile build_shell_validate_image
docker build -t docker-cli-shell-validate -f ./dockerfiles/Dockerfile.shellcheck .
Sending build context to Docker daemon     41MB
Step 1/2 : FROM    debian:stretch-slim
...
Successfully built 81e14e8ad856
Successfully tagged docker-cli-shell-validate:latest

2.75 real         0.45 user         0.56 sys
```

After this patch is applied (with cache)::

```
$ time make -f docker.Makefile build_shell_validate_image
cat ./dockerfiles/Dockerfile.shellcheck | docker build -t docker-cli-shell-validate -
Sending build context to Docker daemon  2.048kB
Step 1/2 : FROM    debian:stretch-slim
...
Successfully built 81e14e8ad856
Successfully tagged docker-cli-shell-validate:latest

0.33 real         0.07 user         0.08 sys
```

Signed-off-by: Sebastiaan van Stijn <[email protected]>
@thaJeztah
Copy link
Member Author

ping @silvin-lubecki @dnephin PTAL 🤗

Copy link
Contributor

@dnephin dnephin left a comment

Choose a reason for hiding this comment

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

LGTM

Interesting solution to the problem. I agree it's an improvement over appending to the Dockerfile.

What I ended up doing in one of my projects to solve this same problem is to make the main image FROM ... as dev and then I added:

FROM dev as dev-with-source
COPY . .

Then I could use --target dev to get a version that still had some context, but not necessarily the full context.

@thaJeztah
Copy link
Member Author

thaJeztah commented Dec 1, 2018 via email

@thaJeztah
Copy link
Member Author

ready for merge? 🤗

Copy link
Collaborator

@vdemeester vdemeester left a comment

Choose a reason for hiding this comment

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

LGTM 🐯

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

Successfully merging this pull request may close these issues.

6 participants