Git clone private repo in docker


Git clone private repo in docker

A simple how to guide to use git clone of a private repository inside docker container. With vuejs / nodejs app.

The package.json build script

  "build": "vue-cli-service build --port 8003", # for vuejs projects
  "start:prod": "babel-node ./src/index.js", # for nodejs projects with babel-node

env file

SSH_KEY="-----BEGIN OPENSSH PRIVATE KEY-----
YOUR CONTENT HERE
-----END OPENSSH PRIVATE KEY-----"

Vuejs app


# Choose and name our temporary image.
FROM alpine as intermediate

# Take an SSH key as a build argument.
ARG SSH_KEY

# Install dependencies required to git clone.
RUN apk update && \
    apk add --update git && \
    apk add --update openssh

# 1. Create the SSH directory.
RUN mkdir -p /root/.ssh/
# 2. Populate the private key file.
RUN echo "$SSH_KEY" > /root/.ssh/id_rsa
# 3. Set the required permissions.
RUN chmod -R 600 /root/.ssh/
# 4. Add github to our list of known hosts for ssh.
RUN ssh-keyscan -t rsa github.com >> ~/.ssh/known_hosts

# inject a datestamp arg which is treated as an environment variable and
# will break the cache for the next RUN command
ARG DATE_STAMP

# Clone a repository
RUN git clone [email protected]:USER/REPO_NAME.git

# default branch is master/main
# if you need to change the branch uncomment the following lines
# RUN cd /REPO_NAME && \
#    git checkout YOUR_BRANCH

## Build Stage ##

# pull the Node.js Docker image
FROM node:lts-alpine

# install simple http server for serving static content
RUN npm install -g http-server

# create the directory inside the container
WORKDIR /usr/src/app

# copy essential files to install dependencies
COPY --from=intermediate /REPO_NAME/app/package*.json .
COPY --from=intermediate /REPO_NAME/app/babel.config.js .
COPY --from=intermediate /REPO_NAME/app/vue.config.js .

# run npm install in our local machine
RUN npm install

# copy the generated modules and all other files to the container
COPY --from=intermediate /REPO_NAME/app/src/ ./src

# build app for production with minification
RUN npm run build

# our app is running on port 8003 within the container, so need to expose it
EXPOSE 8003

# the command that starts our app
CMD [ "http-server", "dist", "--port","8003" ]

# if you have a nodejs project, is not needed http-server,
# you can comment the line above and uncomment the next one
# CMD ["npm", "run", "start:prod"]

Docker-compose file

version: '3.3'

services:
  app:
    build:
      context: .
      dockerfile: app.Dockerfile
      args:
      - SSH_KEY=${SSH_KEY}
      - DATE_STAMP=${DATE_STAMP}
    container_name: app
    restart: always
    depends_on:
      - postgres
    ports:
      - "8003:8003"
    networks:
      - my-network

networks:
  my-network:
    driver: bridge

Newsletter


Related Posts

Get your Jenkins Passwords from secrets

How to get your Jenkins passwords from your secrets credentials. Follow this simple tutoriale and find your password or ssh private key.

How to launch a 6 figures startup with 0$ investment

Are you curious how to launch a startup in the fast and effective way? Here's not the answer, but you can understand how to achieve it

How to cold email

How to cold email - a practical guide about how to use cold email in your activity as a CEO of a software agency or startup owner.

Linux disk space alerts on Discord

Linux disk space alerts. Check your space and be responsible with your project. Take care of your space and don't crack the project.

How to detect the browser language in javascript

How to detect the browser language in javascript in just a line of code. The method is working on all major browser

You don't need a macbook pro to be a pro

You don't need a macbook pro to be a pro! You need a laptop and internet connection, and a brain, but you already have it

Free templates for startup

Free HTML templates for startup or a new project. A complete list with free resources to build your next startup's website and gain the traction to the sky.

What I learned to never do in a presentation meeting

What I learned to never do in a presentation meeting. I had a very bad meeting experience with a gateway provider.

Startup graveyard is a series of killed projects that made millions in revenue per year

Startup graveyard is a series of killed projects that made millions in revenue per year. Just watch their graveyard and do it your own. What better validation do you expect.

How do you detect Credit card type based on the number?

How do you detect Credit card type based on the number using JavaScript? Simple solution using only code, no library required.