This repository has been archived by the owner on Oct 15, 2024. It is now read-only.
forked from iot-salzburg/gpu-jupyter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
generate-Dockerfile.sh
executable file
·218 lines (197 loc) · 10.3 KB
/
generate-Dockerfile.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
#!/usr/bin/env bash
cd $(cd -P -- "$(dirname -- "$0")" && pwd -P)
# Set the path of the generated Dockerfile
export DOCKERFILE=".build/Dockerfile"
export STACKS_DIR=".build/docker-stacks"
# please test the build of the commit in https://github.com/jupyter/docker-stacks/commits/main in advance
export HEAD_COMMIT="e838ff397a2d9c2ad0faae051ef0ec4f20732320"
while [[ "$#" -gt 0 ]]; do case $1 in
-p|--pw|--password) PASSWORD="$2" && USE_PASSWORD=1; shift;;
-c|--commit) HEAD_COMMIT="$2"; shift;;
--no-datascience-notebook) no_datascience_notebook=1;;
--python-only) no_datascience_notebook=1;;
--no-useful-packages) no_useful_packages=1;;
-s|--slim) no_datascience_notebook=1 && no_useful_packages=1;;
-h|--help) HELP=1;;
*) echo "Unknown parameter passed: $1" && HELP=1;;
esac; shift; done
if [[ "$HELP" == 1 ]]; then
echo "Help for ./generate-Dockerfile.sh:"
echo "Usage: $0 [parameters]"
echo " -h|--help: Show this help."
echo " -p|--pw|--password: Set the password (creates .build/jupyter_notebook_config.json and overrides the token)"
echo " -c|--commit: Set the head commit of the jupyter/docker-stacks submodule (https://github.com/jupyter/docker-stacks/commits/main). default: $HEAD_COMMIT."
echo " --python-only|--no-datascience-notebook: Use not the datascience-notebook from jupyter/docker-stacks, don't install Julia and R."
echo " --no-useful-packages: Don't install the useful packages, specified in custom/usefulpackages.Dockerfile"
echo " --slim: no useful packages and no datascience notebook."
exit 21
fi
# Clone if docker-stacks doesn't exist, and set to the given commit or the default commit
ls $STACKS_DIR/README.md > /dev/null 2>&1 || (echo "Docker-stacks was not found, cloning repository" \
&& git clone https://github.com/jupyter/docker-stacks.git $STACKS_DIR)
echo "Set docker-stacks to commit '$HEAD_COMMIT'."
if [[ "$HEAD_COMMIT" == "latest" ]]; then
echo "WARNING, the latest commit of docker-stacks is used. This may result in version conflicts"
cd $STACKS_DIR && git pull && cd -
else
export GOT_HEAD="false"
cd $STACKS_DIR && git pull && git reset --hard "$HEAD_COMMIT" > /dev/null 2>&1 && cd - && export GOT_HEAD="true"
echo "$HEAD"
if [[ "$GOT_HEAD" == "false" ]]; then
echo "Error: The provided sha-commit is invalid."
echo "Usage: $0 -c [sha-commit] # set the head commit of the docker-stacks submodule (https://github.com/jupyter/docker-stacks/commits/master)."
echo "Exiting"
exit 2
else
echo "Set head to given commit."
fi
fi
# Write the contents into the DOCKERFILE and start with the header
echo "# This Dockerfile is generated by 'generate-Dockerfile.sh' from elements within 'custom/'
# **Please do not change this file directly!**
# To adapt this Dockerfile, adapt 'generate-Dockerfile.sh' or 'custom/usefulpackages.Dockerfile'.
# More information can be found in the README under configuration.
" > $DOCKERFILE
cat custom/header.Dockerfile >> $DOCKERFILE
echo "
############################################################################
#################### Dependency: jupyter/docker-stacks-foundation ##########
############################################################################
" >> $DOCKERFILE
if [ -f "$STACKS_DIR/images/docker-stacks-foundation/Dockerfile" ]; then
cat $STACKS_DIR/images/docker-stacks-foundation/Dockerfile | grep -v 'BASE_CONTAINER' | grep -v 'FROM $ROOT_CONTAINER' >> $DOCKERFILE
# copy files that are used during the build
cp $STACKS_DIR/images/docker-stacks-foundation/initial-condarc .build/
cp $STACKS_DIR/images/docker-stacks-foundation/fix-permissions .build/
cp $STACKS_DIR/images/docker-stacks-foundation/start.sh .build/
cp $STACKS_DIR/images/docker-stacks-foundation/run-hooks.sh .build/
cp $STACKS_DIR/images/docker-stacks-foundation/10activate-conda-env.sh .build/
else
cat $STACKS_DIR/docker-stacks-foundation/Dockerfile | grep -v 'BASE_CONTAINER' | grep -v 'FROM $ROOT_CONTAINER' >> $DOCKERFILE
# copy files that are used during the build
cp $STACKS_DIR/docker-stacks-foundation/initial-condarc .build/
cp $STACKS_DIR/docker-stacks-foundation/fix-permissions .build/
cp $STACKS_DIR/docker-stacks-foundation/start.sh .build/
fi
echo "
############################################################################
#################### Dependency: jupyter/base-notebook #####################
############################################################################
" >> $DOCKERFILE
if [ -f "$STACKS_DIR/images/base-notebook/Dockerfile" ]; then
cat $STACKS_DIR/images/base-notebook/Dockerfile | grep -v 'BASE_CONTAINER' >> $DOCKERFILE
# copy files that are used during the build
cp $STACKS_DIR/images/base-notebook/jupyter_server_config.py .build/
cp $STACKS_DIR/images/base-notebook/start-notebook.sh .build/
cp $STACKS_DIR/images/base-notebook/start-notebook.py .build/
cp $STACKS_DIR/images/base-notebook/start-singleuser.sh .build/
cp $STACKS_DIR/images/base-notebook/start-singleuser.py .build/
cp $STACKS_DIR/images/base-notebook/docker_healthcheck.py .build/
else
cat $STACKS_DIR/base-notebook/Dockerfile | grep -v 'BASE_CONTAINER' >> $DOCKERFILE
# copy files that are used during the build
cp $STACKS_DIR/base-notebook/jupyter_server_config.py .build/
cp $STACKS_DIR/base-notebook/start-notebook.sh .build/
cp $STACKS_DIR/base-notebook/start-singleuser.sh .build/
cp $STACKS_DIR/base-notebook/docker_healthcheck.py .build/
fi
chmod 755 .build/*
echo "
############################################################################
################# Dependency: jupyter/minimal-notebook #####################
############################################################################
" >> $DOCKERFILE
if [ -f "$STACKS_DIR/images/minimal-notebook/Dockerfile" ]; then
cat $STACKS_DIR/images/minimal-notebook/Dockerfile | grep -v BASE_CONTAINER >> $DOCKERFILE
# copy files that are used during the build
cp -r $STACKS_DIR/images/minimal-notebook/setup-scripts .build/
cp $STACKS_DIR/images/minimal-notebook/Rprofile.site .build/
else
cat $STACKS_DIR/minimal-notebook/Dockerfile | grep -v BASE_CONTAINER >> $DOCKERFILE
# copy files that are used during the build
cp -r $STACKS_DIR/minimal-notebook/setup-scripts .build/
cp $STACKS_DIR/minimal-notebook/Rprofile.site .build/
fi
echo "
############################################################################
################# Dependency: jupyter/scipy-notebook #######################
############################################################################
" >> $DOCKERFILE
if [ -f "$STACKS_DIR/images/scipy-notebook/Dockerfile" ]; then
cat $STACKS_DIR/images/scipy-notebook/Dockerfile | grep -v BASE_CONTAINER >> $DOCKERFILE
else
cat $STACKS_DIR/scipy-notebook/Dockerfile | grep -v BASE_CONTAINER >> $DOCKERFILE
fi
# install Julia and R if not excluded or spare mode is used
if [[ "$no_datascience_notebook" != 1 ]]; then
echo "
############################################################################
################ Dependency: jupyter/datascience-notebook ##################
############################################################################
" >> $DOCKERFILE
if [ -f "$STACKS_DIR/images/datascience-notebook/Dockerfile" ]; then
cat $STACKS_DIR/images/datascience-notebook/Dockerfile | grep -v BASE_CONTAINER >> $DOCKERFILE
else
cat $STACKS_DIR/images/datascience-notebook/Dockerfile | grep -v BASE_CONTAINER >> $DOCKERFILE
fi
else
echo "Set 'no-datascience-notebook' = 'python-only', not installing the datascience-notebook with Julia and R."
fi
# Note that the following step also installs the cudatoolkit, which is
# essential to access the GPU.
echo "
############################################################################
########################## Dependency: gpulibs #############################
############################################################################
" >> $DOCKERFILE
cat custom/gpulibs.Dockerfile >> $DOCKERFILE
# install useful packages if not excluded or spare mode is used
if [[ "$no_useful_packages" != 1 ]]; then
echo "
############################################################################
############################ Useful packages ###############################
############################################################################
" >> $DOCKERFILE
cat custom/usefulpackages.Dockerfile >> $DOCKERFILE
else
echo "Set 'no-useful-packages', not installing stuff within custom/usefulpackages.Dockerfile."
fi
# Copy the demo notebooks and change permissions
cp -r extra/Getting_Started data
chmod -R 755 data/
# set static token (optional if set)
# copy jupyter server config token addendum to .build
cp custom/jupyter_server_config_token_addendum.py .build/
# append token config into the jupyter server config
echo >> $DOCKERFILE
echo "# Set env-var JUPYTER_TOKEN as static token" >> $DOCKERFILE
echo "ARG JUPYTER_TOKEN" >> $DOCKERFILE
echo "ENV JUPYTER_TOKEN=\$JUPYTER_TOKEN" >> $DOCKERFILE
echo "COPY jupyter_server_config_token_addendum.py /etc/jupyter/" >> $DOCKERFILE
echo "RUN cat /etc/jupyter/jupyter_server_config_token_addendum.py >> /etc/jupyter/jupyter_server_config.py" >> $DOCKERFILE
# set password
if [[ "$USE_PASSWORD" == 1 ]]; then
echo "Set password to given input"
SALT="3b4b6378355"
HASHED=$(echo -n ${PASSWORD}${SALT} | sha1sum | awk '{print $1}')
unset PASSWORD # delete variable PASSWORD
# build jupyter_notebook_config.json
echo "{
\"NotebookApp\": {
\"password\": \"sha1:$SALT:$HASHED\"
}
}" > .build/jupyter_notebook_config.json
# copy the config into .build and append the lines into the Dockerfile
echo >> $DOCKERFILE
echo "# Copy jupyter_notebook_config.json" >> $DOCKERFILE
echo "COPY jupyter_notebook_config.json /etc/jupyter/" >> $DOCKERFILE
fi
# Set environment variables
export JUPYTER_UID=$(id -u)
export JUPYTER_GID=$(id -g)
#cp $(find $(dirname $DOCKERFILE) -type f | grep -v $STACKS_DIR | grep -v .gitkeep) .
echo
echo "The GPU Dockerfile was generated successfully in file ${DOCKERFILE}."
echo "To start the GPU-based Juyterlab instance, run:"
echo " docker build -t gpu-jupyter .build/ # will take a while"
echo " docker run --gpus all -d -it -p 8848:8888 -v $(pwd)/data:/home/jovyan/work -e GRANT_SUDO=yes -e JUPYTER_ENABLE_LAB=yes -e NB_UID=$(id -u) -e NB_GID=$(id -g) --user root --restart always --name gpu-jupyter_1 gpu-jupyter"