{"id":8563,"date":"2022-09-22T11:03:54","date_gmt":"2022-09-22T10:03:54","guid":{"rendered":"https:\/\/www.blopig.com\/blog\/?p=8563"},"modified":"2022-10-04T16:26:41","modified_gmt":"2022-10-04T15:26:41","slug":"how-to-make-your-own-singularity-container-zero-fuss","status":"publish","type":"post","link":"https:\/\/www.blopig.com\/blog\/2022\/09\/how-to-make-your-own-singularity-container-zero-fuss\/","title":{"rendered":"How to make your own singularity container zero fuss!"},"content":{"rendered":"\n<p>In this blog post, I&#8217;ll show you guys how to make your own shiny container for your tool! Zero fuss(*) and in FOUR simple steps.<\/p>\n\n\n\n<p>As an example, I will show how to make a singularity container for one of our public tools, ANARCI, the antibody numbering tool everyone in OPIG and external users are familiar with &#8211; If not, check the web app and the GitHub repo <a rel=\"noreferrer noopener\" href=\"https:\/\/opig.stats.ox.ac.uk\/webapps\/newsabdab\/sabpred\/anarci\/\" data-type=\"URL\" data-id=\"https:\/\/opig.stats.ox.ac.uk\/webapps\/newsabdab\/sabpred\/anarci\/\" target=\"_blank\">here<\/a> and <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/oxpig\/ANARCI\" data-type=\"URL\" data-id=\"https:\/\/github.com\/oxpig\/ANARCI\" target=\"_blank\">here<\/a>.<\/p>\n\n\n\n<p>(*) Provided you have your own Linux machine with <code>sudo<\/code>  permissions, otherwise, you can&#8217;t do it &#8211; sorry. Same if you have a Mac or Windows &#8211; sorry again.<br>BUT, there are workarounds for these cases such as using the remote singularity builder <a rel=\"noreferrer noopener\" href=\"https:\/\/cloud.sylabs.io\/builder\" data-type=\"URL\" data-id=\"https:\/\/cloud.sylabs.io\/builder\" target=\"_blank\">here<\/a>, for which you only need to sign up and create an account, and the use of Virtual Machines (VMs), as described <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.sylabs.io\/guides\/3.2\/user-guide\/installation.html#install-on-windows-or-mac\" data-type=\"URL\" data-id=\"https:\/\/docs.sylabs.io\/guides\/3.2\/user-guide\/installation.html#install-on-windows-or-mac\" target=\"_blank\">here<\/a>. <\/p>\n\n\n\n<!--more-->\n\n\n\n<h1 class=\"wp-block-heading\">A brief preamble<\/h1>\n\n\n\n<p>If you already know what singularity and containerisation are, you can skip this section and move on to the actual fun!<\/p>\n\n\n\n<p>In previous blog posts, other OPIGlets already introduced <em>singularity<\/em> and illustrated the security hazards posed by <em>Docker<\/em> (another popular containerisation technology) in shared systems, like HPC, which singularity prevents [<a rel=\"noreferrer noopener\" href=\"https:\/\/www.blopig.com\/blog\/2020\/03\/considering-containers-go-for-singularity\/\" data-type=\"URL\" data-id=\"https:\/\/www.blopig.com\/blog\/2020\/03\/considering-containers-go-for-singularity\/\" target=\"_blank\">here<\/a>]. Also, explained further the idea of containerisation and highlighted its advantages for scientific reproducibility, to avoid the irritating  &#8220;<em>It works on my machine<\/em>&#8221; scenario. As well as a basic introduction to building your own singularity container [<a rel=\"noreferrer noopener\" href=\"https:\/\/www.blopig.com\/blog\/2021\/05\/singularity-a-guide-for-the-bewildered-bioinformatician\/\" data-type=\"URL\" data-id=\"https:\/\/www.blopig.com\/blog\/2021\/05\/singularity-a-guide-for-the-bewildered-bioinformatician\/\" target=\"_blank\">here<\/a>] and how to run singularity in Windows with WSL2 [<a rel=\"noreferrer noopener\" href=\"https:\/\/www.blopig.com\/blog\/2021\/09\/using-singularity-on-windows-with-wsl2\/\" data-type=\"URL\" data-id=\"https:\/\/www.blopig.com\/blog\/2021\/09\/using-singularity-on-windows-with-wsl2\/\" target=\"_blank\">here<\/a>]. <\/p>\n\n\n\n<p>In this blog post, I&#8217;ll go further on how to build containers using definition files and provide a recipe to equip our container with <code>conda<\/code>, to manage environment variables and dependencies, and illustrate the process by cloning and installing one of our tools available on GitHub. <\/p>\n\n\n\n<h1 class=\"wp-block-heading\">STEP 1. Install singularity. <\/h1>\n\n\n\n<p>Instructions are <a href=\"https:\/\/github.com\/sylabs\/singularity\/blob\/main\/INSTALL.md\" data-type=\"URL\" data-id=\"https:\/\/github.com\/sylabs\/singularity\/blob\/main\/INSTALL.md\" target=\"_blank\" rel=\"noreferrer noopener\">here<\/a>.<\/p>\n\n\n\n<p><em>A word of caution<\/em>: Aim to install any Singularity-CE version &gt; 3.3; CE = Community Edition. Previous versions had bugs resulting in a <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/apptainer\/singularity\/issues\/5408#issuecomment-652412299\" data-type=\"URL\" data-id=\"https:\/\/github.com\/apptainer\/singularity\/issues\/5408#issuecomment-652412299\" target=\"_blank\">mount error<\/a> post-installation. If you find any other bugs, have a look at the issue list of the singularity repo <a rel=\"noreferrer noopener\" href=\"https:\/\/github.com\/sylabs\/singularity\/issues\" data-type=\"URL\" data-id=\"https:\/\/github.com\/sylabs\/singularity\/issues\" target=\"_blank\">here<\/a>. On my laptop, I have SingularityCE 3.9.5v and Go 1.17.7v<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">STEP 2. Steal these lines.<\/h1>\n\n\n\n<p>Copy and paste the block below into a text file, for example, <code>myfile.def<\/code> <\/p>\n\n\n\n<pre class=\"wp-block-code language=&quot;css&quot; has-blue-color has-text-color\"><code>BootStrap: library\nFrom: ubuntu:18.04\n\n%environment\n    # set up all essential environment variables\n    export LC_ALL=C\n    export PATH=\/miniconda3\/bin:$PATH\n    export PYTHONPATH=\/miniconda3\/lib\/python3.9\/:$PYTHONPATH\n    \n    # activate conda environment\n    source activate base;\n    conda activate;\n    \n%post\n    # update and install essential dependencies\n    apt-get -y update\n    apt-get update &amp;&amp; apt-get install -y automake build-essential bzip2 wget git default-jre unzip\n    apt-get install muscle # ANARCI dependency\n    \n    # download, install, and update miniconda3\n    wget https:\/\/repo.continuum.io\/miniconda\/Miniconda3-latest-Linux-x86_64.sh\n    bash Miniconda3-latest-Linux-x86_64.sh -b -f -p \/miniconda3\/\n    rm Miniconda3-latest-Linux-x86_64.sh\n    \n    # install dependencies via conda\n    export PATH=\"\/miniconda3\/bin:$PATH\"\n    conda install -y -c conda-forge pip numpy # general dependencies\n    conda install -y -c bioconda biopython hmmer # ANARCI dependencies\n    conda update --all\n    \n    # clone and install your github code\n    mkdir mycode\n    git clone https:\/\/github.com\/oxpig\/ANARCI.git mycode\/ANARCI\n    cd mycode\/ANARCI\n    pip install .\n\n%labels\n    Author Me\n    Version v0.0\n    MyLabel Hello World<\/code><\/pre>\n\n\n\n<p><em>What do these lines mean?<\/em><br>In singularity jargon, this is called a <em>definition file<\/em>, which is a recipe for singularity to build your singularity image file, aka <code>.sif<\/code>, which is a highly compressed file that will allow you to run your tool in the command line. <\/p>\n\n\n\n<p>As you can see from the definition file, it tells singularity to build a system with an <code>Ubuntu 18.04<\/code>  kernel pulled from its public <code>library<\/code> or file repository, and to define essential system <code>environment<\/code> variables. Also, a set of operations <code>post<\/code> or after pulling the kernel that will be necessary to configure and update the container&#8217;s inner system, as well as the tools you want your system to have along with their dependencies; <code>conda<\/code> and <code>ANARCI<\/code> in this example. <\/p>\n\n\n\n<p>The definition file can be further customised, here I just show some basic sections of it for the purposes of this example. Have a look <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.sylabs.io\/guides\/3.7\/user-guide\/definition_files.html#sections\" data-type=\"URL\" data-id=\"https:\/\/docs.sylabs.io\/guides\/3.7\/user-guide\/definition_files.html#sections\" target=\"_blank\">here<\/a> for what else you can add to your definition file. <\/p>\n\n\n\n<h1 class=\"wp-block-heading\">STEP 3. Build your own sh*t<\/h1>\n\n\n\n<p>Run the following command line in terminal<\/p>\n\n\n\n<p><code>sudo singularity build mytool.sif myfile.def<\/code><\/p>\n\n\n\n<p>Again, you need <code>sudo<\/code> rights to be able to build your output container file. But, as I mentioned above, you can try out the <a href=\"https:\/\/cloud.sylabs.io\/builder\" data-type=\"URL\" data-id=\"https:\/\/cloud.sylabs.io\/builder\" target=\"_blank\" rel=\"noreferrer noopener\">remote builder<\/a> to go around it. <\/p>\n\n\n\n<p><em>An important point<\/em>: probably building directly the <code>.sif<\/code> file is not the best practice. The problem is that once built, you can&#8217;t modify anything inside unless you modify the definition file. This is irritating if you want to modify or debug a tool built into your container. As a workaround, you can create instead a <code>sandbox<\/code><\/p>\n\n\n\n<p><code>sudo singularity build --sandbox mytool myfile.def<\/code><\/p>\n\n\n\n<p>This will output a folder, <code>mytool<\/code>,  containing all the system files that go into your container content which you can actually see and modify at will. For instance, in shell mode (<code>sudo singularity --writable shell mytool\/<\/code>), you can manually install and modify files or update dependencies with conda too. And once you&#8217;re happy with your modifications, then you can create a <code>.sif<\/code> file, which actually is much lighter than a sandbox folder for sharing. <\/p>\n\n\n\n<p><code>sudo singularity build mytool.sif mytool<\/code>\/<\/p>\n\n\n\n<p>For more on this, look <a rel=\"noreferrer noopener\" href=\"https:\/\/docs.sylabs.io\/guides\/3.7\/user-guide\/build_a_container.html?highlight=writable#creating-writable-sandbox-directories\" data-type=\"URL\" data-id=\"https:\/\/docs.sylabs.io\/guides\/3.7\/user-guide\/build_a_container.html?highlight=writable#creating-writable-sandbox-directories\" target=\"_blank\">here<\/a> and <a href=\"https:\/\/www.blopig.com\/blog\/2021\/05\/singularity-a-guide-for-the-bewildered-bioinformatician\/\" data-type=\"URL\" data-id=\"https:\/\/www.blopig.com\/blog\/2021\/05\/singularity-a-guide-for-the-bewildered-bioinformatician\/\" target=\"_blank\" rel=\"noreferrer noopener\">this<\/a> previous blog post.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">STEP 4. Have fun!<\/h1>\n\n\n\n<p>If you made it to this last step without stumbling across any errors, then, you&#8217;re nearly there, cause now you get to play with your tool!<\/p>\n\n\n\n<p>For ANARCI, we&#8217;ll execute its command line tool to number an input sequence by using the <code>exec<\/code> command of singularity upon calling your container<\/p>\n\n\n\n<p><code>singularity exec mytool.sif ANARCI -i EVQLQQSGAEVVRSGASVKLSCTASGFNIKDYYIHWVKQRPEKGLEWIGWIDPEIGDTEYVPKFQGKATMTADTSSNTAYLQLSSLTSEDTAVYYCNAGHDYDRGRFPYWGQGTLVTVSA<\/code><\/p>\n\n\n\n<p>If everything goes well you should see an output like in the screenshot <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><a href=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2022\/09\/image.png?ssl=1\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"625\" height=\"433\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2022\/09\/image.png?resize=625%2C433&#038;ssl=1\" alt=\"\" class=\"wp-image-8579\" srcset=\"https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2022\/09\/image.png?resize=1024%2C710&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2022\/09\/image.png?resize=300%2C208&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2022\/09\/image.png?resize=768%2C532&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2022\/09\/image.png?resize=624%2C433&amp;ssl=1 624w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2022\/09\/image.png?w=1356&amp;ssl=1 1356w, https:\/\/i0.wp.com\/www.blopig.com\/blog\/wp-content\/uploads\/2022\/09\/image.png?w=1250&amp;ssl=1 1250w\" sizes=\"auto, (max-width: 625px) 100vw, 625px\" \/><\/a><\/figure><\/div>\n\n\n\n<p>If you got an output like this, then pat yourself on the back and congratulate yourself. Well done! \ud83c\udf89<\/p>\n\n\n\n<p>Now, you can just share <code>mytool.sif<\/code> with your friends and they should be able to run your tool with singularity too!<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">The bottom line.<\/h1>\n\n\n\n<p>Containerisation is an amazing technology that lowers the reproducibility barrier in scientific computing by providing a self-contained environment to pack your tools and dependencies to make them ready to use.<\/p>\n\n\n\n<p>Here we showed how to use singularity to containerise <code>ANARCI<\/code> in addition to the use  <code>miniconda<\/code> to equip our container with an environment manager to handle dependencies. Now, you can just modify the definition file to pull and install your favourite tool.<\/p>\n\n\n\n<p>Certainly, this is just the tip of the tip the iceberg. I will probably post more about this topic in the future. In the meantime, have fun and if you have any questions I&#8217;d love to hear from you. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post, I&#8217;ll show you guys how to make your own shiny container for your tool! Zero fuss(*) and in FOUR simple steps. As an example, I will show how to make a singularity container for one of our public tools, ANARCI, the antibody numbering tool everyone in OPIG and external users are [&hellip;]<\/p>\n","protected":false},"author":100,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","wikipediapreview_detectlinks":true,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[29,14],"tags":[24,388,198,386,649,648],"ppma_author":[631],"class_list":["post-8563","post","type-post","status-publish","format-standard","hentry","category-code","category-howto","tag-bioinformatics","tag-docker","tag-reproducibility","tag-singularity","tag-software-services","tag-technical"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"authors":[{"term_id":631,"user_id":100,"is_guest":0,"slug":"broncio","display_name":"Broncio Aguilar-Sanjuan","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/98ed21f7b05613d3b21c650ec13af1cafba2e7d8c40a23dba86b9a8b623bd3ef?s=96&d=mm&r=g","0":null,"1":"","2":"","3":"","4":"","5":"","6":"","7":"","8":""}],"_links":{"self":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/8563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/users\/100"}],"replies":[{"embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/comments?post=8563"}],"version-history":[{"count":5,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/8563\/revisions"}],"predecessor-version":[{"id":8608,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/posts\/8563\/revisions\/8608"}],"wp:attachment":[{"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/media?parent=8563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/categories?post=8563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/tags?post=8563"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.blopig.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=8563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}