Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access private git repos via npm install in a Docker container

I am in the process of setting up a Docker container that will pull private repos from GitHub as part of the process. At the moment I am using an Access Token that I pass from the command line (will change once build gets triggered via Jenkins).

docker build -t my-container --build-arg GITHUB_API_TOKEN=123456 .

# Dockerfile
# Env Vars
ARG GITHUB_API_TOKEN
ENV GITHUB_API_TOKEN=${GITHUB_API_TOKEN}

RUN git clone https://${GITHUB_API_TOKEN}@github.com/org/my-repo

This works fine and seems to be a secure way of doing this? (though need to check the var GITHUB_API_TOKEN only being available at build time)

I am looking to find out how people deal with ssh keys or access tokens when running npm install and dependencies pull from github

"devDependencies": {
  "my-repo": "[email protected]:org/my-repo.git",
  "electron": "^1.7.4"
}

At the moment I cannot pull this repo as I get the error Please make sure you have the correct access rights as I have no ssh keys setup in this container

like image 403
Richlewis Avatar asked Jul 18 '17 10:07

Richlewis


People also ask

Can I use Git in a Docker container?

Even if you are running your project on Docker, you can still access your git account inside Docker Containers. All you need to do is just install Git inside your Docker Container.

How do I deploy a private repository in GitHub?

To register the repository SSH key with your private repository on GitHub, go to the Settings for the repository. On GitHub the repository SSH key is referred to by the term Deploy key. Search down the settings page and find the Deploy keys section and select it. Click on the Add deploy key button.


2 Answers

Use the multi-stage build approach.

Your Dockerfile should look something like this:

FROM alpine/git as base_clone
ARG GITHUB_API_TOKEN
WORKDIR /opt
RUN git clone https://${GITHUB_API_TOKEN}@github.com/org/my-repo

FROM <whatever>
COPY --from=base_clone /opt/my-repo /opt
...
...
...

Build:

docker build -t my-container --build-arg GITHUB_API_TOKEN=123456 .

The Github API Token secret won't be present in the final image.

like image 81
Robert Avatar answered Sep 21 '22 19:09

Robert


docker secrets is a thing, but it's only available to containers that are part of a docker swarm. It is meant for handling things like SSH keys. You could do as the documentation suggests and create a swarm of 1 to utilize this feature.

docker-compose also supports secrets, though I haven't used them with compose.

like image 38
bluescores Avatar answered Sep 20 '22 19:09

bluescores