How can I import a private GitHub repository during a Docker build?
Asked by
Dibber
I'm trying to install a code library from a private repository on GitHub during a Docker build, but I'm running into issues. My current package.json looks like this: "dependencies": { "my-utilities": "github:MYORG/my-utilities" }. Here's my Dockerfile:
```
FROM public.ecr.aws/amazonlinux/amazonlinux:2023
RUN dnf update -y && dnf install -y awscli jq nodejs22
WORKDIR /
COPY package.json /
COPY index.js /
ARG GITHUB_PAT
RUN npm config set "@MYORG:registry" https://npm.pkg.github.com
RUN npm config set "//npm.pkg.github.com:_authToken" "${GITHUB_PAT}"
RUN npm i
CMD ["node", "index.js"]
```
When I try building with `docker build --build-arg GITHUB_PAT="github_pat_XXXXXX" -t utilities-test .`, I get an error:
```
npm error code ENOENT
npm error syscall spawn git
npm error path git
npm error errno -2
npm error enoent An unknown git error occurred
```
Running `npm i` from the command line works fine, so I suspect something's not right with the .npmrc configuration during the build. Any ideas on how to fix this?
Answers
It looks like you're missing Git in your base image, which is why you're getting that error. You should install Git as part of your Docker build. You could modify your Dockerfile to include:
```
RUN dnf install -y git
```
This should help with the cloning issue. Just remember, you may run into more problems down the line, but fixing this should be a good start!
Reply to this answer
Your `npmrc` configuration and the way you're trying to pull the GitHub repo are conflicting. The way you currently have it set up indicates to `npm` to clone it directly, which ignores any auth tokens you set in `.npmrc`. If you want to keep pulling from Git, consider setting up a `.netrc` file with your credentials instead.
Reply to this answer
It's generally unsafe to use ARG for secrets since they can end up in your image history. Look into using `--secret` mounts for better security during your builds.
Reply to this answer
Why not publish your package to GitHub Packages directly? You can change your dependency in `package.json` to use a standard package format like "@MYORG/my-utilities": "^1.0.0" instead. This way, your existing npm config would work seamlessly.
Reply to this answer
Add your answer
Answers are reviewed before publication.
Related Discussions