Skip to content

Instantly share code, notes, and snippets.

@EddyBorja
Last active August 1, 2019 05:18
Show Gist options
  • Save EddyBorja/8cbb496e281eddb8ba59e4d10b61d4e3 to your computer and use it in GitHub Desktop.
Save EddyBorja/8cbb496e281eddb8ba59e4d10b61d4e3 to your computer and use it in GitHub Desktop.
Script for Docker lab
#!/bin/bash
if [[ $EUID -ne 0 ]]; then
echo "Error: You need to run this script as root, bro"
exit 1
fi
apt-get remove docker docker-engine docker.io containerd runc -y
apt-get update -y
apt-get upgrade -y
apt-get install apt-transport-https ca-certificates curl gnupg-agent software-properties-common -y
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
apt-key fingerprint 0EBFCD88
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
apt-get update -y
apt-get install docker-ce docker-ce-cli containerd.io -y
echo "Testing Docker installation..."
docker run hello-world
echo "Finished testing Docker installation..."
echo "Creating a folder to place Dockerfile..."
mkdir eg_sshd
currentuser=$(who | awk 'NR==1{print $1}')
chown $currentuser:$currentuser ./eg_sshd
cd eg_sshd
read -sp 'Enter a password for your docker ssh server: ' passvar
cat >Dockerfile <<EOL
FROM ubuntu:16.04
RUN apt-get update && apt-get install -y openssh-server
RUN mkdir /var/run/sshd
RUN echo 'root:$passvar' | chpasswd
RUN sed -i 's/PermitRootLogin prohibit-password/PermitRootLogin yes/' /etc/ssh/sshd_config
# SSH login fix. Otherwise user is kicked off after login
RUN sed 's@session\s*required\s*pam_loginuid.so@session optional pam_loginuid.so@g' -i /etc/pam.d/sshd
ENV NOTVISIBLE "in users profile"
RUN echo "export VISIBLE=now" >> /etc/profile
EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]
EOL
chown $currentuser:$currentuser ./Dockerfile
echo "Created Dockerfile. Building..."
docker build -t eg_sshd .
docker run -d -P --name test_sshd eg_sshd
docker port eg_sshd 22
docker images
docker container ls
cd ..
echo "Doing ssh to root@localhost on port 32768"
echo "Now enter the same ssh password you just entered to ssh in. Have fun!"
ssh root@localhost -p 32768 -oStrictHostKeyChecking=accept-new
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment