0

I need the containers own public key to be in an environment variable in the container image so that when the image is spun up the variable AUTHORIZED_KEYS is already populated with it.

I try to assign the variable AUTHORIZED_KEYS to the public key in the Dockerfile but when I look at it when I start the container and look at all the environment variables in the running container it is empty. I do see my test variable ERNIE being populated correctly. 

Here is the Dockerfile I have so far that does not work -

FROM alpine:latest

ARG SSH_PUBLIC_KEY

# ssh-keygen -A generates all necessary host keys (rsa, dsa, ecdsa, ed25519) at default location.
RUN    apk update \
    && apk add openssh \
    && apk add openssh-server-pam \
    && mkdir /root/.ssh \
    && chmod 0700 /root/.ssh \
    && ssh-keygen -A \
    && sed -i s/^#PasswordAuthentication\ yes/PasswordAuthentication\ no/ /etc/ssh/sshd_config \
    && sed -i s/^#UsePAM\ no/UsePAM\ yes/ /etc/ssh/sshd_config \
    && sed -i s/root:!/"root:*"/g /etc/shadow \
    && ssh-keygen -f ~/.ssh/id_rsa -N "" -q \
    && SSH_PUBLIC_KEY="$(cat ~/.ssh/id_rsa.pub)" 
    #&& AUTHORIZED_KEYS="$(cat ~/.ssh/id_rsa.pub)"
RUN cat /etc/ssh/sshd_config

# This image expects AUTHORIZED_KEYS environment variable to contain your ssh public key.

EXPOSE 22

ENV ERNIE="hellooo there ernie"
ENV AUTHORIZED_KEYS=$SSH_PUBLIC_KEY

ErnieAndBert
  • 1,344
  • 3
  • 21
  • 43
  • [Using SSH keys inside docker container](https://stackoverflow.com/questions/18136389/using-ssh-keys-inside-docker-container) has lots of discussion on this topic; on current Docker you should be able to `RUN --mount=type=ssh` to get access to the calling user's ssh keys. Be careful to _not_ commit the keys into the image lest you compromise them! – David Maze Nov 04 '21 at 20:00
  • (I wouldn't in general recommend trying to run an ssh daemon inside a container. It tends to break the "one process per container" rule, and managing credentials is really tricky, as you're seeing here.) – David Maze Nov 04 '21 at 20:00

0 Answers0