[Part 2] Creating a Docker container that delivers Markdown as HTML in Apache / Pandoc-remote version

Introduction

The other day I introduced a Docker container that incorporates Apache and Pandoc → Local version

It was a method to put the source file in the container or in the directory of the host where the container runs. Instead, I've created a container (remote version) that can be used when the web resource containing the Markdown source I want to provide is outside the container host, that is, remotely, so I will publish it.

The Docker file and other sources are on github, and the created Docker image is published on Docker Hub (see the end).

Below, I will omit the common matters with the previous local version.

Apache

In the case of the local version, the contents of the Markdown source (extension md) that actually exists in the directory under Apache are passed to the standard input of the filter. However, if the Markdown file does not exist in the local directory under Apache, this method cannot be used.

Apache has a mechanism called an action handler that calls a pre-specified CGI script when there is a request for a URL path with a specific extension. The requested path does not have to be in the local directory.

Using this mechanism, I created a handler that reads the Markdown source from a web server other than the container and converts it to HTML.

Specify an action handler named pandoc-handler in the following Apache configuration.

[local.conf]

LoadModule cgid_module modules/mod_cgid.so
LoadModule actions_module modules/mod_actions.so

AddHandler markdown-text .md
Action markdown-text /cgi-bin/pandoc-handler virtual

SetEnv REMOTE_DIR https://elsewhere.com/docker

I will explain.

--Load mod_actions, which makes action handlers available in LoadModule, and mod_cgid, which makes CGI scripts available. --In the AddHandler and Action directives, set the CGI script pandoc-handler to be called when the file specified in the URL path has the extension md. --SetEnv is the remote directory specification explained in the next section.

In the base image httpd: 2.4, the URL'/ cgi-bin' is previously associated with the script execution directory'/ usr / local / apache2 / cgi-bin', so use this.

Apache handler

I have created an Apache handler that calls Pandoc. This is a Bash shell script that converts the Markdown source obtained from Apache according to the URL path passed in the PATH_INFO environment variable with Pandoc and outputs HTML to standard output.

There are many ways to load (or even generate) Markdown source from the requested URL path, depending on your application. Read from a cloud file service, read from a database somewhere, get from a remote git repository, get with sftp, and more.

This sample reads a Markdown source file from a remote directory pointed to by a fixed URL. Read the Markdown file from the REMOTE_DIR environment variable set in the previous section with the curl command.

[pandoc-handler]

 #!/bin/bash
BASENAME=$(basename -s .md $PATH_INFO)
curl -s $REMOTE_DIR$PATH_INFO | /usr/bin/pandoc -f gfm -t html5 -c "/pandoc-gfm.css" \
	-T "Converted" -M title=${BASENAME}

I will explain.

--The URL path of the referenced Markdown source is passed in the PATH_INFO environment variable. --Get the specified file with curl from the remote Web server specified in the REMOTE_DIR environment variable in the previous section. --Convert to HTML with pandoc and output to standard output

Acquisition example:

Docker creation

Create a Docker container that incorporates the above Apache configuration file and handler script.

$ ls
 Dockerfile
 usr-local-apache2/
$ ls usr-local-apache2
 local.conf
 pandoc-handler
$ cat Dockerfile
 FROM httpd:2.4
 RUN apt-get update && apt-get install -y \
  pandoc \
  curl \
  ca-certificates
 COPY ./usr-local-apache2 /usr/local/apache2/
 RUN echo 'Include local.conf' >> /usr/local/apache2/conf/httpd.conf
$ docker build -t pandoc:remote .

As before, get the base image httpd including Linux and Apache from the official Docker hub in FROM. Unlike last time, RUN gets curl in addition to pandoc. ca-certificate was needed to use SSL with curl. As before, copy the Apache configuration file and action handler (CGI script) to the appropriate location in the container with COPY, and execute the Docker build command.

Docker container execution

Once you have a Docker container, try running it on the created host.

$ docker run --detach -publish 8080:80 pandoc:remote

Unlike the previous filter, it doesn't use the local directory, so there is no --mount option. As before, assign the port with the --public option.

test

First from within the host machine running the container:

$ curl localhost:8080/test.md

Then access it with a browser from outside the host machine.

http://example.com:8080/test.md

If the Markdown file that should have been placed at'https://elsewhere.com/docker/test.md'is displayed in HTML, it is successful.

Next, I tried running this Docker container with ECS (Elastic Container Service), one of the container services of Amazon AWS. Next time, I will write the details.


usage environment

Reference link

The source of Dockerfile, Apache configuration file, and Apache filter is published on github. You can create a container by git clone and docker build.

You can also get the created image from Docker Hub.

Related page of Yokohama Kobunsha

-Markdown conversion Docker -Markdown Dynamic Conversion -Markdown commentary

Written 2020/09/22

Recommended Posts

[Part 2] Creating a Docker container that delivers Markdown as HTML in Apache / Pandoc-remote version
[Part 1] Creating a Docker container that delivers Markdown in HTML with Apache / Pandoc
[Participation] Creating a Docker container that distributes Markdown as HTML in Apache / Pandoc --AWS container
Creating a matrix class in Java Part 1