Skip to content

Commit 9f3e9cb

Browse files
committed
fixing up script to allow for multiple formats and correct metadata creation
1 parent 146539a commit 9f3e9cb

File tree

2 files changed

+144
-24
lines changed

2 files changed

+144
-24
lines changed

README.md

Lines changed: 92 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,43 @@
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? 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:
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 gives you a selection of image formats to build.
4+
5+
## Usage
6+
7+
```
8+
docker run docker2singularity
9+
USAGE: docker2singularity [-m "/mount_point1 /mount_point2"] [options] docker_image_name
10+
OPTIONS:
11+
12+
Image Format
13+
-f: build development sandbox (folder)
14+
-w: non-production writable image (ext3)
15+
16+
Default is squashfs (recommended)
17+
```
18+
19+
### Options
20+
21+
**Image Format**
22+
23+
- `default` (no arguments specified) gives you a squashfs (`*.simg`) image. This is a compressed, reliable, and read only format that is recommended for production images.
24+
- `-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.
25+
- `-w` builds an older format (ext3) image (`*.img`) with `--writable`. 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.
26+
27+
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.
28+
29+
**Mount Points**
30+
31+
- `-m` specify one or more mount points to create in the image.
32+
33+
**Image Name**
34+
35+
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`)
36+
37+
38+
## Legacy
39+
40+
If you want a legacy version, see the following other branches:
441

542
- [v2.3](https://github.com/singularityware/docker2singularity/tree/v2.3): Version 2.3 of Singularity. The image format is ext3.
643
- [v2.4](https://github.com/singularityware/docker2singularity/tree/v2.4): Version 2.4 of Singularity. The default image format is squashfs.
@@ -12,7 +49,7 @@ Intermediate versions built on [Docker Hub](https://hub.docker.com/r/singularity
1249
- Docker (native Linux or Docker for Mac or Docker for Windows) - to create the Singularity image.
1350
- 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.
1451

15-
## Usage
52+
## Examples
1653

1754
No need to download anything from this repository! Simply type:
1855

@@ -29,6 +66,58 @@ Replace `D:\host\path\where\to\output\singularity\image` with a path on the host
2966

3067
`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.
3168

69+
## Build a Squashfs Image
70+
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:
71+
72+
```
73+
docker run -v /var/run/docker.sock:/var/run/docker.sock \
74+
-v /host/path/change/me:/output \
75+
--privileged -t --rm \
76+
singularityware/docker2singularity:2.4
77+
ubuntu:14.04
78+
```
79+
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.
80+
81+
```
82+
sudo singularity build --sandbox sandbox/ production.simg
83+
sudo singularity build --writable ext3.img production.simg
84+
```
85+
86+
## Build a Sandbox Image
87+
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:
88+
89+
```
90+
docker run -v /var/run/docker.sock:/var/run/docker.sock \
91+
-v /host/path/change/me:/output \
92+
--privileged -t --rm \
93+
singularityware/docker2singularity:2.4 \
94+
-f \
95+
ubuntu:14.04
96+
```
97+
Importantly, you can use `--writable`, and if needed, you can convert a sandbox folder into a production image:
98+
99+
```
100+
sudo singularity build sandbox/ production.simg
101+
```
102+
103+
## Build a Legacy (ext3) Image
104+
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.
105+
106+
```
107+
docker run -v /var/run/docker.sock:/var/run/docker.sock \
108+
-v /host/path/change/me:/output \
109+
--privileged -t --rm \
110+
singularityware/docker2singularity:2.4 \
111+
-w \
112+
ubuntu:14.04
113+
```
114+
You can also use `--writable` and convert an ext3 image into a production image:
115+
116+
```
117+
sudo singularity build ext3.img production.simg
118+
```
119+
120+
32121
## Tips for making Docker images compatible with Singularity
33122

34123
- Define all environmental variables using the `ENV` instruction set. Do not rely on `.bashrc`, `.profile`, etc.
@@ -68,5 +157,6 @@ If you are getting `WARNING: Non existant bind point (directory) in container: '
68157
-m "/shared_fs /custom_mountpoint2" \
69158
ubuntu:14.04
70159

160+
71161
## Acknowledgements
72162
This work is heavily based on the `docker2singularity` work done by [vsoch](https://github.com/vsoch) and [gmkurtzer](https://github.com/gmkurtzer). Hopefully most of the conversion code will be merged into Singularity in the future making this container even leaner!

docker2singularity.sh

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,36 @@
3636
set -o errexit
3737
set -o nounset
3838

39-
USAGE="Usage: docker2singularity [-m \"/mount_point1 /mount_point2\"] docker_image_name"
39+
USAGE="USAGE: docker2singularity [-m \"/mount_point1 /mount_point2\"] [options] docker_image_name"
4040

4141
# --- Option processing --------------------------------------------
4242
if [ $# == 0 ] ; then
4343
echo $USAGE
44+
echo "OPTIONS:
45+
46+
Image Format
47+
-f: build development sandbox (folder)
48+
-w: non-production writable image (ext3)
49+
50+
Default is squashfs (recommended)
51+
"
52+
4453
exit 1;
4554
fi
4655

4756
mount_points="/oasis /projects /scratch /local-scratch /work /home1 /corral-repl /corral-tacc /beegfs /share/PI /extra /data /oak"
48-
while getopts ':hm:' option; do
57+
image_format="squashfs"
58+
while getopts ':hm:wf' option; do
4959
case "$option" in
5060
h) echo "$USAGE"
51-
exit
61+
exit 0
5262
;;
5363
m) mount_points=$OPTARG
5464
;;
65+
f) image_format="sandbox"
66+
;;
67+
w) image_format="writable"
68+
;;
5569
:) printf "missing argument for -%s\n" "$OPTARG" >&2
5670
echo "$usage" >&2
5771
exit 1
@@ -66,6 +80,9 @@ shift $((OPTIND - 1))
6680

