Project Icon

docker2singularity

Docker容器到Singularity镜像的高效转换工具

docker2singularity是一个将Docker镜像转换为Singularity镜像的开源工具。它支持squashfs、sandbox和ext3等多种输出格式,可自定义挂载点和镜像名称。该工具简化了Docker到Singularity的迁移过程,适用于Linux、Mac和Windows系统。docker2singularity主要面向HPC集群用户,使研究人员能在支持Singularity的环境中运行Docker容器。

docker2singularity

https://www.sylabs.io/guides/latest/user-guide

CircleCI

Are you developing Docker images and you would like to run them on an HPC cluster supporting Singularity? Are you working on Mac or Windows with no easy access to a Linux machine? If the pull, build, and general commands to work with docker images provided by Singularity natively do not fit your needs, docker2singularity is an alternative way to generate Singularity images. The containers are available to you on quay.io, and older versions also available for you on Docker Hub.

Usage

$ docker run quay.io/singularity/docker2singularity
USAGE: docker2singularity [-m "/mount_point1 /mount_point2"] [options] docker_image_name
OPTIONS:

          Image Format
              --folder   -f   build development sandbox (folder)
              --option   -o   add a custom option to build (-o --fakeroot or -option 'section post' )
              --writable -w   non-production writable image (ext3)         
                              Default is squashfs (recommended) (deprecated)
              --name     -n   provide basename for the container (default based on URI)
              --mount    -m   provide list of custom mount points (in quotes!)
              --help     -h   show this help and exit

Options

Image Format

  • squashfs (no arguments specified) gives you a squashfs (*.simg) image. This is a compressed, reliable, and read only format that is recommended for production images. Squashfs support was added to Singularity proper in January of 2017 and thus available as early as the 2.2.1 release.
  • sandbox (-f) builds your image into a sandbox folder. This is ideal for development, as it will produce a working image in a folder on your system.
  • ext3 (-w) builds an older format (ext3) image (*.img). This format is not recommended for production images as we have observed degradation of the images over time, and they tend to be upwards of 1.5x to 2x the size of squashfs.

Note that you are able to convert easily from a folder or ext3 image using Singularity 2.4. If your choice is to develop, making changes, and then finalize, this approach is not recommended - your changes are not recorded and thus the image not reproducible.

Mount Points

  • -m specify one or more mount points to create in the image.

Options

If you look at singularity build --help there are a variety of options available. You can specify some custom option to the command using the --option flag. Make sure that each option that you specify is captured as a single string. E.g.,:

--option --fakeroot 
--option '--section post'

Image Name

The last argument (without a letter) is the name of the docker image, as you would specify to run with Docker (e.g., docker run ubuntu:latest)

Legacy

If you want a legacy version, see the repository branches and tag history on the registry.

Containers were previous built on Docker Hub and now are provided on quay.io. A tag with prefix v corresponds to a release of the Singularity software, while the others are in reference to releases of Docker. Previously used scripts, including environment and action files, are provided in this repository for reference.

Requirements

  • Docker (native Linux or Docker for Mac or Docker for Windows) - to create the Singularity image.
  • Singularity >= 2.1 - to run the Singularity image (versions 2.0 and older are not supported!). Note that if running a 2.4 image using earlier versions, not all (later developed) features may be available.

Examples

Build a Squashfs Image

Squashfs is the recommended image type, it is compressed and less prone to degradation over time. You don't need to specify anything special to create it:

This is a path on my host, the image will be written here

$ mkdir -p /tmp/test

And here is the command to run. Notice that I am mounting the path /tmp/test that I created above to /output in the container, where the container image will be written (and seen on my host).

$ docker run -v /var/run/docker.sock:/var/run/docker.sock \
-v /tmp/test:/output \
--privileged -t --rm \
quay.io/singularity/docker2singularity \
ubuntu:14.04

Image Format: squashfs
Inspected Size: 188 MB

(1/10) Creating a build sandbox...
(2/10) Exporting filesystem...
(3/10) Creating labels...
(4/10) Adding run script...
(5/10) Setting ENV variables...
(6/10) Adding mount points...
(7/10) Fixing permissions...
(8/10) Stopping and removing the container...
(9/10) Building squashfs container...
Building image from sandbox: /tmp/ubuntu_14.04-2017-09-13-3e51deeadc7b.build
Building Singularity image...
Singularity container built: /tmp/ubuntu_14.04-2017-09-13-3e51deeadc7b.simg
Cleaning up...
(10/10) Moving the image to the output folder...
     62,591,007 100%  340.92MB/s    0:00:00 (xfr#1, to-chk=0/1)
Final Size: 60MB

We can now see the finished image!

$ ls /tmp/test
ubuntu_14.04-2018-04-27-c7e04ea7fa32.simg

And use it!

$ singularity shell /tmp/test/ubuntu_14.04-2018-04-27-c7e04ea7fa32.simg
Singularity: Invoking an interactive shell within container...

Singularity ubuntu_14.04-2018-04-27-c7e04ea7fa32.simg:~/Documents/Dropbox/Code/singularity/docker2singularity> 

Take a look again at the generation code above, and notice how the image went from 188MB to 60MB? This is one of the great things about the squashfs filesystem! This reduction is even more impressive when we are dealing with very large images (e.g., ~3600 down to ~1800). A few notes on the inputs shown above that you should edit:

  • /tmp/test: the path you want to have the final image reside. If you are on windows this might look like D:\host\path\where\to\output\singularity\image. -ubuntu:14.04: the docker image name you wish to convert (it will be pulled from Docker Hub if it does not exist on your host system).

docker2singularity uses the Docker daemon located on the host system. It will access the Docker image cache from the host system avoiding having to redownload images that are already present locally.

If you ever need to make changes, you can easily export the squashfs image into either a sandbox folder or ext3 (legacy) image, both of which have writable.

sudo singularity build --sandbox sandbox/ production.simg
sudo singularity build --writable ext3.img production.simg

Custom Naming

Added for version 2.5.1, you can specify the name of your container with the -n/--name argument, as follows:

docker run -v /var/run/docker.sock:/var/run/docker.sock \
-v /tmp/test:/output \
--privileged -t --rm \
quay.io/singularity/docker2singularity \
--name meatballs ubuntu:14.04

...

$ ls /tmp/test/
meatballs.simg

Inspect Your Image

New with docker2singularity 2.4, the labels for the container are available with inspect:

 singularity inspect ubuntu_14.04-2017-09-13-3e51deeadc7b.simg 
{
    "org.label-schema.singularity.build": "squashfs",
    "org.label-schema.docker.version": "17.06.2-ce",
    "org.label-schema.schema-version": "1.0",
    "org.label-schema.singularity.build-type": "docker2singularity",
    "org.label-schema.docker.id": "sha256:dea1945146b96542e6e20642830c78df702d524a113605a906397db1db022703",
    "org.label-schema.build-date": "2017-10-28-17:19:18",
    "org.label-schema.singularity.version": "2.4-dist",
    "org.label-schema.docker.created": "2017-09-13"
}

as is the runscript and environment

singularity inspect --json -e -r ubuntu_14.04-2017-09-13-3e51deeadc7b.simg 
{
    "data": {
        "attributes": {
            "environment": "# Custom environment shell code should follow\n\n",
            "runscript": "#!/bin/sh\n/bin/bash $@\n"
        },
        "type": "container"
    }
}

Build a Sandbox Image

A sandbox image is a folder that is ideal for development. You can view it on your desktop, cd inside and browse, and it works like a Singularity image. To create a sandbox, specify the -f flag:

docker run -v /var/run/docker.sock:/var/run/docker.sock \
-v /host/path/change/me:/output \
--privileged -t --rm \
quay.io/singularity/docker2singularity \
-f \
ubuntu:14.04

Importantly, you can use --writable, and if needed, you can convert a sandbox folder into a production image:

sudo singularity build sandbox/ production.simg

Build a Legacy (ext3) Image

You can build a legacy ext3 image (with --writable) with the -w flag. This is an older image format that is more prone to degradation over time, and (building) may not be supported for future versions of the software.

docker run -v /var/run/docker.sock:/var/run/docker.sock \
-v /host/path/change/me:/output \
--privileged -t --rm \
quay.io/singularity/docker2singularity \
-w \
ubuntu:14.04

You can also use --writable and convert an ext3 image into a production image:

sudo singularity build ext3.img production.simg

Contributed Examples

The following are a list of brief examples and tutorials generated by the Singularity community for using docker2singularity. If you have an example of your own, please let us know!

  • docker2singularity-demo: an example of using docker2singularity on MacOS and using Vagrant to test the output Singularity image, complete with notes and a nice Makefile.

Tips for making Docker images compatible with Singularity

  • Define all environmental variables using the ENV instruction set. Do not rely on .bashrc, .profile, etc.
  • Define an ENTRYPOINT instruction set pointing to the command line interface to your pipeline
  • Do not define CMD - rely only on ENTRYPOINT
  • You can interactively test the software inside the container by overriding the ENTRYPOINT docker run -i -t --entrypoint /bin/bash bids/example
  • Do not rely on being able to write anywhere other than the home folder and /scratch. Make sure your container runs with the --read-only --tmpfs /run --tmpfs /tmp parameters (this emulates the read-only behavior of Singularity)
  • Don’t rely on having elevated user permissions
  • Don’t use the USER instruction set

FAQ

Here are some frequently asked questions if you run into trouble!

"client is newer than server" error

If you are getting the following error: docker: Error response from daemon: client is newer than server

You need to use the docker info command to check your docker version and use it to grab the correct corresponding version of docker2singularity. For example:

     docker run \        
     -v /var/run/docker.sock:/var/run/docker.sock \
     -v D:\host\path\where\to\output\singularity\image:/output \
     --privileged -t --rm \
     singularityware/docker2singularity:1.11 \            
     ubuntu:14.04

Currently only the 1.10, 1.11, 1.12, and 1.13 versions are supported. If you are using an older version of Docker you will need to upgrade.

My cluster/HPC requires Singularity images to include specific mount points

If you are getting WARNING: Non existant bind point (directory) in container: '/shared_fs' or a similar error when running your Singularity image that means that your Singularity images require custom mount points. To make the error go away you can specify the mount points required by your system when creating the Singularity image:

     docker run \        
     -v /var/run/docker.sock:/var/run/docker.sock \
     -v D:\host\path\where\to\output\singularity\image:/output \
     --privileged -t --rm \
     quay.io/singularity/docker2singularity \            
     -m "/shared_fs /custom_mountpoint2" \
     ubuntu:14.04

Development

1. Build the container

You can build a development container as follows. First, update the VERSION to be correct.

VERSION=$(cat VERSION)
image="quay.io/singularity/docker2singularity:${VERSION}"
docker build -t ${image} .

2. Test the container

We have a Circle CI builder that tests generation of the final image, and basic running to ensure the entrypoint is functioning. Since we cannot run the priviledged Docker daemon on Circle, a test.sh script is provided for local testing.

chmod u+x
/bin/bash test.sh

If there are missing tests or you have added new features, please add the test here!

3. Documentation

If you have added new features, please describe usage in the README.md here. Don't forget to read the CONTRIBUTING.md along with the code of conduct and add yourself to the authors file.

Acknowledgements

This work is heavily based on the docker2singularity work done by vsoch and gmkurtzer. The original record of the work can be read about in this commit. Thank you kindly to all the contributors, and please open an issue if you need

项目侧边栏1项目侧边栏2
推荐项目
Project Cover

豆包MarsCode

豆包 MarsCode 是一款革命性的编程助手,通过AI技术提供代码补全、单测生成、代码解释和智能问答等功能,支持100+编程语言,与主流编辑器无缝集成,显著提升开发效率和代码质量。

Project Cover

AI写歌

Suno AI是一个革命性的AI音乐创作平台,能在短短30秒内帮助用户创作出一首完整的歌曲。无论是寻找创作灵感还是需要快速制作音乐,Suno AI都是音乐爱好者和专业人士的理想选择。

Project Cover

有言AI

有言平台提供一站式AIGC视频创作解决方案,通过智能技术简化视频制作流程。无论是企业宣传还是个人分享,有言都能帮助用户快速、轻松地制作出专业级别的视频内容。

Project Cover

Kimi

Kimi AI助手提供多语言对话支持,能够阅读和理解用户上传的文件内容,解析网页信息,并结合搜索结果为用户提供详尽的答案。无论是日常咨询还是专业问题,Kimi都能以友好、专业的方式提供帮助。

Project Cover

阿里绘蛙

绘蛙是阿里巴巴集团推出的革命性AI电商营销平台。利用尖端人工智能技术,为商家提供一键生成商品图和营销文案的服务,显著提升内容创作效率和营销效果。适用于淘宝、天猫等电商平台,让商品第一时间被种草。

Project Cover

吐司

探索Tensor.Art平台的独特AI模型,免费访问各种图像生成与AI训练工具,从Stable Diffusion等基础模型开始,轻松实现创新图像生成。体验前沿的AI技术,推动个人和企业的创新发展。

Project Cover

SubCat字幕猫

SubCat字幕猫APP是一款创新的视频播放器,它将改变您观看视频的方式!SubCat结合了先进的人工智能技术,为您提供即时视频字幕翻译,无论是本地视频还是网络流媒体,让您轻松享受各种语言的内容。

Project Cover

美间AI

美间AI创意设计平台,利用前沿AI技术,为设计师和营销人员提供一站式设计解决方案。从智能海报到3D效果图,再到文案生成,美间让创意设计更简单、更高效。

Project Cover

AIWritePaper论文写作

AIWritePaper论文写作是一站式AI论文写作辅助工具,简化了选题、文献检索至论文撰写的整个过程。通过简单设定,平台可快速生成高质量论文大纲和全文,配合图表、参考文献等一应俱全,同时提供开题报告和答辩PPT等增值服务,保障数据安全,有效提升写作效率和论文质量。

投诉举报邮箱: service@vectorlightyear.com
@2024 懂AI·鲁ICP备2024100362号-6·鲁公网安备37021002001498号