Skip to content

Instantly share code, notes, and snippets.

@Revolucent
Last active February 19, 2023 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Revolucent/d0bb7565aba546e9d58e560027eecd17 to your computer and use it in GitHub Desktop.
Save Revolucent/d0bb7565aba546e9d58e560027eecd17 to your computer and use it in GitHub Desktop.
Using SSH inside a Dockerfile
eval `ssh-agent`
ssh-add
docker build -t my:latest --ssh default=$HOME/.ssh/github-private-key .
# syntax=docker/dockerfile:1.5
FROM haskell:8.10.7
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan github.com >> ~/.ssh/known_hosts
WORKDIR /tmp/app
COPY . .
RUN mkdir -p /home/app
RUN --mount=type=ssh stack install --local-bin-path /home/app
RUN rm -rf /tmp/app
WORKDIR /home/app
CMD ["./my-exe"]

SSH in a Dockerfile

Prerequisites

  • You must have BuildKit enabled in Docker.
  • You must have ~/.ssh/config set up with a proper alias for the server you want to access via SSH.
  • When referencing external resources via SSH, always use a proper domain name, never an SSH alias. In other words, use something like git@github.com:foo/bar.
  • Use the latest Dockerfile syntax using # syntax=docker/dockerfile:1.x at the top.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment