-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
[Documentation]: Recommended way to handle yarn and docker #749
Comments
For actually installing Yarn in a Docker or Vagrant image, you can use the Debian package repo (assuming you're using a Ubuntu or Debian Docker image). Enabling the package repo then doing As for mounting the cache, that's a pretty good idea, I'm not too sure how to do it though (I'm not very familiar with Docker myself). |
You wouldn't mount the Yarn cache directory. Instead, you should make sure you take advantage of Docker's image layer caching. These are the commands I am using:
|
It would depend on the environment and approach you want to take to building your assets. repeated docker build@kyteague's approach is one you could take if you didn't want to use a global cache and instead just cache the project's dependencies in a higher docker layer. (ie: if your development is going to be running manual docker runA more sophisticated approach for development is to run a development container (
Typically I combine the docker run approach with some bash and volume mount caches in. Then at the end I copy my assets out of the container and ship them to S3, etc or yarn on hostYou could also docker-compose (development)If your project has a "watch mode" script, you can use a docker-compose file to alleviate some of the concerns of the "repeated docker build" approach by running something along the lines of So really it depends on your goals and build environment ("watch" development with Docker for Mac/CI from a dev image to an alpine-based prod image/etc). |
@kyteague and @ChristopherBiscardi, great comments! I think it would be valuable to add a page to the documentation around best practices for using Yarn in Docker. Would you like to write a page about "Using Yarn in Docker" for our documentation? The website is in a separate repo: https://github.com/yarnpkg/website |
Ideally, docker could use an external cache but there's some resistance to that ( moby/moby#17745 ), so adding a |
This is the fastest setup I've get so far as Dockerfile
entrypoint.sh
docker-compose.yml
|
@rstuven That will do the |
@daveisfera Yes, that's why I stressed on its "fastest" quality. I missed to point out this is rather for development workflow where fast iterations matter most. On the other hand, the yarn.lock file should guarantee the reproducible aspect, but yes, it's not enough. |
Another approach: https://medium.com/@mfornasa/using-yarn-with-docker-c116ad289d56 |
Am I wrong to assume that Yarn's global cache stores the package zips (which contain cross-platform code by which I mean it will be compiled at install-time)? If the global cache only contains the downloaded archives and no build artifacts, would we not be able to at least mount the host's global cache so that the Docker container wouldn't have to download them? |
@kyteague |
Looks like there are plenty of ways now. |
another way is to mitm yarn traffic with caching proxy and self-signed cert using
|
For productionCOPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile --no-cache --production Note: You don't want dev dependencies in a production image, also you need to make sure that Yarn's cache folder is not bundled into the image. For test and CICOPY package.json yarn.lock ./
RUN yarn install --frozen-lockfile Note: In a test / CI environment you still want to install NPM modules via Docker builder in order to utilize Docker layer caching. The next time your image is being built on a CI server, these two steps will be skipped in favor of using an existing layer, unless either For local developmentCOPY package.json yarn.lock ./ Note: In development mode (locally) it would be faster to install NPM modules at run-time, this way you can attach a volume with Yarn cache to your container. The approach above can be implemented by using a single FROM node:8.9.1-alpine
ARG NODE_ENV=production
ENV NODE_ENV=$NODE_ENV
# Set a working directory
WORKDIR /usr/src/app
# Install native dependencies
# RUN set -ex; \
# apk add --no-cache ...
# Install Node.js dependencies
COPY package.json yarn.lock ./
RUN set -ex; \
if [ "$NODE_ENV" = "production" ]; then \
yarn install --no-cache --frozen-lockfile --production; \
elif [ "$NODE_ENV" = "test" ]; then \
yarn install --no-cache --frozen-lockfile; \
fi;
... Note: It's better to install native dependencies, if any, via a separate
|
You can also do it in a multi stage Dockerfile for production, like the following for something that runs as a static front end and doesn't need node/yarn at runtime:
Note: Maybe with |
The only way I can find to not have an extra 100MB of cache is to do this on latest version of yarn (1.5.1).
|
Just in case, there's no |
from this comment I like the concise nature of using /dev/shm as a volatile storage of the cache |
This is a modified version of the changes in PR #2. I copied the installation arguments from this GitHub issue[1]. [1]: yarnpkg/yarn#749 (comment)
@jeremejevs to prevent the yarn cache from winding up in docker layers, we would need to have them together in a single |
@jedwards1211 That is correct, yes. |
A tad off topic but I'm doing |
AFAICT yarn still doesn't have a |
Yes, |
we may want to add --frozen-lockfile to our yarn installs as well as run yarn clean cache to reduce image size e.g., in production: `yarn install --frozen-lockfile --production && yarn cache clean` https://stackoverflow.com/questions/44552348/should-i-commit-yarn-lock-and-package-lock-json-files yarnpkg/yarn#749
Goal of this issue is to update the readme file how to use yarn and docker.
I want a docker image to build my node projects - and use yarn to install the packages as the installation is much faster (and more deterministic) than using npm.
One reason why yarn is fast is of course the local yarn cache. So the docker image needs to mount the yarn cache directory when building the projects. Any other hints how to use docker and yarn?
The text was updated successfully, but these errors were encountered: