-
-
Notifications
You must be signed in to change notification settings - Fork 35
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
Use NODE_ENV ARG to control NPM install and tests #130
Conversation
By using the `--build-arg` parameter to `docker build`, we can control environment variables at build time. Couple with changes like those in pelias/placeholder#130, we can build images with different settings in different cases. In this example, I've chosen to set up the `production` images to skip installing dev dependencies, making the images quite a bit leaner.
I actually like the idea of running tests when building the image, simply because they ensure that the environment is valid. Eg. If I dev a feature on my mac, all the tests pass, I generate the image successfully and push it to Totally fine with removing the dev dependencies 👍 |
We can avoid downloading and installing the |
We need the devDependencies to run the tests though. The workflow I had in mind was one where only the production images skip the tests/devDependencies. Since we always merge master->staging->production, there would be equivalent[1] images with/without the devDependencies/tests run for any change. This ensures there exists a minimal, fast, secure image for production use, but also that there is a dev image for deeper exploration. [1] The commit hash isn't identical right now, because we generate merge commits when going from master->staging and staging->production. With a little work we could do away with that, and then it would be very clear that two images are identical except for missing dev dependencies |
By using the Dockerfile [ARG](https://docs.docker.com/engine/reference/builder/#arg) directive, we can set an ENV variable and control it via the `--build-arg` parameter to `docker build`. Using this we can configure the Dockerfile to skip installing NPM dev dependencies and running unit tests. In my testing this reduces the size of the Placeholder docker images from 290MB to 200MB, which is pretty huge! It reduces the time to build the image, reduces our surface area for security issues, and seems like an all around win.
4d8a72c
to
ada9fd4
Compare
So the issue we are trying to solve here is to not include dev dependencies in the docker image? I would tend towards something that is very simple, and having different environments during dev and production doesn't sound like a good idea to me. If the problem we are trying to solve is the size of the dev dependencies then the simplest solution without compromising any functionality and also not not adding any complexity to the workflow is simply:
|
That's true, that would be simple. A downside there however, is we can't use our current system where This would make local development significantly slower: every rebuild of an image locally would have to pay the cost of As it stands, with only code changed, the time to build a new docker image is approximately equal to the time to run I also think there is value in having docker images with testing tools, as well as value having docker images that are as lean as possible. Having both lets us optimize for different situations. |
It seems that once the jshint issue (referenced above) is resolved, and we roll out the changes to remove semantic-release from devDependencies (pelias/api#1187), this may not matter as much, as the total size of the devDependencies might not be much larger than installing only dependencies. |
By using the Dockerfile ARG directive, we can set an ENV variable and control it via the
--build-arg
parameter todocker build
.Using this we can configure the Dockerfile to skip installing NPM dev dependencies and running unit tests.
In my testing this reduces the size of the Placeholder docker images from 290MB to 200MB, which is pretty huge! It reduces the time to build the image, reduces our surface area for security issues, and seems like an all around win.