Skip to content

Commit 146539a

Browse files
committed
finishing up v2.4 docker to singularity conversion
1 parent 890209e commit 146539a

File tree

3 files changed

+51
-52
lines changed

3 files changed

+51
-52
lines changed

Dockerfile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
FROM docker:1.13
22

3-
RUN apk add --update automake libtool m4 autoconf alpine-sdk linux-headers && \
4-
wget -qO- https://github.com/gmkurtzer/singularity/archive/2.1.2.tar.gz | tar zxv && \
5-
cd singularity-2.1.2 && ./autogen.sh && ./configure --prefix=/usr/local && make && make install && \
6-
cd ../ && rm -rf singularity-2.1.2 && \
3+
RUN apk add --update automake libtool python m4 autoconf alpine-sdk linux-headers && \
4+
wget -qO- https://github.com/singularityware/singularity/archive/2.4.tar.gz | tar zxv && \
5+
cd singularity-2.4 && ./autogen.sh && ./configure --prefix=/usr/local && make && make install && \
6+
cd ../ && rm -rf singularity-2.4 && \
77
apk del automake libtool m4 autoconf alpine-sdk linux-headers
88

99
RUN mkdir -p /usr/local/var/singularity/mnt
1010

11-
RUN apk add e2fsprogs bash tar rsync
11+
RUN apk add e2fsprogs bash tar rsync squashfs-tools
1212
ADD docker2singularity.sh /docker2singularity.sh
1313
RUN chmod a+x docker2singularity.sh
1414

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
# `docker2singularity`
22

