Shortcuts: Pull a git repository in a Dockerfile
docker node.js

Shortcuts: Pull a git repository in a Dockerfile

Marcell Simon
Marcell Simon

A sample node.js application for the demo:

Other than the Dockerfile, the most important thing is the ssh folder with the keys. You need to generate an ssh key (`ssh-keygen`) and add the key to GitLab, so you can use it to access your repository.

The Dockerfile:

FROM ubuntu:latest

RUN apt-get update && \
    apt-get install -y git

# Authorize SSH Host
RUN mkdir -p /root/.ssh && \
    chmod 0700 /root/.ssh && \
    ssh-keyscan > /root/.ssh/known_hosts

# Add the keys and set permissions
COPY ./ssh/id_rsa /root/.ssh/id_rsa
RUN chmod 600 /root/.ssh/id_rsa

RUN git clone [email protected]:marcellsimon/docker-git-example.git

WORKDIR /app/docker-git-example

RUN npm i

You need to enable so it can use it for ssh, and won't prompt a message in CLI. Then add the keys to your docker container, and change the permissions (otherwise it'd throw an error). Then `RUN git clone` will pull the project.

Then you can add any commands you'd like to. You will always have the latest version of your repository in the built image. You can also add the branch name to it, so it'll always pull `main` or a specific branch.

Photo by Anete Lusina

Join the conversation.