Turkmenistan are okay with you getting your visa at the border, so in theory you would be able to get your visa on arrival in Turkmenbashi. BUT the past couple of years the immigration officers in Azerbaijan haven't been letting passengers into the port to board the ferry without a valid Turkmen visa already in their passports.
Therefore if you're crossing the Caspian Sea, you'll need to get the visa before boarding the ferry and the final dates will need to be stated when you apply for the actual visa at the embassy either in Baku or in Istanbul. Yes, this is difficult. But if you wanted easy you wouldn't be here.
Read more about how to get your Turkmenistan Visa in Azerbaijan on The Visa Machine help pages here.