我有两个Docker镜像,一个包含pandoc
(一个实用程序,用于将不同格式的文档转换为多种格式),另一个包含pdflatex(从texlive
开始,将tex文件转换为pdf).我的目标是将文档从md转换为pdf.
我可以分别运行每个图像:
# call pandoc inside my-pandoc-image (md -> tex)
docker run --rm \
-v $(pwd):/pandoc \
my-pandoc-image \
pandoc -s test.md -o test.tex
# call pdflatex inside my-texlive-image (tex -> pdf)
docker run --rm \
-v $(pwd):/texlive \
my-texlive-image \
pdflatex test.tex # generates test.pdf
但是,实际上,我想要的是直接调用pandoc(从容器中)将md转换为pdf,如下所示:
docker run --rm \
-v $(pwd):/pandoc \
my-pandoc-image \
pandoc -s test.md --latex-engine pdflatex -o test.pdf
此命令在此处不起作用,因为容器内的pandoc尝试调用pdflatex(必须在$PATH中)来生成pdf,但pdflatex不存在,因为它未安装在my-pandoc-image中.
就我而言,pdflatex安装在图像my-texlive-image中.
因此,从这个例子中,我的问题是:容器A可以调用位于另一个容器B上的可执行文件吗?
我很确定这是可能的,因为如果我在我的主机上安装pandoc(没有pdflatex),我可以运行pandoc -s test.md – latex-engine = pdflatex -o test.pdf,只需将pdflatex命令别名:
pdflatex() {
docker run --rm \
-v $(pwd):/texlive \
my-texlive-image \
pdflatex "$@"
}
因此,当pandoc调用pdflatex时,容器会启动并执行转换.
但是当使用2个容器时,我怎么能将pdflatex命令别名来模拟它在只有pandoc的容器上的存在?
我看了一下docker-compose,因为我已经用它来制作2个容器进行通信(app与数据库通信).我甚至考虑过从容器A到容器B的ssh,以调用pdflatex命令,但这绝对是not the right solution.
最后,我还构建了一个包含pandoc pdflatex的图像(因为两个可执行文件位于同一图像上,所以它有效),但我真的想分别保留2个图像,因为它们可以被其他图像独立使用.
编辑:
一个类似的问题暴露在here,因为据我所知,提供的答案需要在容器A上安装Docker,并且需要在主机和容器A之间使用docker socket绑定(/var/run/docker.sock).我不认为这是最好的做法,它似乎是一个可以创建security issues的黑客.