Working directory – Lions, Tigers, and Containers – Oh My! Podman and Friends
Working directory
You’ll often want to specify the working directory inside the container, because that’s where the container is configured by default to perform its work. To get the working directory of a container, you can run the following:
$ podman image inspect pandoc/core | jq -r ‘.[].Config.WorkingDir’
Again, you get bonus points if you do this without installing jq by running this:
$ podman image inspect pandoc/core | podman run -i –rm stedolan/jq -r ‘.[].Config.WorkingDir’
In either case, the output of this command will be /data.
This tells us that when we run the pandoc/core container, the default working directory inside the container is /data.
Volume mounting
In order to make use of a container that performs a task against files on your local machine, you’ll first need to mount a volume into the container. An easy way to do this is to specify your present working directory by specifying $(pwd). Another method might be to specify ./. The order in which this is done is by declaring the host machine’s directory first, and the container’s directory second.
For example, if you wanted to mount the present working directory of the host machine into the working directory of the container, you would run -v $(pwd):/data. In this example, we are saying we want our present working directory on the host machine to be accessible inside the container at the /data path. Remember, we found the working directory of the container when we used the podman inspect command.
Super useful utility containers
Now that we’ve discussed some of the important concepts, I am going to list examples of useful utilities I’ve found in containers and then will expand on the practicality of leveraging containers for these functions.
Converting a Markdown file to docx using pandoc
In this example, let’s imagine we have a document that we want to convert to another type. There are many tools we can leverage to do this, but one such tool that comes to mind is pandoc. Rather than installing pandoc on my local machine, I’ll simply run the pandoc/core container, which has pandoc preinstalled. The entrypoint in the pandoc container is /bash/pandoc. This means that anything after the specification of the container image will automatically append to the pandoc command inside the container.
We can run –help after the pandoc/core command to learn how to use the utility, for example:
$ podman run –rm pandoc/core –help
From that (you can also view the man page maintained here: https://linux.die.net/man/1/pandoc), I now have some insight into how to use pandoc. In this case, I can use the pandoc command within the container by mounting my present working directory into the container and specifying my source document and what I want the output to be, for example:
$ podman run –rm -v $(pwd)/:/data pandoc/core -s input.md -o output.docx
Since the working directory inside the container is /data, and since I have that mounted to my host system, if the container creates a new file inside that directory, I will be able to find the output on my host system. Cool!