I've been trying for some time to cache node_modules
on a Docker build. I've tried several approaches including the one here, but without success.
My main reason to cache is because it takes 30+ minutes to build my image, which is way too much.
My Dockerfile
:
# This image will be based on the oficial nodejs docker image
FROM node:4.2.1
RUN npm install -g [email protected] && \
npm install -g gulp && \
npm install -g tsd
# Use changes to package.json to force Docker not to use the cache
# when we change our application's nodejs dependencies:
ADD package.json /src/package.json
RUN cd /src && npm install
# Put all our code inside that directory that lives in the container
ADD . /src
# Set in what directory commands will run
WORKDIR /src
# Install dependencies
RUN cd /src && \
tsd reinstall -so && \
jspm install && \
gulp build -p
# Tell Docker we are going to use this port
EXPOSE 3000
# The command to run our app when the container is run
CMD ["npm", "run", "start-production"]
I do not have a .dockerignore
file. I added one before but it still didn't cache my node_modules
.
So, how to I cache my node_modules? Feel free to suggest modifications to the Dockerfile
.
Thanks!
As I understand the documentation about caching both in this repo and in the upstream actions/cache repo, the reason node_modules is not recommended to cache is because dependencies can break across versions of Node. js.
Docker's build-cache is a handy feature. It speeds up Docker builds due to reusing previously created layers. You can use the --no-cache option to disable caching or use a custom Docker build argument to enforce rebuilding from a certain step.
To avoid the npm install phase on every docker build just copy those lines and change the ^/opt/app^ to the location your app lives inside the container. That works.
About the Docker Build CacheDocker images are built in layers, where each layer is an instruction from a Dockerfile. Layers stack on top of each other, adding functionality incrementally. The build process knew the Dockerfile didn't change, so it used the cache from the last build for all four layers.
I'm not sure whether it is the root of the error, but try to specify the destination folder in the ADD command and not the destination file.
ADD package.json /src
Moreover, you can use COPY instead of ADD (ADD can work with url and archives but you don't need it here).
You can also specify your working directory earlier in the file.
Try with this code :
# This image will be based on the official nodejs docker image
FROM node:4.2.1
RUN npm install -g [email protected] && \
npm install -g gulp && \
npm install -g tsd
# Set in what directory commands will run
WORKDIR /src
# Use changes to package.json to force Docker not to use the cache
# when we change our application’s nodejs dependencies:
COPY package.json ./
RUN npm install
# Put all our code inside that directory that lives in the container
COPY . ./
# Install dependencies
RUN tsd reinstall -so && \
jspm install && \
gulp build -p
# Tell Docker we are going to use this port
EXPOSE 3000
# The command to run our app when the container is run
CMD ["npm", "run", "start-production"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With