25

I want to build singularity container from dockerfile.

I have pulled and run docker images from docker hub with singularity.

singularity pull docker://ubuntu:latest

I have also build the image from singularity recipe file.

singularity build  cpp.sif singularity_file

But I want to build singularity image from dockerfile.

Anyone know how to do it. Is it possible ???

alex
  • 303
  • 1
  • 4
  • 6
  • Looks like all the accepted solutions uses docker. Is there a way to build the singularity container directly from the dockerfile without calling docker? Only singularity? – Soerendip Dec 06 '22 at 00:31

3 Answers3

30

You cannot build a singularity container directly from a Dockerfile, but you can do it in a two-step process.

docker build -t local/my_container:latest .
sudo singularity build my_container.sif docker-daemon://local/my_container:latest

Using docker://my_container looks for the container on Docker Hub. When you use docker-daemon, it looks at your locally built docker containers. You can also use Bootstrap: docker-daemon in a Singularity definition file.


EDIT: Both singularity and apptainer now require an explicit tag name for the source docker container. Answer updated accordingly.

tsnowlan
  • 3,472
  • 10
  • 15
16

You can transform a Dockerfile into a singularity recipe or vise-versa using Singularity Python. Singularity Python offers some very helpful utilities, consider to install it if you plan to work with singularity a lot

pip3 install spython # if you do not have spython install it from the command line

# print in the console
spython recipe Dockerfile

# save in the *.def file
spython recipe Dockerfile &> Singularity.def

If you have problems with pip you can download spython or pull a container as described in Singularity Python install. Find more about recipe conversion here

Serge
  • 3,387
  • 3
  • 16
  • 34
  • 1
    I would recommend using ">" and not "&>" as "&>" will also write warnings into the output file, which will then of course result in parsing errors when you try to use it. – RHS Aug 08 '23 at 08:14
-6
sudo singularity build ubuntu.sif docker://ubuntu:latest

builds it directly for me

Unsure if there was an update to singularity for this purpose

perrycn
  • 3
  • 3