-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
docker2singularity.sh
executable file
·333 lines (274 loc) · 12.2 KB
/
docker2singularity.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#! /bin/bash
#
# docker2singularity.sh will convert a docker image into a singularity
# Must be run with sudo to use docker commands (eg aufs)
#
# NOTES:
# If the docker image uses both ENTRYPOINT and CMD the latter will be ignored
#
# KNOWN ISSUES:
# Currently ENTRYPOINTs and CMDs with commas in the arguments are not supported
#
# USAGE: docker2singularity.sh ubuntu:14.04
#
#
# Copyright (c) 2016-2024 Vanessa Sochat, All Rights Reserved
# Copyright (c) 2017 Singularityware LLC and AUTHORS
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -o errexit
set -o nounset
function usage() {
echo "USAGE: docker2singularity [-m \"/mount_point1 /mount_point2\"] [options] docker_image_name"
echo ""
echo "OPTIONS:
Image Format
--folder -f build development sandbox (folder)
--option -o add a custom option to build (-o --fakeroot or -option 'section post' )
--writable -w non-production writable image (ext3)
Default is squashfs (recommended) (deprecated)
--name -n provide basename for the container (default based on URI)
--mount -m provide list of custom mount points (in quotes!)
--help -h show this help and exit
"
}
# --- Option processing --------------------------------------------
if [ $# == 0 ] ; then
usage
exit 0;
fi
mount_points="/oasis /projects /scratch /local-scratch /work /home1 /corral-repl /corral-tacc /beegfs /share/PI /extra /data /oak"
image_format="squashfs"
new_container_name=""
options=""
while true; do
case ${1:-} in
-h|--help|help)
usage
exit 0
;;
-n|--name)
shift
new_container_name="${1:-}"
shift
;;
-m|--mount)
shift
mount_points="${1:-}"
shift
;;
-o|--option)
shift
options="${1:-} ${options}"
shift
;;
-f|--folder)
shift
image_format="sandbox"
;;
-w|--writable)
shift
image_format="writable"
;;
:) printf "missing argument for -%s\n" "$option" >&2
usage
exit 1
;;
\?) printf "illegal option: -%s\n" "$option" >&2
usage
exit 1
;;
-*)
printf "illegal option: -%s\n" "$option" >&2
usage
exit 1
;;
*)
break;
;;
esac
done
image=${1}
echo ""
echo "Image Format: ${image_format}"
echo "Docker Image: ${image}"
if [ "${new_container_name}" != "" ]; then
echo "Container Name: ${new_container_name}"
fi
echo ""
################################################################################
### CONTAINER RUNNING ID #######################################################
################################################################################
runningid=`docker run -d $image tail -f /dev/null`
# Full id looks like
# sha256:d59bdb51bb5c4fb7b2c8d90ae445e0720c169c553bcf553f67cb9dd208a4ec15
# Take the first 12 characters to get id of container
container_id=`echo ${runningid} | cut -c1-12`
# Network address, if needed
network_address=`docker inspect --format="{{.NetworkSettings.IPAddress}}" $container_id`
################################################################################
### IMAGE NAME #################################################################
################################################################################
image_name=`docker inspect --format="{{.Config.Image}}" $container_id`
# using bash substitution
# removing special chars [perhaps echo + sed would be better for other chars]
image_name=${image_name//\//_}
image_name=${image_name/:/_}
# following is the date of the container, not the docker image.
#creation_date=`docker inspect --format="{{.Created}}" $container_id`
creation_date=`docker inspect --format="{{.Created}}" $image`
################################################################################
### IMAGE SIZE #################################################################
################################################################################
size=`docker inspect --format="{{.Size}}" $image`
# convert size in MB
size=`echo $(($size/1000000+1))`
echo "Inspected Size: $size MB"
echo ""
################################################################################
### IMAGE CREATION #############################################################
################################################################################
TMPDIR=$(mktemp -u -d)
mkdir -p $TMPDIR
creation_date=`echo ${creation_date} | cut -c1-10`
# The user has not provided a custom name
if [ "${new_container_name}" == "" ]; then
new_container_name=/tmp/$image_name-$creation_date-$container_id
# The user has provided a custom name
else
new_container_name=/tmp/$(basename $new_container_name)
new_container_name="${new_container_name%.*}"
fi
build_sandbox="${new_container_name}.build"
echo "(1/10) Creating a build sandbox..."
mkdir -p ${build_sandbox}
echo "(2/10) Exporting filesystem..."
docker export $container_id >> $build_sandbox.tar
tar -C $build_sandbox -xf $build_sandbox.tar
docker inspect $container_id >> $build_sandbox/singularity.json
################################################################################
### METADATA ###################################################################
################################################################################
# Quiet Singularity debug output when adding labels
SINGULARITY_MESSAGELEVEL=0
export SINGULARITY_MESSAGELEVEL
# For docker2singularity, installation is at /usr/local
echo "(3/10) Creating labels..."
LABELS=$(docker inspect --format='{{json .Config.Labels}}' $image)
LABELFILE=$(printf "%q" "$build_sandbox/.singularity.d/labels.json")
ADD_LABEL="/addLabel.py -f --file ${LABELFILE}"
# Labels could be null
if [ "${LABELS}" == "null" ]; then
LABELS="{}"
fi
mkdir -p $build_sandbox/.singularity.d
touch ${LABELFILE}
# Extract some other "nice to know" metadata from docker
SINGULARITY_version=`singularity --version`
SINGULARITY_VERSION=$(printf "%q" "$SINGULARITY_version")
DOCKER_VERSION=$(docker inspect --format='{{json .DockerVersion}}' $image)
DOCKER_ID=$(docker inspect --format='{{json .Id}}' $image)
# Add labels from Docker, then relevant to Singularity build
echo $LABELS > $LABELFILE;
eval $ADD_LABEL --key "org.label-schema.schema-version" --value "1.0"
eval $ADD_LABEL --key "org.label-schema.singularity.build-type" --value "docker2singularity"
eval $ADD_LABEL --key "org.label-schema.singularity.build" --value "${image_format}"
eval $ADD_LABEL --key "org.label-schema.build-date" --value $(date +%Y-%m-%d-%H:%M:%S)
eval $ADD_LABEL --key "org.label-schema.singularity.version" --value "${SINGULARITY_VERSION}"
eval $ADD_LABEL --key "org.label-schema.docker.version" --value "${DOCKER_VERSION}"
eval $ADD_LABEL --key "org.label-schema.docker.Created" --value "${creation_date}"
eval $ADD_LABEL --key "org.label-schema.docker.Id" --value "${DOCKER_ID}"
unset SINGULARITY_MESSAGELEVEL
################################################################################
### SINGULARITY RUN SCRIPT #####################################################
################################################################################
echo "(4/10) Adding run script..."
function shell_escape () {
python -c 'import json, pipes, sys; print(" ".join(pipes.quote(a) for a in json.load(sys.stdin) or []))'
}
CMD=$(docker inspect --format='{{json .Config.Cmd}}' $image | shell_escape)
ENTRYPOINT=$(docker inspect --format='{{json .Config.Entrypoint}}' $image | shell_escape)
echo '#!/bin/sh' > $build_sandbox/.singularity.d/runscript
# Take working directory into account
WORKINGDIR=$(docker inspect --format='{{json .Config.WorkingDir}}' $image)
if [[ $WORKINGDIR != '""' ]]; then
echo cd $WORKINGDIR >> $build_sandbox/.singularity.d/runscript
fi
# First preference goes to both entrypoint / cmd, then individual
if [ -n "$ENTRYPOINT" ] && [ -n "$CMD" ]; then
echo exec "$ENTRYPOINT" "$CMD" '"$@"' >> $build_sandbox/.singularity.d/runscript;
elif [ -n "$ENTRYPOINT" ]; then
echo exec "$ENTRYPOINT" '"$@"' >> $build_sandbox/.singularity.d/runscript;
elif [ -n "$CMD" ]; then
echo exec "$CMD" '"$@"' >> $build_sandbox/.singularity.d/runscript;
fi
chmod +x $build_sandbox/.singularity.d/runscript;
################################################################################
### SINGULARITY ENVIRONMENT ####################################################
################################################################################
echo "(5/10) Setting ENV variables..."
# Removed copying of template files to avoid scripts permissions issue (see #66)
# Create env scripts directory
mkdir -p $build_sandbox/.singularity.d/env
chmod 755 $build_sandbox/.singularity.d/env
# Then customize by adding Docker variables
docker run --rm --entrypoint="/usr/bin/env" $image > $TMPDIR/docker_environment
# do not include HOME and HOSTNAME - they mess with local config
sed -i '/^HOME/d' $TMPDIR/docker_environment
sed -i '/^HOSTNAME/d' $TMPDIR/docker_environment
sed -i 's/^/export /' $TMPDIR/docker_environment
# add quotes around the variable names
sed -i 's/=/="/' $TMPDIR/docker_environment
sed -i 's/$/"/' $TMPDIR/docker_environment
cp $TMPDIR/docker_environment $build_sandbox/.singularity.d/env/10-docker.sh
chmod +x $build_sandbox/.singularity.d/env/10-docker.sh;
rm -rf $TMPDIR
################################################################################
### Permissions ################################################################
################################################################################
if [ "${mount_points}" ] ; then
echo "(6/10) Adding mount points..."
for mount_point in ${mount_points}; do
mkdir -p "${build_sandbox}/${mount_point}"
done
else
echo "(6/10) Skipping mount points..."
fi
# making sure that any user can read and execute everything in the container
echo "(7/10) Fixing permissions..."
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 '{}' \;
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 '{}' \;
echo "(8/10) Stopping and removing the container..."
docker stop $container_id >> /dev/null
docker rm $container_id >> /dev/null
# Build a final image from the sandbox
echo "(9/10) Building ${image_format} container..."
if [ "$image_format" == "squashfs" ]; then
new_container_name=${new_container_name}.sif
singularity build ${options} ${new_container_name} $build_sandbox
elif [ "$image_format" == "writable" ]; then
new_container_name=${new_container_name}.simg
singularity build ${options} --writable ${new_container_name} $build_sandbox
else
mv $build_sandbox $new_container_name
fi
echo "(10/10) Moving the image to the output folder..."
finalsize=`du -shm $new_container_name | cut -f1`
rsync --info=progress2 -a $new_container_name /output/
echo "Final Size: ${finalsize}MB"