Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

docker buildkit mount ssh when using remote agent forwarding

I use the --ssh docker buildkit feature and it works fine locally. I want to build Docker at a remote server and for that I use the -A flag to forward my local github key, like:

ssh -i "server.pem" -A <user>@<server-ip>

Then in server terminal I run:

ssh -T [email protected]

And I get the "Hello user" message, which means the key forwarding works fine.
(In the server, $SSH_AUTH_SOCK is indeed set, and I can git clone)

Now, when building locally I use:

DOCKER_BUILDKIT=1 docker build --ssh default=~/.ssh/id_rsa -t myimage:latest .

Which works fine.
But in the server the private key does not exists at ~/.ssh/id_rsa. So how can I forward it to docker build? Tried this in the server:

DOCKER_BUILDKIT=1 docker build --ssh default=$SSH_AUTH_SOCK -t myimage:latest .

But it does not work. The error is:

could not parse ssh: [default]: invalid empty ssh agent socket, make sure SSH_AUTH_SOCK is set

Even though SSH_AUTH_SOCK is set

Docker version: 19.03

like image 476
user3599803 Avatar asked Jan 21 '20 09:01

user3599803


People also ask

Is docker BuildKit still experimental?

Buildkit itself is distributed with the current stable releases of docker and available behind a feature flag (not an experimental flag).

Should I use BuildKit docker?

To summarize, BuildKit has better performance and uses the same docker build interface which we are already familiar with. Additionally, BuildKit enables the use of cache and storing cache in remote container repositories like DockerHub for better build performance as we don't have to rebuild every layer of an image.

Is BuildKit enabled by default?

BuildKit is now available with the Docker Daemon service. It is not enabled by default and can be enabled by setting the environment variable DOCKER_BUILDKIT=1 in the pipelines configuration.

What is docker BuildKit?

Docker BuildKit is the next generation container image builder, which helps us to make Docker images more efficient, secure, and faster. It's integrated into the Docker release version v18.06. BuildKit is a part of the Moby project which was developed after learning's and failures to make the image build process –


2 Answers

I had a similar issue and it was fixed quite simply, I wrapped ${SSH_AUTH_SOCK} within curly braces

eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
DOCKER_BUILDKIT=1 docker build -t myimage:latest --ssh default=${SSH_AUTH_SOCK} .

In the Docker file, I have appropriate RUN instruction to run a command that requires sensitive data

RUN --mount=type=ssh \
    mkdir vendor && composer install
like image 200
Serhii Popov Avatar answered Oct 20 '22 11:10

Serhii Popov


You need to have ssh-agent running on your machine and the key added to it with ssh-add or use ssh -A -o AddKeysToAgent=true when logging in. SSH will not automatically forward the key specified with -i if you set -A afaik. After logging in you can run ssh-add -L to make sure your keys were forwarded and if you see records there then docker build --ssh default . should work fine now.

eval `ssh-agent`
ssh-add server.pem
ssh -A <user>@<server-ip>
like image 38
Tõnis Tiigi Avatar answered Oct 20 '22 09:10

Tõnis Tiigi