Unable to Connect to Git - SSH Issues

I am trying to checkout the my code from my corporate codecloud URL to the docker directory and then trying to run mvn command on the code to create a jar file.

I am working on Windows 11.

My current Dockerfile is like:

FROM maven:3.8.6 AS maven
FROM eclipse-temurin:11-jdk-alpine as jdk

RUN mkdir -p $HOME/images/lib/ && cd $HOME/images/lib/

RUN apk update && apk add git && apk add net-tools procps openssh-client openssh-server

RUN ssh-keyscan codecloud.web.att.com >> ~/.ssh/known_hosts

RUN git clone ssh://git@CODECLOUD_REPO_URL

RUN mvn clean install -e

But when I run the docker build command, I get:

Step 6/8 : RUN ssh-keyscan CODECLOUD_HOST >> ~/.ssh/known_hosts
—> Running in 704ee07c00e8
/bin/sh: can’t create /root/.ssh/known_hosts: nonexistent directory

Is there a way to fix this?

Also is it possible to make it dynamic enough, so that it can create a ssh private/pub key and then the known_hosts file and use it to connect to codecloud?

You need to create an SSH directory first as suggested in the documentation:
Source: RUN --mount=type=ssh

FROM alpine
RUN apk add --no-cache openssh-client
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
1 Like