Crude Ruby Script to ask for a Password to use for Private git Repo

Crude Ruby Script to ask for a Password to use for Private git Repo

The Intro

Good ole Docker. It is a powerful tool to setup your environments whether in production, dev or test. Plowing through and building a new container I discovered I had the need or want to pass an argument to the Dockerfile and let it log me into a private repo.

The Environment Variable. 

Looking through the Dockerfile documentation on
the docker.io website I saw this wonderful looking thing called an environment variable. When I first looked at it I thought that meant that we could pass them in from the host cli running the docker command. I was dead wrong. The environment variable does create a variable but its only for the container’s environment. So what does that mean for us. Well if you think about the reason for Docker is for an unattended setup of an environment. Now if I were going to do this completely automated then I would probably create a ssh key and copy it over using Dockerfile. This is not what I wanted. I really wanted a seperation where I didn’t store my password or ssh key within the environment. Below is my crude way of getting around this. 

The Gemfile 

source 'https://rubygems.org'
gem 'highline'

The Gemfile in this instance is very simple. You really could skip using highline all together, however I thought it would be cool to mask the password as you type.

The Ruby

require 'highline/import'
pass = ask("Enter your Password : "){ |q| q.echo = "*" }
file = File.open("dock_temp", "r")
text = file.read()
file.close
out = File.open("Dockerfile", "w")
out.puts text.gsub("$PASS", pass)
out.close
`docker build -t yourtag .`
`rm Dockerfile`

The Ruby script will ask you for a password. Once input it will open the “dock_temp” file within the directory. Then we do a simple replacement of the $PASS variable to be the actual password that was input by the user. Once that is complete we write it to a file called Dockerfile.
The Docker file is the file used when you run “docker build”. The Ruby script then makes a system call and it will build your environment. Lastly we want to get rid of the password just laying around so we delete the Dockerfile

The Temp Dockerfile

FROM ubuntu:14.10
RUN apt-get update
RUN apt-get install -y git
RUN git clone https://username:$PASS@bitbucket.org/username/your_repo.git
WORKDIR /your_repo

The Dockerfile template is very simple. It grabs a copy of ubuntu, updates the repo, installs git. At this point all you have to do is run the git clone and you have you code
Voila!!!