Skip to content

Commit 739e6a1

Browse files
committed
adding labels!
1 parent 9f3e9cb commit 739e6a1

File tree

2 files changed

+113
-31
lines changed

2 files changed

+113
-31
lines changed

README.md

Lines changed: 68 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,18 @@ OPTIONS:
1414
-w: non-production writable image (ext3)
1515
1616
Default is squashfs (recommended)
17+
1718
```
1819

1920
### Options
2021

2122
**Image Format**
2223

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.
24+
- `squashfs` (no arguments specified) gives you a squashfs (`*.simg`) image. This is a compressed, reliable, and read only format that is recommended for production images.
25+
- `sandbox` (`-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.
26+
- `ext3` (`-w`) builds an older format (ext3) image (`*.img`). 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.
2627

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+
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.
2829

2930
**Mount Points**
3031

@@ -51,21 +52,6 @@ Intermediate versions built on [Docker Hub](https://hub.docker.com/r/singularity
5152

5253
## Examples
5354

54-
No need to download anything from this repository! Simply type:
55-
56-
```
57-
docker run \
58-
-v /var/run/docker.sock:/var/run/docker.sock \
59-
-v D:\host\path\where\to\output\singularity\image:/output \
60-
--privileged -t --rm \
61-
singularityware/docker2singularity:2.4 \
62-
ubuntu:14.04
63-
```
64-
65-
Replace `D:\host\path\where\to\output\singularity\image` with a path on the host filesystem where your Singularity image will be created. Replace `ubuntu:14.04` with the docker image name you wish to convert (it will be pulled from Docker Hub if it does not exist on your host system).
66-
67-
`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.
68-
6955
## Build a Squashfs Image
7056
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:
7157

@@ -75,14 +61,76 @@ docker run -v /var/run/docker.sock:/var/run/docker.sock \
7561
--privileged -t --rm \
7662
singularityware/docker2singularity:2.4
7763
ubuntu:14.04
78-
```
64+
65+
Image Format: squashfs
66+
Inspected Size: 188 MB
67+
68+
(1/10) Creating a build sandbox...
69+
(2/10) Exporting filesystem...
70+
(3/10) Creating labels...
71+
(4/10) Adding run script...
72+
(5/10) Setting ENV variables...
73+
(6/10) Adding mount points...
74+
(7/10) Fixing permissions...
75+
(8/10) Stopping and removing the container...
76+
(9/10) Building squashfs container...
77+
Building image from sandbox: /tmp/ubuntu_14.04-2017-09-13-3e51deeadc7b.build
78+
Building Singularity image...
79+
Singularity container built: /tmp/ubuntu_14.04-2017-09-13-3e51deeadc7b.simg
80+
Cleaning up...
81+
(10/10) Moving the image to the output folder...
82+
62,591,007 100% 340.92MB/s 0:00:00 (xfr#1, to-chk=0/1)
83+
Final Size: 60MB
84+
```
85+
86+
Notice how the image went from 188MB to 60MB? This reduction is even more impressive when we are dealing with
87+
very large images (e.g., ~3600 down to ~1800). A few notes on the inputs shown above that you should edit:
88+
89+
- `/host/path/change/me`: the path you want to have the final image reside. If you are on windows this might look like `D:\host\path\where\to\output\singularity\image`.
90+
-`ubuntu:14.04`: the docker image name you wish to convert (it will be pulled from Docker Hub if it does not exist on your host system).
91+
92+
`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.
93+
7994
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.
8095

8196
```
8297
sudo singularity build --sandbox sandbox/ production.simg
8398
sudo singularity build --writable ext3.img production.simg
8499
```
85100

101+
## Inspect Your Image
102+
New with `docker2singularity` 2.4, the labels for the container are available with `inspect`:
103+
104+
```
105+
singularity inspect ubuntu_14.04-2017-09-13-3e51deeadc7b.simg
106+
{
107+
"org.label-schema.singularity.build": "squashfs",
108+
"org.label-schema.docker.version": "17.06.2-ce",
109+
"org.label-schema.schema-version": "1.0",
110+
"org.label-schema.singularity.build-type": "docker2singularity",
111+
"org.label-schema.docker.id": "sha256:dea1945146b96542e6e20642830c78df702d524a113605a906397db1db022703",
112+
"org.label-schema.build-date": "2017-10-28-17:19:18",
113+
"org.label-schema.singularity.version": "2.4-dist",
114+
"org.label-schema.docker.created": "2017-09-13"
115+
}
116+
```
117+
118+
as is the runscript and environment
119+
120+
```
121+
singularity inspect --json -e -r ubuntu_14.04-2017-09-13-3e51deeadc7b.simg
122+
{
123+
"data": {
124+
"attributes": {
125+
"environment": "# Custom environment shell code should follow\n\n",
126+
"runscript": "#!/bin/sh\n/bin/bash $@\n"
127+
},
128+
"type": "container"
129+
}
130+
}
131+
132+
```
133+
86134
## Build a Sandbox Image
87135
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:
88136

docker2singularity.sh

Lines changed: 45 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ mkdir -p $TMPDIR
134134
creation_date=`echo ${creation_date} | cut -c1-10`
135135
new_container_name=/tmp/$image_name-$creation_date-$container_id
136136
build_sandbox="${new_container_name}.build"
137-
echo "(1/9) Creating a build sandbox..."
137+
echo "(1/10) Creating a build sandbox..."
138138
mkdir -p ${build_sandbox}
139-
echo "(2/9) Exporting filesystem..."
139+
echo "(2/10) Exporting filesystem..."
140140
docker export $container_id >> $build_sandbox.tar
141141
singularity image.import $build_sandbox < $build_sandbox.tar
142142
docker inspect $container_id >> $build_sandbox/singularity.json
@@ -146,13 +146,46 @@ docker inspect $container_id >> $build_sandbox/singularity.json
146146
### METADATA ###################################################################
147147
################################################################################
148148

149+
# Quiet Singularity debug output when adding labels
150+
SINGULARITY_MESSAGELEVEL=0
151+
export SINGULARITY_MESSAGELEVEL
152+
149153
# For docker2singularity, installation is at /usr/local
150-
zcat /usr/local/libexec/singularity/bootstrap-scripts/environment.tar | ( cd $build_sandbox; tar -xf - >/dev/null)
154+
echo "(3/10) Creating labels..."
155+
libexec="/usr/local/libexec/singularity"
156+
zcat "${libexec}/bootstrap-scripts/environment.tar" | ( cd $build_sandbox; tar -xf - >/dev/null)
157+
LABELS=$(docker inspect --format='{{json .Config.Labels}}' $image)
158+
LABELFILE=$(printf "%q" "$build_sandbox/.singularity.d/labels.json")
159+
ADD_LABEL="${libexec}/python/helpers/json/add.py -f --file ${LABELFILE}"
160+
161+
# Labels could be null
162+
if [ "${LABELS}" == "null" ]; then
163+
LABELS="{}"
164+
fi
165+
166+
# Extract some other "nice to know" metadata from docker
167+
SINGULARITY_version=`singularity --version`
168+
SINGULARITY_VERSION=$(printf "%q" "$SINGULARITY_version")
169+
DOCKER_VERSION=$(docker inspect --format='{{json .DockerVersion}}' $image)
170+
DOCKER_ID=$(docker inspect --format='{{json .Id}}' $image)
171+
172+
# Add labels from Docker, then relevant to Singularity build
173+
echo $LABELS > $LABELFILE;
174+
eval $ADD_LABEL --key "org.label-schema.schema-version" --value "1.0"
175+
eval $ADD_LABEL --key "org.label-schema.singularity.build-type" --value "docker2singularity"
176+
eval $ADD_LABEL --key "org.label-schema.singularity.build" --value "${image_format}"
177+
eval $ADD_LABEL --key "org.label-schema.build-date" --value $(date +%Y-%m-%d-%H:%M:%S)
178+
eval $ADD_LABEL --key "org.label-schema.singularity.version" --value "${SINGULARITY_VERSION}"
179+
eval $ADD_LABEL --key "org.label-schema.docker.version" --value "${DOCKER_VERSION}"
180+
eval $ADD_LABEL --key "org.label-schema.docker.Created" --value "${creation_date}"
181+
eval $ADD_LABEL --key "org.label-schema.docker.Id" --value "${DOCKER_ID}"
182+
183+
unset SINGULARITY_MESSAGELEVEL
151184

152185
################################################################################
153186
### SINGULARITY RUN SCRIPT #####################################################
154187
################################################################################
155-
echo "(3/9) Adding run script..."
188+
echo "(4/10) Adding run script..."
156189
CMD=$(docker inspect --format='{{json .Config.Cmd}}' $image)
157190
if [[ $CMD != [* ]]; then
158191
if [[ $CMD != "null" ]]; then
@@ -187,7 +220,8 @@ chmod +x $build_sandbox/.singularity.d/runscript;
187220
################################################################################
188221
### SINGULARITY ENVIRONMENT ####################################################
189222
################################################################################
190-
echo "(4/9) Setting ENV variables..."
223+
224+
echo "(5/10) Setting ENV variables..."
191225
docker run --rm --entrypoint="/usr/bin/env" $image > $TMPDIR/docker_environment
192226
# do not include HOME and HOSTNAME - they mess with local config
193227
sed -i '/^HOME/d' $TMPDIR/docker_environment
@@ -205,24 +239,24 @@ rm -rf $TMPDIR
205239
### Permissions ################################################################
206240
################################################################################
207241
if [ "${mount_points}" ] ; then
208-
echo "(5/9) Adding mount points..."
242+
echo "(6/10) Adding mount points..."
209243
mkdir -p "${build_sandbox}/${mount_points}"
210244
else
211-
echo "(5/9) Skipping mount points..."
245+
echo "(6/10) Skipping mount points..."
212246
fi
213247

214248
# making sure that any user can read and execute everything in the container
215-
echo "(6/9) Fixing permissions..."
249+
echo "(7/10) Fixing permissions..."
216250

217251
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 '{}' \;
218252
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 '{}' \;
219253

220-
echo "(7/9) Stopping and removing the container..."
254+
echo "(8/10) Stopping and removing the container..."
221255
docker stop $container_id >> /dev/null
222256
docker rm $container_id >> /dev/null
223257

224258
# Build a final image from the sandbox
225-
echo "(8/9) Building ${image_format} container..."
259+
echo "(9/10) Building ${image_format} container..."
226260
if [ "$image_format" == "squashfs" ]; then
227261
new_container_name=${new_container_name}.simg
228262
singularity build ${new_container_name} $build_sandbox
@@ -233,7 +267,7 @@ else
233267
mv $build_sandbox $new_container_name
234268
fi
235269

236-
echo "(9/9) Moving the image to the output folder..."
270+
echo "(10/10) Moving the image to the output folder..."
237271
finalsize=`du -shm $new_container_name | cut -f1`
238272
rsync --info=progress2 -a $new_container_name /output/
239273
echo "Final Size: ${finalsize}MB"

0 commit comments

Comments
 (0)