How can I import a private GitHub repository during a Docker build?

0
18
Asked By CuriousCoder42 On

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?

4 Answers

Answered By NodeNinja On

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.

Answered By DockerGuru On

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.

Answered By DevDude123 On

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.

Answered By TechieTom On

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!

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.