Factor dockerConnect() into its own function

It was getting a bit unwieldy.