3-
Are you developing Docker images and you would like to run them on an HPC cluster supporting [Singularity](http://singularity.lbl.gov)? Are you working on Mac or Windows with no easy access to a Linux machine? `docker2singularity` is the simplest way to generate Singularity images. Note that the upcoming Singularity (2.3) release supports import from Docker without sudo, natively, and this is the recommended approach. More information will be provided in the upcoming documentation for 2.3.
3+
Are you developing Docker images and you would like to run them on an HPC cluster supporting [Singularity](http://singularity.lbl.gov)? Are you working on Mac or Windows with no easy access to a Linux machine? If the pull, import, and general commands to [work with docker images provided by Singularity](http://singularity.lbl.gov/docs-docker) natively do not fit your needs, `docker2singularity` is an alternative way to generate Singularity images. This particular branch is intended for Singularity 2.4, which builds a squashfs image. If you want a legacy version to build ext3 images, see the following other branches:
4+
5+
- [v2.3](https://github.com/singularityware/docker2singularity/tree/v2.3): Version 2.3 of Singularity. The image format is ext3.
6+
- [v2.4](https://github.com/singularityware/docker2singularity/tree/v2.4): Version 2.4 of Singularity. The default image format is squashfs.
7+
8+
Intermediate versions built on [Docker Hub](https://hub.docker.com/r/singularityware/docker2singularity/tags/). A tag with prefix `v` corresponds to a release of the Singularity software, while the others are in reference to releases of Docker.
49

510
## Requirements
611

712
- Docker (native Linux or Docker for Mac or Docker for Windows) - to create the Singularity image.
8-
- Singularity >= 2.1 - to run the Singularity image (**versions 2.0 and older are not supported!**).
13+
- 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.
914

1015
## Usage
1116

@@ -16,7 +21,7 @@ docker run \
1621
-v /var/run/docker.sock:/var/run/docker.sock \
1722
-v D:\host\path\where\to\output\singularity\image:/output \
1823
--privileged -t --rm \
19-
singularityware/docker2singularity \
24+
singularityware/docker2singularity:2.4 \
2025
ubuntu:14.04
2126
```
2227

@@ -51,6 +56,7 @@ You need to use the `docker info` command to check your docker version and use i
5156

5257
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.
5358

59+
5460
### My cluster/HPC requires Singularity images to include specific mount points
5561
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:
5662

docker2singularity.sh

Lines changed: 37 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ if [ $# == 0 ] ; then
4343
echo $USAGE
4444
exit 1;
4545
fi
46+
4647
mount_points="/oasis /projects /scratch /local-scratch /work /home1 /corral-repl /corral-tacc /beegfs /share/PI /extra /data /oak"
4748
while getopts ':hm:' option; do
4849
case "$option" in
@@ -108,9 +109,7 @@ size=`echo $(($size/1000000+1))`
108109
# I think it would be Ok by adding 1/3 of the size.
109110
size=`echo $(($size+$size/2))`
110111

111-
echo "Size: $size MB for the singularity container"
112-
113-
112+
echo "Size: $size MB for the singularity container, although not needed for squashfs."
114113

115114

116115
################################################################################
@@ -120,23 +119,20 @@ TMPDIR=$(mktemp -u -d)
120119
mkdir -p $TMPDIR
121120

122121
creation_date=`echo ${creation_date} | cut -c1-10`
123-
new_container_name=/tmp/$image_name-$creation_date-$container_id.img
124-
echo "(1/9) Creating an empty image..."
125-
singularity create -s $size $new_container_name
126-
echo "(2/9) Importing filesystem..."
127-
docker export $container_id | singularity import $new_container_name
128-
docker inspect $container_id >> $TMPDIR/singularity.json
129-
singularity copy $new_container_name $TMPDIR/singularity.json /
130-
131-
# Bootstrap the image to set up scripts for environment setup
132-
echo "(3/9) Bootstrapping..."
133-
singularity bootstrap $new_container_name
134-
chmod a+rw -R $TMPDIR
122+
new_container_name=/tmp/$image_name-$creation_date-$container_id.simg
123+
build_sandbox="${new_container_name}.build"
124+
echo "(1/9) Creating a build sandbox..."
125+
mkdir -p ${build_sandbox}
126+
echo "(2/9) Exporting filesystem..."
127+
docker export $container_id >> $build_sandbox.tar
128+
singularity image.import $build_sandbox < $build_sandbox.tar
129+
docker inspect $container_id >> $build_sandbox/singularity.json
130+
135131

136132
################################################################################
137133
### SINGULARITY RUN SCRIPT #####################################################
138134
################################################################################
139-
echo "(4/9) Adding run script..."
135+
echo "(3/9) Adding run script..."
140136
CMD=$(docker inspect --format='{{json .Config.Cmd}}' $image)
141137
if [[ $CMD != [* ]]; then
142138
if [[ $CMD != "null" ]]; then
@@ -156,61 +152,58 @@ fi
156152
# Remove quotes, commas, and braces
157153
ENTRYPOINT=`echo "${ENTRYPOINT//\"/}" | sed 's/\[//g' | sed 's/\]//g' | sed 's/,/ /g'`
158154

159-
echo '#!/bin/sh' > $TMPDIR/singularity
155+
echo '#!/bin/sh' > $build_sandbox/singularity
160156
if [[ $ENTRYPOINT != "null" ]]; then
161-
echo $ENTRYPOINT '$@' >> $TMPDIR/singularity;
157+
echo $ENTRYPOINT '$@' >> $build_sandbox/singularity;
162158
else
163159
if [[ $CMD != "null" ]]; then
164-
echo $CMD '$@' >> $TMPDIR/singularity;
160+
echo $CMD '$@' >> $build_sandbox/singularity;
165161
fi
166162
fi
167163

168-
chmod +x $TMPDIR/singularity
169-
singularity copy $new_container_name $TMPDIR/singularity /
164+
chmod +x $build_sandbox/singularity
165+
170166

171167
################################################################################
172168
### SINGULARITY ENVIRONMENT ####################################################
173169
################################################################################
174-
echo "(5/9) Setting ENV variables..."
170+
echo "(4/9) Setting ENV variables..."
175171
docker run --rm --entrypoint="/usr/bin/env" $image > $TMPDIR/docker_environment
176-
# don't include HOME and HOSTNAME - they mess with local config
172+
# do not include HOME and HOSTNAME - they mess with local config
177173
sed -i '/^HOME/d' $TMPDIR/docker_environment
178174
sed -i '/^HOSTNAME/d' $TMPDIR/docker_environment
179175
sed -i 's/^/export /' $TMPDIR/docker_environment
180176
# add quotes around the variable names
181177
sed -i 's/=/="/' $TMPDIR/docker_environment
182178
sed -i 's/$/"/' $TMPDIR/docker_environment
183-
singularity copy $new_container_name $TMPDIR/docker_environment /
184-
singularity exec --writable $new_container_name /bin/sh -c "echo '. /docker_environment' >> /environment"
179+
cp $TMPDIR/docker_environment $build_sandbox/docker_environment
180+
echo '. /docker_environment' >> $build_sandbox/environment
185181
rm -rf $TMPDIR
186182

183+
187184
################################################################################
188185
### Permissions ################################################################
189186
################################################################################
190-
if [ "${mount_points}" ]; then
191-
echo "(6/9) Adding mount points..."
192-
singularity exec --writable --contain $new_container_name /bin/sh -c "mkdir -p ${mount_points}"
193-
else
194-
echo "(6/9) Skipping mount points..."
187+
if [ "${mount_points}" ] ; then
188+
echo "(5/9) Adding mount points..."
189+
mkdir -p "${build_sandbox}/${mount_points}"
190+
else
191+
echo "(5/9) Skipping mount points..."
195192
fi
196193

197194
# making sure that any user can read and execute everything in the container
198-
echo "(7/9) Fixing permissions..."
199-
singularity exec --writable --contain $new_container_name /bin/sh -c "find /* -maxdepth 0 -not -path '/dev*' -not -path '/proc*' -not -path '/sys*' -exec chmod a+r -R '{}' \;"
200-
buildname=$(singularity exec --contain $new_container_name /bin/sh -c "head -n 1 /etc/issue")
201-
echo $buildname
202-
if [[ $buildname =~ Buildroot|Alpine ]] ; then
203-
# we're running on a Builroot container and need to use Busybox's find
204-
echo "We're running on BusyBox/Buildroot"
205-
singularity exec --writable --contain $new_container_name /bin/sh -c "find / -type f -or -type d -perm -u+x,o-x -not -path '/dev*' -not -path '/proc*' -not -path '/sys*' -exec chmod a+x '{}' \;"
206-
else
207-
echo "We're not running on BusyBox/Buildroot"
208-
singularity exec --writable --contain $new_container_name /bin/sh -c "find / -executable -perm -u+x,o-x -not -path '/dev*' -not -path '/proc*' -not -path '/sys*' -exec chmod a+x '{}' \;"
209-
fi
195+
echo "(6/9) Fixing permissions..."
210196

211-
echo "(8/9) Stopping and removing the container..."
197+
find ${build_sandbox}/* -maxdepth 0 -not -path '${build_sandbox}/dev*' -not -path '${build_sandbox}/proc*' -not -path '${build_sandbox}/sys*' -exec chmod a+r -R '{}' \;
198+
find ${build_sandbox}/* -type f -or -type d -perm -u+x,o-x -not -path '${build_sandbox}/dev*' -not -path '${build_sandbox}/proc*' -not -path '${build_sandbox}/sys*' -exec chmod a+x '{}' \;
199+
200+
echo "(7/9) Stopping and removing the container..."
212201
docker stop $container_id
213202
docker rm $container_id
214203

204+
# Build a final image from the sandbox
205+
echo "(8/9) Building..."
206+
singularity build $new_container_name $build_sandbox
207+
215208
echo "(9/9) Moving the image to the output folder..."
216-
rsync --info=progress2 /tmp/$image_name-$creation_date-$container_id.img /output/
209+
rsync --info=progress2 $new_container_name /output/

0 commit comments

Comments
 (0)