6781
image=$1
6882

83+
echo ""
84+
echo "Image Format: ${image_format}"
85+
6986
################################################################################
7087
### CONTAINER RUNNING ID #######################################################
7188
################################################################################
@@ -103,14 +120,10 @@ creation_date=`docker inspect --format="{{.Created}}" $image`
103120
################################################################################
104121

105122
size=`docker inspect --format="{{.Size}}" $image`
106-
# convert size in MB (it seems too small for singularity containers ...?). Add 1MB to round up (minimum).
123+
# convert size in MB
107124
size=`echo $(($size/1000000+1))`
108-
# adding half of the container size seems to work (do not know why exactly...?)
109-
# I think it would be Ok by adding 1/3 of the size.
110-
size=`echo $(($size+$size/2))`
111-
112-
echo "Size: $size MB for the singularity container, although not needed for squashfs."
113-
125+
echo "Inspected Size: $size MB"
126+
echo ""
114127

115128
################################################################################
116129
### IMAGE CREATION #############################################################
@@ -119,7 +132,7 @@ TMPDIR=$(mktemp -u -d)
119132
mkdir -p $TMPDIR
120133

121134
creation_date=`echo ${creation_date} | cut -c1-10`
122-
new_container_name=/tmp/$image_name-$creation_date-$container_id.simg
135+
new_container_name=/tmp/$image_name-$creation_date-$container_id
123136
build_sandbox="${new_container_name}.build"
124137
echo "(1/9) Creating a build sandbox..."
125138
mkdir -p ${build_sandbox}
@@ -129,6 +142,13 @@ singularity image.import $build_sandbox < $build_sandbox.tar
129142
docker inspect $container_id >> $build_sandbox/singularity.json
130143

131144

145+
################################################################################
146+
### METADATA ###################################################################
147+
################################################################################
148+
149+
# For docker2singularity, installation is at /usr/local
150+
zcat /usr/local/libexec/singularity/bootstrap-scripts/environment.tar | ( cd $build_sandbox; tar -xf - >/dev/null)
151+
132152
################################################################################
133153
### SINGULARITY RUN SCRIPT #####################################################
134154
################################################################################
@@ -152,16 +172,16 @@ fi
152172
# Remove quotes, commas, and braces
153173
ENTRYPOINT=`echo "${ENTRYPOINT//\"/}" | sed 's/\[//g' | sed 's/\]//g' | sed 's/,/ /g'`
154174

155-
echo '#!/bin/sh' > $build_sandbox/singularity
175+
echo '#!/bin/sh' > $build_sandbox/.singularity.d/runscript
156176
if [[ $ENTRYPOINT != "null" ]]; then
157-
echo $ENTRYPOINT '$@' >> $build_sandbox/singularity;
177+
echo $ENTRYPOINT '$@' >> $build_sandbox/.singularity.d/runscript;
158178
else
159179
if [[ $CMD != "null" ]]; then
160-
echo $CMD '$@' >> $build_sandbox/singularity;
180+
echo $CMD '$@' >> $build_sandbox/.singularity.d/runscript;
161181
fi
162182
fi
163183

164-
chmod +x $build_sandbox/singularity
184+
chmod +x $build_sandbox/.singularity.d/runscript;
165185

166186

167187
################################################################################
@@ -176,8 +196,8 @@ sed -i 's/^/export /' $TMPDIR/docker_environment
176196
# add quotes around the variable names
177197
sed -i 's/=/="/' $TMPDIR/docker_environment
178198
sed -i 's/$/"/' $TMPDIR/docker_environment
179-
cp $TMPDIR/docker_environment $build_sandbox/docker_environment
180-
echo '. /docker_environment' >> $build_sandbox/environment
199+
cp $TMPDIR/docker_environment $build_sandbox/.singularity.d/env/10-docker.sh
200+
chmod +x $build_sandbox/.singularity.d/env/10-docker.sh;
181201
rm -rf $TMPDIR
182202

183203

@@ -198,12 +218,22 @@ find ${build_sandbox}/* -maxdepth 0 -not -path '${build_sandbox}/dev*' -not -pat
198218
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 '{}' \;
199219

200220
echo "(7/9) Stopping and removing the container..."
201-
docker stop $container_id
202-
docker rm $container_id
221+
docker stop $container_id >> /dev/null
222+
docker rm $container_id >> /dev/null
203223

204224
# Build a final image from the sandbox
205-
echo "(8/9) Building..."
206-
singularity build $new_container_name $build_sandbox
225+
echo "(8/9) Building ${image_format} container..."
226+
if [ "$image_format" == "squashfs" ]; then
227+
new_container_name=${new_container_name}.simg
228+
singularity build ${new_container_name} $build_sandbox
229+
elif [ "$image_format" == "writable" ]; then
230+
new_container_name=${new_container_name}.img
231+
singularity build --writable ${new_container_name} $build_sandbox
232+
else
233+
mv $build_sandbox $new_container_name
234+
fi
207235

208236
echo "(9/9) Moving the image to the output folder..."
209-
rsync --info=progress2 $new_container_name /output/
237+
finalsize=`du -shm $new_container_name | cut -f1`
238+
rsync --info=progress2 -a $new_container_name /output/
239+
echo "Final Size: ${finalsize}MB"

0 commit comments

Comments
 (0)