Sunday, March 19, 2017

Command-line Docker tip

If you're developing/running command line tools in Docker containers, you'll probably want the tool to know from what folder you're running the tool. To do this, you'll want to pass the "-v `pwd`:/source" option, where:
  • "pwd" (surrounded by backticks) is aliased to the working directory within the container
  • "/source" is the internal working directory within the container

For this (above and below) example, I'm using the Pandoc container, developed by jagregory. If I save the script (below) as "md2pdf", the syntax to run it would be something like:

  md2pdf source.md result.pdf

My Script:

#!/usr/bin/perl

$src=$ARGV[0];
$tgt=$ARGV[1];

# following must be "beamer" or "latex"
# note: use beamer when producing PDF slides 
#       and latex when producing papers

#$format = "beamer";
$format = "latex";

$margin="";
$toc="";
if($format eq "latex") {
 $margin="-V geometry:margin=1in";
 $toc = "--toc";
 $highlight = "--listings --highlight-style=tango";
}

system("sudo docker run --rm -v `pwd`:/source pg/pandoc $toc $margin \
  $highlight -f markdown -t $format $src -o $tgt");