user1513388
user1513388

Reputation: 7441

creating new SSH keys during docker run

Is there a way to generate a new SSH server key each time a container is run? I initially thought I could add this to the Dockerfile but I think this only refers to the image build process. I need a new key each time the container is run.

These is what I did to create my image.

https://docs.docker.com/engine/examples/running_ssh_service/

Upvotes: 0

Views: 402

Answers (2)

Rene M.
Rene M.

Reputation: 2690

Here a small example setup of a custom entry point script to dynamicly edit your container at start time.

Dockerfile:

FROM SOMETHING

RUN mkdir /start
ADD entrypoint.sh /start/entrypoint.sh

# set the entry point to custom script
ENTRYPOINT ["/start/entrypoint.sh"]

# define pseudo command, which is recognized by entry point script
CMD ["sshd"]

This copies and sets the entry point to "entrypoint.sh" and the CMD to "sshd".

Here the entrypoint.sh:

#!/bin/bash

# Fail fast, including pipelines
set -eo pipefail

... do your stuff like SSH key creation

if [ "$1" = 'sshd' ]; then
    exec /usr/sbin/sshd -D
fi

# if CMD is not sshd execute the CMD. 
# Good for debugging because you can pass /bin/bash for example as CMD
exec "$@"

Now you can gerate your ssh key at start time in the entry point script and run your application if the correct CMD is provided.

For debugging the container you can now provide "/bin/bash" for example as CMD to the docker run command. Which will give you a bash shell instead of starting your application, but your customization is still done.

Start SSHD:

docker run myImageName

Debug if entrypoint script runs correctly:

docker run -i -t myImageName /bin/bash

Upvotes: 3

malbarbo
malbarbo

Reputation: 11177

Try this

CMD /bin/rm -v /etc/ssh/ssh_host_* && dpkg-reconfigure openssh-server && /usr/sbin/sshd -D

Upvotes: 1

Related Questions