Disable cache for a build step

In some edge-cases you might want to disable cache for a build step in the Dockerfile, for instance if using gatsby and fetching external data which might have changed since you last built the image. This is not supported by Docker, but we have enabled a hack that makes it possible. Note that this will disable cache for that ann all subsequent build steps.

When building your docker-file, we set a build argument, BUILD_DATE_USED_FOR_CACHE_BUSTING to the current ISO-8601 timestamp. This can be used in a build-step, and since that will change for each build, the cache will never be re-used.

Example Dockerfile:

# This is the build-container, we need node to build
FROM node:16 as build

# Set NODE_ENV to production to create a production-build of react
ENV NODE_ENV production

# Create a directory to use for building
RUN mkdir /app
# Set the build-directory as the working (and current) directory
WORKDIR /app

# We start by building just dependencies, this means that we can use cached dependencies
# if these files are not changed
COPY gatsby-site/package.json .
# We are using yarn. If you use npm, this would be package-lock.json
COPY gatsby-site/yarn.lock .
# Remove this if you are not using typescript. (but you should use typescript)
COPY gatsby-site/tsconfig.json .
COPY gatsby-site/create-env.js .
# Install dependencies
RUN yarn install --pure-lockfile

# Copy the actual code and public (static files)
COPY gatsby-site /app
# Build the dependencies. This will output to `/app/build` the static files which need to be served
# to the user
RUN node /app/create-env.js

# This is set by iterapp to the current datetime
ARG BUILD_DATE_USED_FOR_CACHE_BUSTING=not_set

# This one is required to bust the cache. Required because yarn build fetch data from sanity, and
# Docker does not know if that has changed
RUN echo "Build date: $BUILD_DATE_USED_FOR_CACHE_BUSTING"

# This will never be cached
RUN yarn build


# We don't need the node-container in production, we just need something that can serve the static
# files to the user. Nginx is really good at this. `FROM` starts a new container
FROM nginx:1.21.1-alpine

# We copy the built files from the build-container. These files are in `/app/build` after the
# build-step above.
COPY --from=build /app/public /usr/share/nginx/html