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

Dockerignore: convert to whitelist #361

Draft
wants to merge 3 commits into
base: next
Choose a base branch
from

Conversation

alxndrsn
Copy link
Contributor

@alxndrsn alxndrsn commented Dec 1, 2022

No description provided.

@alxndrsn alxndrsn changed the base branch from master to next December 1, 2022 13:46
Copy link
Member

@matthew-white matthew-white left a comment

Choose a reason for hiding this comment

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

What will happen to the version files if we make this change? I'm thinking that they'll identify the excluded files as changes to the working directory and append -dirty to the version, which probably isn't what we want.

What do you think about running git clean instead at some point during the build? That way we could remove things like node_modules without causing a change that would show up in the version files.

.dockerignore Show resolved Hide resolved
.dockerignore Outdated

# Re-include the things we actually want

# .git dirs: required to generate version files in service container
Copy link
Member

Choose a reason for hiding this comment

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

Also required to generate version.txt in the nginx container (files/prebuild/write-version.sh I believe).

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, added to comment

@@ -1,2 +1,22 @@
node_modules
Copy link
Member

Choose a reason for hiding this comment

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

If we already had node_modules in here, how was it copied through during the build of that one server? Does .dockerignore work differently from .gitignore in that way?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes 😞

.dockerignore

the patterns /foo/bar and foo/bar both exclude a file or directory named bar in the foo subdirectory of PATH or in the root of the git repository located at URL. Neither excludes anything else.
-- https://docs.docker.com/engine/reference/builder/#dockerignore-file

.gitignore

If there is a separator at the beginning or middle (or both) of the pattern, then the pattern is relative to the directory level of the particular .gitignore file itself. Otherwise the pattern may also match at any level below the .gitignore level.
-- https://git-scm.com/docs/gitignore#_pattern_format

Probably good to include the preceding / in .dockerignore to make the intent more explicit.

Copy link
Member

Choose a reason for hiding this comment

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

Could we make it something like this:

/server/node_modules
/server/npm-debug.log

/client/node_modules
/client/npm-debug.log

Actually, could that change be all we need? Then an errant node_modules directory wouldn't be copied through.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would be an alternative approach, but I think with a blacklisting approach there is more scope for accidentally including unwanted files in the future.

@alxndrsn
Copy link
Contributor Author

alxndrsn commented Dec 6, 2022

What will happen to the version files if we make this change? I'm thinking that they'll identify the excluded files as changes to the working directory and append -dirty to the version, which probably isn't what we want.

What do you think about running git clean instead at some point during the build? That way we could remove things like node_modules without causing a change that would show up in the version files.

Very interesting point. I don't think we can reliably keep .dockerignore and .gitignore in sync, especially using a whitelist in one and not the other.

Maybe the real issue in the node_modules case is that we carefully populate that directory:

COPY server/package*.json ./
RUN npm install --production
RUN npm install pm2 -g

and then subsequently overwrite that with whatever the user has locally:

COPY server/ ./

😰

@alxndrsn
Copy link
Contributor Author

alxndrsn commented Dec 6, 2022

COPY server/ ./

might be better replaced with:

COPY server/config config/
COPY server/lib lib/

@matthew-white
Copy link
Member

matthew-white commented Jan 6, 2023

Maybe the real issue in the node_modules case is that we carefully populate that directory:

COPY server/package*.json ./
RUN npm install --production
RUN npm install pm2 -g

and then subsequently overwrite that with whatever the user has locally:

COPY server/ ./


COPY server/ ./

might be better replaced with:

COPY server/config config/
COPY server/lib lib/

That change sounds sensible to me. I worry a little about the day when we add a new directory besides config/ and lib/, but it'd be easy enough to add it here if/when we do.

Another option to consider: what if copy everything, but do so before the npm clean-install? Something like:

-COPY server/package*.json ./
+COPY server/ ./
 
 RUN npm clean-install --omit=dev --legacy-peer-deps
 RUN npm install pm2@5.2.0 -g
 
-COPY server/ ./

Then even if node_modules is initially copied, it will be replaced by npm clean-install.

@alxndrsn
Copy link
Contributor Author

Then even if node_modules is initially copied, it will be replaced by npm clean-install.

I don't think this would pick up manual changes made in node_modules, so still leaves scope for unpredictable builds. Also including node_modules in the first place will increase the size of the build context.

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.

None yet

2 participants