Skip to content

Commit 17cf023

Browse files
committed
fixing up argument parsing to allow for custom image naming to close #29
1 parent 60c099f commit 17cf023

File tree

3 files changed

+89
-28
lines changed

3 files changed

+89
-28
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ represented by the pull requests that fixed them. Critical items to know are:
1515
This changelog was started with version of Singularity v2.5, and reflects changes since then.
1616

1717
## [master](https://github.com/singularityware/docker2singularity/tree/master) (master)
18-
- update of Singularity from [v2.4](https://github.com/singularityware/docker2singularity/tree/v2.4) to [v2.5](https://github.com/singularityware/docker2singularity/tree/v2.5), including adding libarchive dependency (v2.5)
18+
- update of Singularity from [v2.4](https://github.com/singularityware/docker2singularity/tree/v2.4) to [v2.5](https://github.com/singularityware/docker2singularity/tree/v2.5), including adding libarchive dependency, and custom name with -n (v2.5)

README.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ $ mkdir -p /tmp/test
6565
And here is the command to run. Notice that I am mounting the path `/tmp/test` that I created above to `/output` in the container, where the container image will be written (and seen on my host).
6666

6767
```bash
68-
docker run -v /var/run/docker.sock:/var/run/docker.sock \
68+
$ docker run -v /var/run/docker.sock:/var/run/docker.sock \
6969
-v /tmp/test:/output \
7070
--privileged -t --rm \
7171
singularityware/docker2singularity \
@@ -124,6 +124,23 @@ sudo singularity build --sandbox sandbox/ production.simg
124124
sudo singularity build --writable ext3.img production.simg
125125
```
126126

127+
## Custom Naming
128+
Added for version 2.5.1, you can specify the name of your container with the `-n/--name` argument, as follows:
129+
130+
```bash
131+
docker run -v /var/run/docker.sock:/var/run/docker.sock \
132+
-v /tmp/test:/output \
133+
--privileged -t --rm \
134+
singularityware/docker2singularity \
135+
--name meatballs ubuntu:14.04
136+
137+
...
138+
139+
$ ls /tmp/test/
140+
meatballs.simg
141+
```
142+
143+
127144
## Inspect Your Image
128145
New with `docker2singularity` 2.4, the labels for the container are available with `inspect`:
129146

docker2singularity.sh

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

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

4141
# --- Option processing --------------------------------------------
4242
if [ $# == 0 ] ; then
43-
echo $USAGE
43+
echo "${usage}"
4444
echo "OPTIONS:
4545
4646
Image Format
4747
-f: build development sandbox (folder)
4848
-w: non-production writable image (ext3)
49-
50-
Default is squashfs (recommended)
49+
Default is squashfs (recommended)
50+
-n: provide basename for the container (default based on URI)
5151
"
5252

5353
exit 1;
5454
fi
5555

5656
mount_points="/oasis /projects /scratch /local-scratch /work /home1 /corral-repl /corral-tacc /beegfs /share/PI /extra /data /oak"
5757
image_format="squashfs"
58-
while getopts ':hm:wf' option; do
59-
case "$option" in
60-
h) echo "$USAGE"
61-
exit 0
62-
;;
63-
m) mount_points=$OPTARG
64-
;;
65-
f) image_format="sandbox"
66-
;;
67-
w) image_format="writable"
68-
;;
69-
:) printf "missing argument for -%s\n" "$OPTARG" >&2
70-
echo "$usage" >&2
71-
exit 1
72-
;;
73-
\?) printf "illegal option: -%s\n" "$OPTARG" >&2
74-
echo "$usage" >&2
75-
exit 1
76-
;;
77-
esac
58+
new_container_name=""
59+
60+
while true; do
61+
case ${1:-} in
62+
-h|--help|help)
63+
echo "${usage}"
64+
exit 0
65+
;;
66+
-n|--name)
67+
shift
68+
new_container_name="${1:-}"
69+
shift
70+
;;
71+
-m|--mount)
72+
shift
73+
mount_points="${1:-}"
74+
shift
75+
;;
76+
-f|--folder)
77+
shift
78+
image_format="sandbox"
79+
shift
80+
;;
81+
-s|--sandbox)
82+
shift
83+
image_format="writable"
84+
shift
85+
;;
86+
:) printf "missing argument for -%s\n" "$option" >&2
87+
echo "$usage" >&2
88+
exit 1
89+
;;
90+
\?) printf "illegal option: -%s\n" "$option" >&2
91+
echo "$usage" >&2
92+
exit 1
93+
;;
94+
-*)
95+
printf "illegal option: -%s\n" "$option" >&2
96+
echo "$usage" >&2
97+
exit 1
98+
;;
99+
*)
100+
break;
101+
;;
102+
esac
78103
done
79-
shift $((OPTIND - 1))
80104

81105
image=$1
82106

83107
echo ""
84108
echo "Image Format: ${image_format}"
109+
echo "Docker Image: ${image}"
110+
111+
if [ "${new_container_name}" != "" ]; then
112+
echo "Container Name: ${new_container_name}"
113+
fi
114+
115+
echo ""
85116

86117
################################################################################
87118
### CONTAINER RUNNING ID #######################################################
@@ -132,8 +163,21 @@ TMPDIR=$(mktemp -u -d)
132163
mkdir -p $TMPDIR
133164

134165
creation_date=`echo ${creation_date} | cut -c1-10`
135-
new_container_name=/tmp/$image_name-$creation_date-$container_id
166+
167+
# The user has not provided a custom name
168+
if [ "${new_container_name}" == "" ]; then
169+
new_container_name=/tmp/$image_name-$creation_date-$container_id
170+
171+
# The user has provided a custom name
172+
else
173+
new_container_name=/tmp/$(basename $new_container_name)
174+
new_container_name="${new_container_name%.*}"
175+
fi
176+
177+
136178
build_sandbox="${new_container_name}.build"
179+
180+
137181
echo "(1/10) Creating a build sandbox..."
138182
mkdir -p ${build_sandbox}
139183
echo "(2/10) Exporting filesystem..."

0 commit comments

Comments
 (0)