【9.3.2.3】CWL--Containers

Docker容器通过为软件及其依赖项提供完整的已知良好运行时来简化软件安装。然而,容器也有目的地与主机系统隔离,因此为了在Docker容器内运行工具,需要额外的工作来确保容器内的输入文件可用,并且可以从容器中恢复输出文件。CWL运行程序可以自动执行这项工作,允许您使用Docker简化软件管理,同时避免调用和管理Docker容器的复杂性。

CWL运行程序的职责之一是调整输入文件的路径,以反映它们在容器中出现的位置。

本例在Docker容器中运行一个简单的Node.js脚本,然后将“Hello World”打印到标准输出中。

docker.cwl

#!/usr/bin/env cwl-runner

cwlVersion: v1.0
class: CommandLineTool
baseCommand: node
hints:
  DockerRequirement:
    dockerPull: node:slim
inputs:
  src:
    type: File
    inputBinding:
      position: 1
outputs:
  example_out:
    type: stdout
stdout: output.txt

docker-job.yml

src:
  class: File
  path: hello.js

在我们运行这个之前,让我们先把它分解一下,看看一些部分做了什么。大部分内容在前面的章节中已经解释过了,唯一真正新的部分是dockerRequirement部分。

baseCommand: node
hints:
  DockerRequirement:
    dockerPull: node:slim

baseCommand:node告诉CWL我们将在容器中运行此命令。然后,我们需要为如何找到我们想要的容器指定一些提示。在这种情况下,我们只在DockerRequirements中列出了对docker容器的需求。dockerPull:参数的值与传递给dockerPull命令的值相同。也就是说,容器图像的名称(您甚至可以指定标签,这对于使用容器进行可重复研究的最佳实践来说是个好主意)。在本例中,我们使用了一个名为node:slim的容器。

提供一个“hello.js”并调用cwltool,在命令行上提供工具描述和输入对象:

hello.js

console.log("Hello World");

运行:

$ cwltool docker.cwl docker-job.yml
INFO /opt/hostedtoolcache/Python/3.9.14/x64/bin/cwltool 3.1.20221008225030
INFO Resolved 'docker.cwl' to 'file:///home/runner/work/user_guide/user_guide/src/_includes/cwl/using-containers/docker.cwl'
INFO [job docker.cwl] /tmp/go2i7j2x$ docker \
    run \
    -i \
    --mount=type=bind,source=/tmp/go2i7j2x,target=/TrYKhX \
    --mount=type=bind,source=/tmp/6iyw6npw,target=/tmp \
    --mount=type=bind,source=/home/runner/work/user_guide/user_guide/src/_includes/cwl/using-containers/hello.js,target=/var/lib/cwl/stg110e27ff-01eb-4df5-b579-34242e7b0000/hello.js,readonly \
    --workdir=/TrYKhX \
    --read-only=true \
    --log-driver=none \
    --user=1001:121 \
    --rm \
    --cidfile=/tmp/0q73qojh/20221018123433-839892.cid \
    --env=TMPDIR=/tmp \
    --env=HOME=/TrYKhX \
    node:slim \
    node \
    /var/lib/cwl/stg110e27ff-01eb-4df5-b579-34242e7b0000/hello.js > /tmp/go2i7j2x/output.txt
INFO [job docker.cwl] Max memory used: 0MiB
INFO [job docker.cwl] completed success
{
    "example_out": {
        "location": "file:///home/runner/work/user_guide/user_guide/src/_includes/cwl/using-containers/output.txt",
        "basename": "output.txt",
        "class": "File",
        "checksum": "sha1$648a6a6ffffdaa0badb23b8baf90b6168dd16b3a",
        "size": 12,
        "path": "/home/runner/work/user_guide/user_guide/src/_includes/cwl/using-containers/output.txt"
    }
}
INFO Final process status is success

	
$ cat output.txt
Message is: Hello world!
Notice the CWL runner has constructed a Docker command line to run the script.

在本例中,脚本hello.js的路径是容器外的/home/me/cwl/user_guide/hello.js,但容器内的路径是/var/lib/cwl/job369354770_examples/hello.jsp,这反映在node命令的调用中

参考资料

药企,独角兽,苏州。团队长期招人,感兴趣的都可以发邮件聊聊:tiehan@sina.cn
个人公众号,比较懒,很少更新,可以在上面提问题,如果回复不及时,可发邮件给我: tiehan@sina.cn