Skip to content

Instantly share code, notes, and snippets.

@kneradovsky
Created February 26, 2015 08:11
Show Gist options
  • Save kneradovsky/6674ba2f9d190ab7b507 to your computer and use it in GitHub Desktop.
Save kneradovsky/6674ba2f9d190ab7b507 to your computer and use it in GitHub Desktop.
Docker image with ssh server
#!/bin/bash
NAME=linuxserver
MASTER_NAME=sshd
if [[ -z "$2" ]]; then
EXTERNAL_PORT=2022
else
EXTERNAL_PORT=$2
fi
export NAME=$NAME
export EXTERNAL_PORT=$EXTERNAL_PORT
sudo -E /opt/docker-runners/runners-default/master-$MASTER_NAME $1
FROM oraclelinux:6.6
MAINTAINER Konstantin Neradovsky "kneradovsky@gmail.com"
RUN yum install -y openssh-server sudo passwd
ADD run /usr/local/bin/run
ADD adminuser /etc/sudoers.d/adminuser
RUN ssh-keygen -f /etc/ssh/ssh_host_rsa_key -N '' -t rsa
RUN ssh-keygen -f /etc/ssh/ssh_host_dsa_key -N '' -t dsa
RUN echo 123qweasd | passwd --stdin root
RUN useradd -G root adminuser
RUN echo 123qwe | passwd --stdin adminuser
RUN chmod +x /usr/local/bin/run
CMD ["/usr/local/bin/run"]
#!/bin/bash
LOGS="tail -n 200 -f /var/log/messages"
CONSOLE="bash"
INTERNAL_PORT=22
PREFIX=sshdserver
CID=$EXTERNAL_PORT
HELP_MESSAGE="USAGE: $NAME <start, stop, remove, logs, console or status> [ID]"
if [[ -z "$1" ]]; then
echo $HELP_MESSAGE
exit 0
fi
if [ $1 == "start" ]; then
docker run -p $EXTERNAL_PORT:$INTERNAL_PORT --privileged -d -t --name $NAME-$CID oraclelinux:$PREFIX
docker start $NAME-$CID
echo "container started"
elif [ $1 == "stop" ]; then
docker stop $NAME-$CID
echo "Container stopped"
elif [ $1 == "remove" ]; then
docker stop $NAME-$CID
docker rm $NAME-$CID
echo "container removed"
elif [ $1 == "status" ];then
docker ps|grep "NAMES" && docker ps -a|grep "$NAME-$CID"
elif [ $1 == "logs" ];then
lxc-attach -n `sudo docker inspect $NAME-$CID | grep '"ID"' | sed 's/[^0-9a-z]//g'` -- $LOGS
elif [ $1 == "console" ];then
lxc-attach -n `sudo docker inspect $NAME-$CID | grep '"ID"' | sed 's/[^0-9a-z]//g'` -- $CONSOLE
else
echo $HELP_MESSAGE
fi
#!/bin/bash
set -e
exec /usr/sbin/sshd -D
@kneradovsky
Copy link
Author

Why

Sometime you need discardable environment to play with. Docker containers could be lightweight alternative to the regular vm snapshots. The container will start ssh server in foregorund letting you to login inside from outside of the docker host.

How to use

  1. Clone gist to folder sshdserver , cd to the folder
  2. Change line 1 in docker file to the name of your base image.
  3. Create docker image using dockerfile sudo docker build -t tag-your-image
  4. Change path of the master-sshd script on the line 13 of the container-run to the sshdserver folder
    5 . start docker using ./container-run start if portnum is omitted default value 2022 is used
  5. login into the container slogin -P portnum adminuser@dockerhosr. The password is 123qwe

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment