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

[examples/dockerdev] Add .dockerignore #26

Closed
FunkyloverOne opened this issue Aug 27, 2020 · 8 comments · Fixed by #27
Closed

[examples/dockerdev] Add .dockerignore #26

FunkyloverOne opened this issue Aug 27, 2020 · 8 comments · Fixed by #27

Comments

@FunkyloverOne
Copy link

Guys, I'm loving your post, but it could be even better for newbies (like me) if it also included .dockerignore file, which is important to make an image-building much faster for bigger projects.

@Envek
Copy link
Member

Envek commented Aug 27, 2020

You don't need .dockerignore for dockerized development because you don't add whole directories into development docker image: it contains only Ruby, dependencies to build your gems, and Nodejs for frontend.

@Envek
Copy link
Member

Envek commented Aug 27, 2020

But you do need .dockerignore for production build. It will vary a lot between projects, but there is more or less generic example:

# for all code you usually don't want .git history in image, just the current commit you have checked out
.git

# you usually don't want dockerfile and compose files in the image either (to not lost caches on not significant changes)
*Dockerfile*
*docker-compose*
.dockerignore

# Local gems (there should be separate `bundle install` step in Dockerfile)
vendor/bundle/

# Specs or tests (may contain large fixtures)
spec/

# Keys for Rails credentials (they should be mounted into container from outside)
config/master.key
config/credentials/*.key

# CI configuration
.circleci/
.github/

# Backend dev tools
.rspec_status
.rubocop*
.bundle/

# running application artifacts
log/
tmp/
public/system/
public/storage/
public/uploads/

# Frontend
public/packs*
node_modules/

# Developer-specific things. So bad that there is no global .dockerignore. See https://github.com/moby/moby/issues/12843
/.idea/

# What else to exclude?
# - Helm chart or Ansible playbooks or anything deployment-specific, not used in production

@FunkyloverOne
Copy link
Author

FunkyloverOne commented Aug 27, 2020

@Envek I disagree.

I know it sounds weird, but since all those folders are inside of build context - all of them are being sent to the docker engine, and it might take a long time.

Note that I do not copy or add anything, my Dockerfile for development is equal to yours - it only sets up the environment.

I thought that I had some issues with docker and it just stuck. But no, I just had to wait like 3 minutes for all the context files, before the build even started. Then I've added a .dockerignore file, similar to yours here, and it started flying!
It gets straight to the building immediately now.

This is because on my project there's a large node_modules tree, also the project is old, so the .git folder is huge too.

(I found that solution here: docker/compose#4396 (comment))

@Envek
Copy link
Member

Envek commented Aug 27, 2020

Ah, yes, you're right! Build context!

Another solution is to limit build context at docker-compose level (see Compose file v2 build section):

--- a/examples/dockerdev/docker-compose.yml
+++ b/examples/dockerdev/docker-compose.yml
@@ -2,8 +2,8 @@ version: '2.4'
 
 x-app: &app
   build:
-    context: .
-    dockerfile: ./.dockerdev/Dockerfile
+    context: .dockerdev
+    dockerfile: Dockerfile
     args:
       RUBY_VERSION: '2.6.3'
       PG_MAJOR: '11'

But that also requires changing .dockerdev/Dockerfile to remove .dockerdev references to current directory:

--- a/examples/dockerdev/.dockerdev/Dockerfile
+++ b/examples/dockerdev/.dockerdev/Dockerfile
@@ -31,7 +31,7 @@ RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
   && echo 'deb http://dl.yarnpkg.com/debian/ stable main' > /etc/apt/sources.list.d/yarn.list
 
 # Install dependencies
-COPY .dockerdev/Aptfile /tmp/Aptfile
+COPY Aptfile /tmp/Aptfile
 RUN apt-get update -qq && DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
   DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
     libpq-dev \

@Envek
Copy link
Member

Envek commented Aug 27, 2020

This is because on my project there's a large node_modules tree, also the project is old, so the .git folder is huge too.

(I found that solution here: docker/compose#4396 (comment))

As usually there is .dockerignore for production Dockerfile, .git and node_modules are also being stripped from .dockerdev/Dockerfile build also. That's probably why people doesn't usually notice it 😃

@FunkyloverOne
Copy link
Author

Yup, so I believe one of those solutions is worth to be added to your awesome post! 😄
However, at least a reference to .dockerignore and why it is important would be very helpful for those who are getting started.

@Envek
Copy link
Member

Envek commented Sep 15, 2020

Also changed the “Ruby on Whales” post accordingly.

@oliverklee
Copy link
Contributor

The changes to the Dockerfile (as mentioned in #26 (comment)) still need to make it into the article.

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 a pull request may close this issue.

3 participants