Archive: Docker

GMT (Generic Mapping Toolbox) in Docker

GMT (Generic Mapping Toolbox) is a very powerful tool for making maps. It is primarily a command line tool that creates Post Script files (yes, sound like very old times, but still powerful). It is available from the GMT website. GMT was created and is still maintained by Paul Wessel at the University of Hawai’i at Mānoa. My first GMT experience dates back to ~2010, when I was a Masters student.

Fun Fact: In mid 2010s I applied for Post Doc postion at the University of Hawai’i at Mānoa. I was very excited about the opportunity to work with Paul Wessel. Unfortunately, I didn’t get the position, but it was close!

I recently decided to try GMT again, and to save time on installation and configuration I decided to use Docker. I tried to find ready to use container with GMT, but finally decided to create my own. I’m sharing my Dockerfile and instructions on GitHub: https://github.com/gozwei/gmt-docker.

I’m going to use this container to create maps for my blog. I’m going to share the code and maps in the future posts. Stay tuned!

Quick usage instructions:

I will assume that you have Docker installed on your computer. If not, please follow the instructions on the Docker website.

Clone to repository and build the container:

        
docker build -t gmt:6.4.0 .
        

Create an alias (optional, recommended if you are using Linux or MacOS – have no idea how to do that on Windows):

        
alias gmt-docker='docker run --rm -v "$PWD/:/root/work/" -i -t gmt:6.4.0'
        

This will allow you to use the container anywhere on your computer – this is useful, because you need to map your current directory to the container to use it.

Now you can use the container. For example, to create a map of the USA you can use the following commands:

        
gmt-docker gmt set FONT_ANNOT_PRIMARY 5p,Helvetica
gmt-docker gmt psbasemap -R-128/-65/24/51 -Jl-96.5/40/23/52/1:40000000 -B10g2 -K -P > USA.ps
gmt-docker gmt pscoast -R -Df -Ia/0.03p,black -J -N1/0.5p -N2/0.25p,- -G#dde6d5 -S#a5bfdd -A0/0/4 -W0.5p,black -O -P >> USA.ps
gmt-docker convert -geometry 2048x2048 -density 600 -background white -flatten -trim +repage USA.ps USA.png
        

This will create a map of the USA and save it as USA.png. The map will look like this:

Disclaimer: The purpose of this post is not to teach GMT or Docker. I’m just sharing my experience with these tools. If you want to learn more about GMT, please visit the GMT website. If you want to learn more about Docker, please visit the Docker website. I just hope I can make your journey with these tools a little bit easier.

I hope you will find this useful. If you have any questions, please let me know in the comments.

| comments

Generating images with LaTeX equations

Sometimes it is useful to generate an image (like a PNG file) with a high-resolution mathematical equation. I know that there are online tools for doing that but imagine you are like me and prefer getting things done “in-house”.

I will assume you have docker available (if not, go to https://www.docker.com/ and install Docker Desktop).

In the first step we will need to get two images from Docker Hub (we do that in terminal / command line):

docker pull miktex/miktex
docker pull dpokidov/imagemagick

Both commands will download some stuff and should be ready in a minute or so. Now we will need our LaTeX code that defines the equation:

\documentclass{article}
\usepackage{amsmath}
\pagenumbering{gobble}
\begin{document}
\begin{equation*}
e^{i\pi }+1=0
\end{equation*}
\end{document}

Now assuming that the LaTeX code is saved in test.tex file we will need to execute 3 steps:

docker run --rm -v miktex:/miktex/.miktex -v $PWD:/miktex/work miktex/miktex latex test.tex
docker run --rm -v miktex:/miktex/.miktex -v $PWD:/miktex/work miktex/miktex dvipng -D 600 -bg Transparent test.dvi
docker run --rm -v $PWD:/imgs dpokidov/imagemagick imgs/test1.png -trim imgs/test2.png

The first command uses miktex/miktex Docker Image to “compile” out LaTeX file to DVI format.

The second command uses the same Docker Image to convert DVI into PNG image. Our LaTeX document has one page, so one PNG will be created after conversion (test1.png). Unfortunately, we are not done yet, because our PNG image has strange margins.

In the last step, we fix those margins by using dpokidov/imagemagick Docker Image. Our final result looks like this (test2.png):

| comments