From df45ce9a136e22b459132af4a9285e34968e3901 Mon Sep 17 00:00:00 2001 From: Sammy filly <136061549+sammyfilly@users.noreply.github.com> Date: Sun, 3 Sep 2023 07:20:25 +0100 Subject: [PATCH] Create SSH Signed-off-by: Sammy filly <136061549+sammyfilly@users.noreply.github.com> --- SSH | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 SSH diff --git a/SSH b/SSH new file mode 100644 index 0000000..653740b --- /dev/null +++ b/SSH @@ -0,0 +1,66 @@ + +How To Install Private Git Hosted Dependencies Inside Docker Image Using SSH +# +docker +# +devops +# +security +# +python +Introduction +This quick guide will show you how to mount a ssh key inside a container in build time, to allow you to install private dependencies, that won't be persisted in the final image. It uses python but could work with any language/package manager that uses git + ssh. + +Dockerfile +First you need to set Dockerfile syntax to docker/dockerfile:1.2. Put this in the beggining of the file: + +# syntax = docker/dockerfile:1.2 +Now install git and openssh, and setup ssh folders: + +RUN apt update && \ + apt install -y git openssh-client && \ + mkdir -p /root/.ssh && \ + ssh-keyscan github.com >> /root/.ssh/known_hosts +May vary depending on the base image you're using, just change with the package manager you use. + +Make sure to change github.com with your git host. + +Now you have to mount the ssh key in the step that installs the dependency: + +RUN --mount=type=secret,id=id_rsa,dst=/root/.ssh/id_rsa \ + pip install git+ssh://git@github.com/username/repository.git@version +This will mount secret identified by id_rsa on /root/.ssh/id_rsa. + +Building +When building you need to specify your ssh key as id_rsa secret: + +docker build . \ + -f Dockerfile \ + --secret id=id_rsa,src=/home/user/.ssh/id_rsa +Or using docker compose: + +version: '3.7' +services: + your_service: + build: + context: . + dockerfile: Dockerfile + secrets: + - id_rsa +secrets: + id_rsa: + file: /home/user/.ssh/id_rsa +Final file +# syntax = docker/dockerfile:1.2 + +FROM python:3.11 + +RUN apt update && \ + apt install -y git openssh-client && \ + mkdir -p /root/.ssh && \ + ssh-keyscan github.com >> /root/.ssh/known_hosts + +RUN --mount=type=secret,id=id_rsa,dst=/root/.ssh/id_rsa \ + pip install git+ssh://git@github.com/username +example + pip install git+ssh://git@github.com/sammyfilly