rp346
rp346

Reputation: 7028

docker build --ssh default doesn't mount ssh key

I am trying to run following docker build command

DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain docker build --ssh default=~/.ssh/id_rsa -t dev-build .

and have following command for installing private package using composer in Dockerfile


RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts
RUN --mount=type=ssh git clone [email protected]:devteam/db.git

But its failing pass the SSH key. I can clone the same repo using the same ssh key locally.

#7 [stage-0 4/5] RUN --mount=type=ssh git clone [email protected]:devteam/db.git
#7 0.309 Cloning into 'db'...
#7 0.476 Host key verification failed.
#7 0.477 fatal: Could not read from remote repository.
#7 0.477
#7 0.477 Please make sure you have the correct access rights
#7 0.477 and the repository exists.
#7 ERROR: process "/bin/sh -c git clone [email protected]:devteam/db.git" did not complete successfully: exit code: 128
------
 > [stage-0 4/5] RUN --mount=type=ssh git clone [email protected]:devteam/db.git:
0.309 Cloning into 'db'...
0.476 Host key verification failed.
0.477 fatal: Could not read from remote repository.
0.477
0.477 Please make sure you have the correct access rights
0.477 and the repository exists.
------

I am on Docker version 26.1.4, build 5650f9b

Any help on this what I am missing here ?

Upvotes: 0

Views: 509

Answers (1)

aetherblaze
aetherblaze

Reputation: 1

the SSH key isn’t being passed correctly during the Docker build.

Enable BuildKit in Docker: Edit /etc/docker/daemon.json and add:

{
    "features": {
        "buildkit": true
    }
}

Restart Docker:

sudo systemctl restart docker

Update your Dockerfile: Make sure your Dockerfile uses the SSH mount correctly:

Dockerfile # syntax=docker/dockerfile:1.4

FROM your_base_image

RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan bitbucket.org >> ~/.ssh/known_hosts
RUN --mount=type=ssh git clone [email protected]:devteam/db.git

Build with SSH key: Use this build command:

DOCKER_BUILDKIT=1 BUILDKIT_PROGRESS=plain docker build --ssh default=$HOME/.ssh/id_rsa -t dev-build .

Check SSH key permissions: Ensure the SSH key has the right permissions:

chmod 600 ~/.ssh/id_rsa

Add SSH key to the agent: If using Docker Desktop, add your SSH key to the agent:

ssh-add ~/.ssh/id_rsa

Upvotes: 0

Related Questions