Skip to content

Instantly share code, notes, and snippets.

@adriancmiranda
Forked from eschwartz/Dockerfile
Created April 8, 2020 16:23
Show Gist options
  • Save adriancmiranda/cb9b939850810b9e750ed09eb116fe0a to your computer and use it in GitHub Desktop.
Save adriancmiranda/cb9b939850810b9e750ed09eb116fe0a to your computer and use it in GitHub Desktop.
npm install from private repo, in docker build
# Add these lines to your dockerfile, before `npm install`
# Copy the bitbucket private key to your docker image
COPY ./bitbucket_ssh_key /opt/my-app
# Copy the ssh script to your docker image
COPY ./ssh-bitbucket.sh /opt/my-app
# Tell git to use your `ssh-bitbucket.sh` script
ENV GIT_SSH="/opt/map-project-tile-server/ssh-bitbucket.sh"
RUN npm install
# Remove the private key once npm install is complete
# To previous any nefarious activities
RUN rm ./bitbucket_ssh_key

Goal: Install a package from a private bitbucket repository, using npm, from within a docker build script.

Steps:

  • Add a project from a private repo to your package.json. For example: git+ssh://[email protected]/hamweather/private-repo
  • Generate ssh keys using ssh-keygen (see https://confluence.atlassian.com/bitbucket/set-up-ssh-for-git-728138079.html)
  • Save the public key to the bitbucket repo you want to install (see Settings > Deployment Keys)
  • Commit the private key to your repo
  • Commit the ssh-bitbucket.sh script to your repo
  • Add executable permissions to ssh-bitbucket.sh
  • Set permissionson the private key to 0600
  • Update your Dockerfile, as described below
#!/usr/bin/env bash
# http://superuser.com/questions/232373/how-to-tell-git-which-private-key-to-use/920849#920849
ssh -i /opt/my-app/bitbucket_ssh_key \
-o UserKnownHostsFile=/dev/null \
-o StrictHostKeyChecking=no \
$*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment