Skip to content

Instantly share code, notes, and snippets.

@dlants
Created March 9, 2023 01:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dlants/cb9c2dcd9cafee929cc4706c1bc5eb5d to your computer and use it in GitHub Desktop.
Save dlants/cb9c2dcd9cafee929cc4706c1bc5eb5d to your computer and use it in GitHub Desktop.
yarn in docker build dockerfile w/ private github repo & cache mount
# for yarn berry
FROM --platform=linux/amd64 node:14.21-bullseye
# install node_module packages needed to build
WORKDIR /app
COPY .yarnrc.yml .
COPY .yarn/releases/yarn-3.4.1.cjs .yarn/releases/yarn-3.4.1.cjs
COPY package.json .
COPY yarn.lock .
# local deps & preinstall scripts must be in place before running yarn install
# COPY local packages, scripts
# the app/.yarn/cache directory will be preserved between builds, to take advantage of the yarn cache.
# the --mount=type=ssh flag allows this command to use $SSH_AUTH_SOCKET provided by the docker build command (see below)
# yarn asks git to check out the code for the private repo. git, in turn, sshes into github.
# Since the docker container is a new host, there are no known_hosts, so by default, ssh will try and confirm the new host
# by opening a tty. This will fail.
# We remove the need for this confirmation by telling git to run ssh with the StrictHostKeyChecking=no flag.
RUN --mount=type=ssh,required=true \
--mount=type=cache,id=yarn,target=/app/.yarn/cache \
GIT_SSH_COMMAND="ssh -o StrictHostKeyChecking=no" yarn install
#!/bin/bash
echo $SSH_AUTH_SOCK # should print out something to confirm ssh-agent is running
docker build --ssh default .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment