7

I am new to both Docker and Singularity. I recently created the canonical main.c.

#include <stdio.h>
#include <stdlib.h>

int main(void){
    printf("Hello Docker World!\n");
    return 0;
}

I statically compiled this code, i.e.

gcc -static -static-libgcc -static-libstdc++ -o hello main.c

I then built the docker image and could run it, i.e.

dockerd &  ## Start Docker daemon
docker build --tag hello .
docker run hello   ## Outputs "Hello Docker World"

I then save the image so that I can export it to a second computer (which does not have docker, but does have singularity), i.e.

docker save hello > hello.tar

Now on the second machine, which does not have docker but does have singularity, I want to create a singularity image. Tthe singularity documentation give instructions on creating a singularity image from a docker image on Docker Hub, but they do not give instructions on converting from a docker tar'd file.

Question : How would I create a singularity image from my hello.tar

irritable_phd_syndrome
  • 4,631
  • 3
  • 32
  • 60

3 Answers3

6
  1. First, save your docker image.

    sudo docker save image_id -o local.tar

  2. Then copy to another machine in any way you like

  3. Finally, build a singularity image from the local.tar by

    singularity build local_tar.sif docker-archive://local.tar

    which used the docker-archive bootstrap agent. Can read it here

Community
  • 1
  • 1
Ruolin Liu
  • 151
  • 2
  • 5
  • 1
    How can I do this if I don't have access to docker on the machine in step 3? – Rylan Schaeffer Oct 07 '20 at 04:16
  • I get `ERROR: Unknown container build Singularity recipe format: docker-archive://local.tar ABORT: Aborting with RETVAL=255` – eric_kernfeld Dec 02 '20 at 12:33
  • @RuolinLiu I tried the same method to do the conversion which as you mentioned is on the official singularity website as well. But the problem I face is that the converted image is quite small as compared to the original image (approx 1/3rd) which makes me believe that the build is skipping all the packages (perhaps custom ones) in the original docker image. Do you have any ideas as to what's going on here? – Omayr Apr 19 '21 at 14:31
  • 1
    Try this. `singularity build local_tar.sif local.tar` It works for me. – Simon Jul 17 '21 at 02:13
2

The answer by Ruolin Liu is what I would recommend trying first.

Another thing you could try is the tool docker2singularity. docker2singularity is itself a docker image so it is easy to install. Furthermore it offers support for current and older versions of singularity.

As an example of how to use docker2singularity on your hello docker image you would...

  1. make a directory to write the singularity image file to

    mkdir -p /tmp/test
    
  2. use docker2singularity on the docker image hello and mount /tmp/test as the output directory

    docker run -v /var/run/docker.sock:/var/run/docker.sock \
    -v /tmp/test:/output \
    --privileged -t --rm \
    quay.io/singularity/docker2singularity \
    hello
    
  3. move the created singularity image file from /tmp/test to a host with singularity and run it

Josh Anibal
  • 146
  • 5
0

Use local registry container as below:

# Start a docker registry
$ docker run -d -p 5000:5000 --restart=always --name registry registry:2
# Push local docker container to it
$ docker tag alpine localhost:5000/alpine
$ docker push localhost:5000/alpine
# Create def file for singularity like this..
# (add your modifications)
Bootstrap: docker
Registry: http://localhost:5000
Namespace:
From: alpine:latest
# Build singularity container
$ sudo SINGULARITY_NOHTTPS=1 singularity build alpine.simg def
Lakshya Garg
  • 736
  • 2
  • 8
  • 23