-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinfra.tex
2690 lines (1894 loc) · 96 KB
/
infra.tex
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass[12pt, a4paper]{scrbook}
\usepackage[utf8]{inputenc}
\usepackage{comment, listings, amssymb, hyperref, natbib, graphicx, amsmath, textcomp, breqn, mathtools, csquotes, cancel, enumitem, amsthm, caption, upquote}
\usepackage[english]{babel}
\usepackage[svgnames]{xcolor}
\usepackage{tikz} % load after `xcolor`, since `tikz` internally loads `xcolor`
\usepackage[margin = 0.9in]{geometry}
\numberwithin{equation}{section}
\theoremstyle{definition}
\newtheorem{thm}{Theorem}[section] % reset theorem numbering for each chapter
\theoremstyle{definition}
\newtheorem{defn}[thm]{Definition} % definition numbers are dependent on theorem numbers
\newtheorem{exmp}[thm]{Example} % same for example numbers
\newtheorem{lemma}[thm]{Lemma} % same for example numbers
\newtheorem{remark}[thm]{Remark} % same for example numbers
\newcommand{\norm}[2]{\left\vert\left\vert #1 \right\vert\right\vert_{#2}}
\newcommand{\inner}[1]{\left\langle #1 \right\rangle}
\newcommand{\epi}[1]{\text{epi($ #1 $) } }
\lstdefinestyle{mystylepython}{
language=Python,
basicstyle=\ttfamily,
commentstyle=\color{green!40!black},
keywordstyle=\color{blue},
numberstyle=\tiny\color{gray},
numbers=left,
frame=single,
breaklines=true,
showstringspaces=false,
captionpos=b,
tabsize=1, % Adjust as needed
columns=fullflexible,
}
\lstdefinestyle{mystylejava}{
language=Java,
basicstyle=\ttfamily,
commentstyle=\color{green!40!black},
keywordstyle=\color{blue},
numberstyle=\tiny\color{gray},
numbers=left,
frame=single,
breaklines=true,
showstringspaces=false,
captionpos=b,
tabsize=1, % Adjust as needed
columns=fullflexible,
}
\lstdefinestyle{mystylebash}{
language=bash,
basicstyle=\ttfamily,
commentstyle=\color{green!40!black},
keywordstyle=\color{blue},
numberstyle=\tiny\color{gray},
numbers=left,
frame=single,
breaklines=true,
showstringspaces=false,
captionpos=b,
tabsize=1, % Adjust as needed
columns=fullflexible,
}
\lstset{
literate={~}{$\sim$}{1} % otherwise tilde's look really ugly in `lstlisting` envs
}
\title{Infra}
% \subtitle{Questions}
\author{Imahn Shekhzadeh \\ \texttt{\href{mailto:email@example.com}{imahn.shekhzadeh@posteo.de}}\normalsize}
\date{\today}
\begin{document}
\maketitle
\tableofcontents
\section*{\large Note}
The commands in this document might only run through if you use the \texttt{.bashrc} file provided in App.~\ref{app:bashrc_file}
\chapter{Bash \& Linux}
\section{Pattern Matching}
In Bash, \texttt{[[ ]]} over \texttt{[ ]} is preferred, since \texttt{[[ ]]} is safer and more capable within Bash scripts.
Within \texttt{[ ]}, where word splitting and filename expansion do occur, it is good practice to double-quote variables.
But it is safe to omit the double-quotes for e.g.~\$\# within \texttt{[[ ]]}.
\section{Multi-Line Comments}
Use \texttt{:\ \textquotesingle\ [...]\ \textquotesingle} for a multi-line comment.
\begin{lstlisting}[style=mystylebash, label=alg:bash_multiline_comments, xleftmargin=\parindent]
: '
This is a
very long
comment
'
\end{lstlisting}
\section{File Download}
Downloading file from URL and allowing for redirects,
\begin{lstlisting}[style=mystylebash, label=alg:curl, xleftmargin=\parindent]
curl -Lo output.out https://url.com
\end{lstlisting}
When taking a \texttt{GitHub} link, note that you need to take the URL of the raw file.
\section{\texttt{for}-loops}
For this directory structure,
\begin{lstlisting}[style=mystylebash, label=alg:exmp__dir_struc, xleftmargin=\parindent]
infra_upd.tex
infra_upd.pdf
\end{lstlisting}
rename via
\begin{lstlisting}[style=mystylebash, label=alg:linux_mv_files_for, xleftmargin=\parindent]
for file in infra_upd.*; do mv "$file" "${file/infra_upd/infra}"; done
\end{lstlisting}
What happens is a \href{https://stackoverflow.com/questions/13210880/replace-one-substring-for-another-string-in-shell-script}{\color{blue}substring replacement}.
\section{Argument Retrieval}
Retrieving all but the first argument,
\begin{lstlisting}[style=mystylebash, label=alg:bash__args_shift, xleftmargin=\parindent]
bash_func(){
shift
echo "all provided args (except the first): $@"
}
\end{lstlisting}
Doing this $N\geq 1$-times,
\begin{lstlisting}[style=mystylebash, label=alg:bash__args_shift_gen, xleftmargin=\parindent]
test_sth(){
shift
...
shift
echo "all provided args (except the first N): $@"
}
\end{lstlisting}
\section{Colored Outputs}
Using colored outputs in Bash, cf.~\texttt{str\_diff} in App.~\ref{app:bashrc_file}.
Personally, I use the following color scheme for the CLI,
\begin{enumerate}
\item monokai color scheme, i.e.~dark gray background (\#272822) with light peach color for the text (\#F8F8F2).
\item File paths are still displayed in blue, which is suboptimal, to change the color to the better readable cyan-blue color, click on the three horizontal
lines in the CLI, then on \textbf{Preferences}, then choose the currently active color, switch to the \textbf{Colors} tab, then go to \textbf{Palette},
click on the blue color \& instead use the color \#66D9EF
\end{enumerate}
where \texttt{-e} stands for human readability and \texttt{-s} for summarizing.
\section{String/File/Directory Operations}
Appending line to file (\texttt{-a}:~\texttt{appending}, otherwise \texttt{tee} overwrites \texttt{output.out} if existent),
\begin{lstlisting}[style=mystylebash, label=alg:ubuntu__line_appending, xleftmargin=\parindent]
echo "this is a line" | tee -a output.out # -a: appending, important
\end{lstlisting}
Checking whether string is empty,
\begin{lstlisting}[style=mystylebash, label=alg:bash__empty_string_check, xleftmargin=\parindent]
[[ -z "$env_name" ]] && echo "The string is empty."
\end{lstlisting}
Finding out size of file/directory,
\begin{lstlisting}[style=mystylebash, label=alg:dir_size, xleftmargin=\parindent]
du -hs <path_to_file_or_dir> # du -hs file.ext
# for shorter summary (single quotation strings required)
du -hs <path_to_file_or_dir> | awk '{print $1}'
\end{lstlisting}
Unzipping a file via the CLI,
\begin{lstlisting}[style=mystylebash, label=alg:bash_unzip, xleftmargin=\parindent]
unzip /path/to/file.zip -d /path/to/destination
\end{lstlisting}
Zipping a directory via the CLI,
\begin{lstlisting}[style=mystylebash, label=alg:bash_zip, xleftmargin=\parindent]
zip -r archive_name.zip /path/to/directory
\end{lstlisting}
and zipping several files,
\begin{lstlisting}[style=mystylebash, label=alg:bash_zip__files, xleftmargin=\parindent]
zip archive_name.zip /path/to/file1 /path/to/file2 /path/to/file3
\end{lstlisting}
Opening a file and automatically scrolling to the bottom,
\begin{lstlisting}[style=mystylebash, label=alg:less__bottom_scroll, xleftmargin=\parindent]
less +G /path/to/file.ext
\end{lstlisting}
Searching for files with specific extension, e.g. \texttt{.ext}:
\begin{lstlisting}[style=mystylebash, label=alg:bash_find, xleftmargin=\parindent]
find . -name "*.ext"
# find . -name "*.png"
\end{lstlisting}
Creating new directory including all parent directories (\texttt{-p} option is safe, since if directory is already existent,
no error will be outputted),
\begin{lstlisting}[style=mystylebash, label=alg:bash__dir_creation, xleftmargin=\parindent]
mkdir -p <dir>
\end{lstlisting}
Comparing the contents of two directories,
\begin{lstlisting}[style=mystylebash, label=alg:bash_diff__dir, xleftmargin=\parindent]
diff -r --color directory1 directory2 # `-r` for recursive comparison
diff -rq --color directory1 directory2 # `-q` suppresses the output of differences and only shows which files differ
\end{lstlisting}
Ignoring files only existent in one of the directories (which treats absent files as empty),
\begin{lstlisting}[style=mystylebash, label=alg:, xleftmargin=\parindent]
diff -rq --color --unidirectional-new-file directory1 directory2
\end{lstlisting}
\section{Monitoring}
\begin{lstlisting}[style=mystylebash, label=alg:htop, xleftmargin=\parindent]
htop
\end{lstlisting}
RAM usage,
\begin{lstlisting}[style=mystylebash, label=alg:free, xleftmargin=\parindent]
free -h
\end{lstlisting}
\section{Systems Information}
Retrieving the number of available CPU resources,
\begin{lstlisting}[style=mystylebash, label=alg:nproc, xleftmargin=\parindent]
echo "$(nproc)"
\end{lstlisting}
Print day and time from CLI,
\begin{lstlisting}[style=mystylebash, label=alg:cli_date, xleftmargin=\parindent]
echo "$(date +%d_%m_%y-%H_%M_%S)"
# echo "$(date +%dp%mp%y-%Hp%Mp%S)"
\end{lstlisting}
Listing all available kernels in Debian-based Linux systems,
\begin{lstlisting}[style=mystylebash, label=alg:ubuntu_kernel, xleftmargin=\parindent]
dpkg --list | grep linux-image
\end{lstlisting}
Currently active kernel version,
\begin{lstlisting}[style=mystylebash, label=alg:ubuntu_kernel__current, xleftmargin=\parindent]
uname -a
\end{lstlisting}
\section{Port Number}
To check whether port is used or not,
\begin{lstlisting}[style=mystylebash, label=alg:port_number, xleftmargin=\parindent]
ss -tuln | grep :<port_number>
# e.g.: `ss -tuln | grep :11434`
\end{lstlisting}
\section{CUDA}
\begin{itemize}
\item When you need to find out the CUDA version installed, install \texttt{nvidia-cuda-toolkit}, but do NOT reboot. After its use, immediately remove this package and any package installed alongside with it!
\item In case NVIDIA drivers do not allow for boot into Ubuntu, e.g.~because you did not uninstall the \texttt{nvidia-cuda-toolkit} package,
\begin{enumerate}
\item Boot into an older kernel version of Linux (in order to get there, do a "hard" reboot, and then go into
\enquote{Advanced options for Ubuntu}, and choose an older kernel version).
\item Once booted into the older kernel version, I removed `nvidia-cuda-toolkit` and rebooted.
\item After a few more hard reboots and booting into the older kenel version, at some point, the newer kernel
version was picked up and worker again.
\item Now to fix the monitors (because dual-monitor setup didn't work), I had to open the program
\enquote{Additional Drivers} and change the driver from the open-source version to an NVIDIA proprietary one.
\item Then I had to install CUDA according to \href{https://docs.nvidia.com/cuda/cuda-installation-guide-linux/index.html}{these instructions}.
\item For PyTorch to recognize the GPU, I had to reboot.
\end{enumerate}
\end{itemize}
\section{tmux}
\begin{itemize}
\item Creating a new session:
\begin{lstlisting}[style=mystylebash, label=alg:tmux_sess_creation, xleftmargin=\parindent]
tmux new -s <name>
# tmux new -s mysession
\end{lstlisting}
\item To detach from the current \textsc{tmux} session and leave it running in the background, press \textsc{Ctrl + b} and then type \textsc{d}.
\item Reconnecting to a \textsc{tmux} session,
\begin{lstlisting}[style=mystylebash, label=alg:tmux_sess_reconnect, xleftmargin=\parindent]
tmux attach -t <name>
# tmux attach -t mysession
\end{lstlisting}
\item Killing a \textsc{tmux} session from outside,
\begin{lstlisting}[style=mystylebash, label=alg:tmux_sess_kill, xleftmargin=\parindent]
tmux kill-session -t <name>
# tmux kill-session -t mysession
\end{lstlisting}
\end{itemize}
% - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - %
\chapter{Docker}
\section{Installation}
\begin{itemize}
\item Follow \href{https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04}{this great tutorial by DigitalOcean}.
\item To start the Docker daemon,
\begin{lstlisting}[style=mystylebash, label=alg:docker_daemon_start, xleftmargin=\parindent]
sudo systemctl start docker
\end{lstlisting}
\item To use NVIDIA GPUs (both in PyTorch \& Jax), install the \href{https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html#installing-with-apt}{NVIDIA Container Toolkit}
\item Once done with the installation of the NVIDIA Container Toolkit, proceed with the \href{https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html#configuring-docker}{configuration}. During the configuration, it will be necessary to restart the docker daemon, which you can achieve as follows:
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
sudo systemctl restart docker
\end{lstlisting}
\item Checking that the docker installation was successful,
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
docker run hello-world
\end{lstlisting}
\end{itemize}
\section{Basics}
\begin{itemize}
\item Interactive start of containers:
\begin{lstlisting}[style=mystylebash, label=alg:docker_id, xleftmargin=\parindent]
d ps -a # find out ID (also docker container name)
d start -i ID
\end{lstlisting}
\item Copying files from local system to docker container and vice versa; \textbf{run both commands from local CLI}
\begin{lstlisting}[style=mystylebash, label=alg:docker_cp, xleftmargin=\parindent]
d cp file_name container_ID:/target_dir # local -> docker
d cp container_ID:/file_name dir_name # docker -> local
\end{lstlisting}
\end{itemize}
\section{Dockerfile}
\begin{itemize}
\item When you find the command for pulling a docker image on \url{https://hub.docker.com}, e.g.~
\begin{lstlisting}[style=mystylebash, label=alg:dockerhub, xleftmargin=\parindent]
d pull ubuntu:jammy-20231004
\end{lstlisting}
then in the Dockerfile, just write
\begin{lstlisting}[style=mystylebash, label=alg:dockerfile_from, xleftmargin=\parindent]
FROM ubuntu:jammy-20231004
\end{lstlisting}
When no tag is specified, by default the \textit{latest} one will be taken. However, using the \textit{latest} tag can potentially cause issues with reproducibility and consistency, because you might pull a different version of the image at different times without knowing it if the latest tag gets updated. \textbf{For more predictable builds, it is advised to use a specific version tag.}
\item Note that the structure of the \textit{docker pull} command is
\begin{lstlisting}[style=mystylebash, label=alg:docker_pull, xleftmargin=\parindent]
d pull [OPTIONS] NAME[:TAG|@DIGEST]
\end{lstlisting}
In general, the \textit{NAME} is in the format \textit{repository/image}. If \textit{repository} is not specified, Docker assumes the image is located in the default DockerHub library repository. However, many images (like PyTorch) are hosted under a specific user or organization's namespace on DockerHub, rather than the top-level library. That's why the command for the docker pull (for the latest tag) reads
\begin{lstlisting}[style=mystylebash, label=alg:docker_pull_exmp, xleftmargin=\parindent]
d pull pytorch/pytorch
\end{lstlisting}
\item If using a Docker image like \textit{pytorch/pytorch:latest}, conda is already installed. In this case, the default environment is named \textit{base}, which is a common practice in Docker images with conda -- unless otherwise stated.
\item Copying local scripts into docker container,
\begin{lstlisting}[style=mystylebash, label=alg:docker_copy, xleftmargin=\parindent]
COPY relative/path/to/script.py .
\end{lstlisting}
From the \href{https://docs.docker.com/engine/reference/builder/#copy}{documentation}:
\begin{quote}
Multiple $\langle$src$\rangle$ resources may be specified but the paths of files and directories will be interpreted as relative to the source of the context of the build.
\end{quote}
It is also important to put the \textit{.} at the end, since it represents the destination in the Docker image where the file should be copied. The dot . refers to the current working directory inside the Docker image, which is determined by the WORKDIR command in the Dockerfile. If WORKDIR is not set, it defaults to the root directory (/) of the image.
Also, each time the script \texttt{relative/path/to/script.py} changes, the Dockerfile needs to be rebuilt --- \textbf{however, a cached version will be used, which speeds things up}.
\item Copying local dirs into docker container,
\begin{lstlisting}[style=mystylebash, label=alg:docker__copy_dir, xleftmargin=\parindent]
COPY relative/path/to/dir/ .
\end{lstlisting}
\item Running a Dockerfile,
\begin{lstlisting}[style=mystylebash, label=alg:docker_build, xleftmargin=\parindent]
d build -f file_name -t img_name .
d build -f file_name -t img_name:tag_name . # tag name optional, but recommended, e.g. 1.0 (no quotes required)
\end{lstlisting}
where \texttt{img\_name} will be the name of the newly created image, \texttt{tag\_name} the tag name and \texttt{file\_name} the name of the docker file.
The \texttt{.} specifies the context of the build, which is the current directory in this case. If \texttt{custom_docker_file} is in another path, it can be easily provided,
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
d build -f file_name -t img_name:tag_name /path/to/build/context
\end{lstlisting}
\item In order to forcefully rebuild the Dockerfile from scratch, i.e. without the use of a cached version, use the \texttt{--no-cache} option,
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
d build -f file_name --no-cache -t img_name:tag_name .
\end{lstlisting}
\item Via
\begin{lstlisting}[style=mystylebash, label=alg:docker__expose_port, xleftmargin=\parindent]
EXPOSE custom-port-number
# EXPOSE 80
\end{lstlisting}
it is possible to expose a port. Note that port exposure is related to network access. Note that even though network access might not be needed, there is still no harm in exposing a port (since an exposure of the port does not make the docker container more vulnerable).
\end{itemize}
\section{Docker images}
\begin{itemize}
\item Check all available Docker images via
\begin{lstlisting}[style=mystylebash, label=alg:docker__check_avail_imgs, xleftmargin=\parindent]
d images
\end{lstlisting}
\item Cleaning up dangling docker images --- these are the entries with $\langle$\texttt{none}$\rangle$ in the repository or tag name in the output of the previous algo:
\begin{lstlisting}[style=mystylebash, label=alg:docker_rm__dangling, xleftmargin=\parindent]
d image prune -f
\end{lstlisting}
If the total acclaimed disk space is $0$B, give this command a try, which can help clean up \textit{a lot of} disk space:
\begin{lstlisting}[style=mystylebash, label=alg:docker_system_prune, xleftmargin=\parindent]
d system prune
\end{lstlisting}
\item Removing a Docker image --- \textbf{only do this when finished with using the image}
\begin{lstlisting}[style=mystylebash, label=alg:docker_remove, xleftmargin=\parindent]
d image rm img_name:tag_name
# d container rm <container_id> # in case some containers are using the image
\end{lstlisting}
\end{itemize}
\section{Docker containers}
\subsection{Basics}
\begin{itemize}
\item In case of shared memory issues in the docker container, the shared memory --- which is by default $64$ MB --- needs to be increased when the docker container is run,
\begin{lstlisting}[style=mystylebash, label=alg:docker_run, xleftmargin=\parindent]
d run --shm-size 512m [...] # requesting memory in MB
# d run --shm-size 1G [...] # requesting memory in GB
\end{lstlisting}
\item Running Docker images -- without being able to utilize NVIDIA GPUs:
\begin{lstlisting}[style=mystylebash, label=alg:docker_run, xleftmargin=\parindent]
d run -it img_name # if `tag_name` was not provided
d run -it img_name:tag_name # if `tag_name` was provided during build (recommended)
\end{lstlisting}
\item Running Docker images \& utilizing GPUs:
\begin{lstlisting}[style=mystylebash, label=alg:docker_run_gpus, xleftmargin=\parindent]
d run --gpus all -it img_name
d run --gpus all -it img_name:tag_name # recommended
\end{lstlisting}
\item To mount a local file to the container at runtime, do
\begin{lstlisting}[style=mystylebash, label=alg:docker__run_volume_mounting, xleftmargin=\parindent]
d run -v /absolute/path/to/script.py:/path/to/workdir/script.py --gpus all -it img_name
d run -v /absolute/path/to/script.py:/path/to/workdir/script.py --gpus all -it img_name:tag_name # recommended, provide `img_name` & `tag_name`
\end{lstlisting}
The mounting expects \textbf{absolute} file paths on the side of the host machine.
\item Note that you can include the bash command \textbf{pwd} to avoid having to manually pass absolute paths for the mounting
\begin{lstlisting}[style=mystylebash, label=alg:docker_run__with_pwd, xleftmargin=\parindent]
d run -v $(pwd)/script.py:/path/to/workdir/script.py --gpus all -it img_name:tag_name # recommended, provide `img_name` & `tag_name`
\end{lstlisting}
If you need the container to reflect changes made to the scripts on the host without rebuilding the image every time, you would use the \textit{-v} flag to mount the directory. If the scripts won't change, or you don't need to reflect changes in real-time, you don't need to mount the directory, as the necessary scripts have already been copied into the image during the build process.
\item It is also possible to directly mount directories:
\begin{lstlisting}[style=mystylebash, label=alg:docker_run__mount_dir, xleftmargin=\parindent]
d run -v $(pwd)/dir_path:/path/to/workdir --gpus all -it img_name:tag_name
\end{lstlisting}
Note that the specified directory from the host is mounted into the container at the specified mount point. If there are any existing files or directories in the container at the mount point, they become obscured by the mount.
\item In several cases it can be useful to remove the docker container right after execution:~When you$\dots$
\begin{itemize}
\item[$\circ$] $\dots$are running many short-lived containers, like during development or testing,
\item[$\circ$] $\dots$want to avoid manual cleanup of stopped containers later on,
\item[$\circ$] $\dots$are running containers for one-off tasks that do not need to persist any state after they are finished.
\end{itemize}
In this case,
\begin{lstlisting}[style=mystylebash, label=alg:docker_run__mount_dir_rm, xleftmargin=\parindent]
d run --rm -v $(pwd)/dir_path:/path/to/workdir --gpus all -it img_name:tag_name
\end{lstlisting}
\item It is also possible to mount two separate host directories to two separate directories within the container,
\begin{lstlisting}[style=mystylebash, label=alg:docker_run__mount_sev_dir_rm, xleftmargin=\parindent]
d run --rm -v $(pwd)/dir_path1:/path/to/workdir1 -v $(pwd)/dir_path2:/path/to/workdir2 --gpus all -it img_name:tag_name
\end{lstlisting}
This will not cause any overwriting as each \textit{-v} flag creates a unique mount point inside the container.
\item Finding out the python version of the Docker image
\begin{lstlisting}[style=mystylebash, label=alg:docker__python_version, xleftmargin=\parindent]
d run -it --rm img_name:tag_name python3 --version
\end{lstlisting}
This command will immediately remove the container after execution.
\item It is also possible to interact with a docker container,
\begin{lstlisting}[style=mystylebash, label=alg:docker_interactive_bash, xleftmargin=\parindent]
docker run -it --rm img_name:tag_name /bin/bash
\end{lstlisting}
\end{itemize}
\subsection{Passing Arguments}
It is possible to pass arguments when running a docker container.
\begin{enumerate}
\item Assuming you have a bash script \textit{run\_scripts.sh}, in which a Python script, e.g.
\begin{lstlisting}[style=mystylebash, label=alg:docker_run__pass_arguments, xleftmargin=\parindent]
#!/bin/sh
isort /app/scripts/*.py
black /app/scripts/*.py
python3 -B /app/scripts/test_script.py
python3 -B /app/scripts/test_anil.py
\end{lstlisting}
Modify this bash script s.t.~any arguments passed to the CLI when running the docker container are picked up,
\begin{lstlisting}[style=mystylebash, label=alg:docker_run__pass_arguments_mod, xleftmargin=\parindent]
python3 -B /app/scripts/test_anil.py "$@"
# python3 -B /app/scripts/test_script.py "$@" # alternative
\end{lstlisting}
\item Rebuild (!) the docker image.
\item Now run the docker container as follows:
\begin{lstlisting}[style=mystylebash, label=alg:docker_run__pass_arguments_container, xleftmargin=\parindent]
d run --rm -v $(pwd)/dir_path:/path/to/workdir --gpus all -it img_name:tag_name arg1 arg2
# d run --rm -v $(pwd)/dir_path:/path/to/workdir --gpus all -it img_name:tag_name --n_ways 1 --k_shots 1 # example
\end{lstlisting}
\end{enumerate}
\subsection{Listing \& Stopping}
\begin{itemize}
\item Listing all running containers,
\begin{lstlisting}[style=mystylebash, label=alg:docker__check_all_contain, xleftmargin=\parindent]
d ps
\end{lstlisting}
Listing only the container ID of all running containers,
\begin{lstlisting}[style=mystylebash, label=alg:docker__contains_ids, xleftmargin=\parindent]
d ps -q
\end{lstlisting}
\item Stopping a running container,
\begin{lstlisting}[style=mystylebash, label=alg:docker__stop_contain, xleftmargin=\parindent]
d stop container-ID
\end{lstlisting}
\item Stopping a running container and removing it,
\begin{lstlisting}[style=mystylebash, label=alg:docker__stop_rem_contain, xleftmargin=\parindent]
d stop container-ID && d rm container-ID
\end{lstlisting}
\end{itemize}
\section{Storage}
\begin{itemize}
\item In case you want \texttt{docker} to install images and containers in a separate drive, e.g.~one under \texttt{/media/user-name/samsung_500}, you can follow these steps:
\begin{enumerate}
\item Stop the docker service,
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
sudo systemctl stop docker
\end{lstlisting}
\item Move the directory \texttt{/var/lib/docker} to the separate drive,
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
sudo mv /var/lib/docker /media/user-name/samsung_500/docker
\end{lstlisting}
Note that you should \textit{not} create the \texttt{docker} directory directly under \texttt{/media/user-name/samsung_500}.
\item Configure \texttt{docker} to use the new directory by editing the \texttt{docker} daemon configuration file,
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
sudo nano /etc/docker/daemon.json
\end{lstlisting}
and then adding the following configuration,
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
{
"data-root": "/media/user-name/docker"
}
\end{lstlisting}
\item Restart the \texttt{docker} service,
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
sudo systemctl restart docker
\end{lstlisting}
\item Follow the instructions from the NVIDIA Container Toolkit \href{https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html#configuring-docker}{configuration} to ensure that NVIDIA GPUs can still be used inside the containers.
\end{enumerate}
\end{itemize}
\chapter{AWS S3}
\section{Installation \& Configuration}
\begin{enumerate}
\item \href{https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html#getting-started-install-instructions}{Installation instructions}
\item The CLI will display the path under which the \textit{aws} package was installed, but it might be sufficient to simply run
\begin{lstlisting}[style=mystylebash, label=alg:aws, xleftmargin=\parindent]
aws
\end{lstlisting}
Double check by running
\begin{lstlisting}[style=mystylebash, label=alg:aws_path, xleftmargin=\parindent]
which aws
\end{lstlisting}
\item After installation, configuration is necessary. For this run
\begin{lstlisting}[style=mystylebash, label=alg:aws_configure, xleftmargin=\parindent]
aws configure
\end{lstlisting}
You can leave these fields empty:
\begin{lstlisting}[style=mystylebash, label=alg:aws_configure__default_reg, xleftmargin=\parindent]
Default region name [None]:
Default output format [None]:
\end{lstlisting}
A configuration file will be saved under
\begin{lstlisting}[style=mystylebash, label=alg:aws__config_file, xleftmargin=\parindent]
~/.aws/credentials
\end{lstlisting}
\item In the case you are a member of UNIGE, you can obtain the AWS access key ID and the secret access key as follows:
\begin{lstlisting}[style=mystylebash, label=alg:aws__access_secret_key, xleftmargin=\parindent]
echo -n "$user_name" | base64 # the `-n` is important in this context
echo -n "$passwd" | md5sum
\end{lstlisting}
where \$user\_name and \$passwd need to be provided
Otherwise, you need login to the AWS Management Console.
\item \textbf{To test the configuration was successful, do this:}
\begin{lstlisting}[style=mystylebash, label=alg:aws_configure_test, xleftmargin=\parindent]
aws s3 ls --endpoint-url https://your-custom-s3-endpoint.com
\end{lstlisting}
where you replace the endpoint-url \url{https://your-custom-s3-endpoint.com} with yours.
\end{enumerate}
\section{AWS Credentials (Profiles)}
\begin{itemize}
\item It is possible to use several profiles in the file \texttt{$\sim$/.aws/credentials}.
\item For example,
\begin{lstlisting}[style=mystylebash, label=alg:aws_profiles, xleftmargin=\parindent]
[default]
aws_access_key_id = YOUR_DEFAULT_ACCESS_KEY
aws_secret_access_key = YOUR_DEFAULT_SECRET_KEY
[profile1]
aws_access_key_id = ANOTHER_ACCESS_KEY_ID
aws_secret_access_key = ANOTHER_SECRET_ACCESS_KEY
[profile2]
aws_access_key_id = YET_ANOTHER_ACCESS_KEY_ID
aws_secret_access_key = YET_ANOTHER_SECRET_ACCESS_KEY
\end{lstlisting}
Using specific profile when running \texttt{aws cli} commands via \texttt{--profile} option in the command:
\begin{lstlisting}[style=mystylebash, label=alg:aws_cli__profile, xleftmargin=\parindent]
aws s3 --profile profile1 [...]
# aws s3 --profile default [...]
\end{lstlisting}
\end{itemize}
\section{Buckets}
One can have several buckets.
\subsection{Creation}
\begin{itemize}
\item Creating a new bucket,
\begin{lstlisting}[style=mystylebash, label=alg:aws__new_bucket, xleftmargin=\parindent]
aws s3api create-bucket --bucket custom-bucket-name --endpoint-url https://custom-s3-endpoint.com --profile default
\end{lstlisting}
\end{itemize}
\subsection{Listings}
\begin{itemize}
\item Directly \enquote{folder} contents of an s3 bucket,
\begin{lstlisting}[style=mystylebash, label=alg:aws__bucket_ls, xleftmargin=\parindent]
aws s3 ls s3://custom-bucket-name --recursive --endpoint-url https://custom-s3-endpoint.com --profile default # `--recursive` optional
\end{lstlisting}
\item Showing file contents,
\begin{lstlisting}[style=mystylebash, label=alg:aws__bucket_prefix_ls, xleftmargin=\parindent]
aws s3 ls s3://custom-bucket-name/prefix/ --recursive --endpoint-url https://custom-s3-endpoint.com --profile default # `--recursive` optional
\end{lstlisting}
Note that the \texttt{/} at the end of the prefix (\enquote{folder}) is necessary.
\end{itemize}
\subsection{File Copying}
\begin{itemize}
\item Local machine $\longrightarrow$ S3:
\begin{lstlisting}[style=mystylebash, label=alg:aws__file_copying_local_s3, xleftmargin=\parindent]
aws s3 cp path/to/custom_file.ext s3://custom-bucket-name/path/to/custom_file.ext --endpoint-url https://custom-s3-endpoint.com --profile default
\end{lstlisting}
\item S3 $\longrightarrow$ local machine:
\begin{lstlisting}[style=mystylebash, label=alg:aws__file_copying_s3_local, xleftmargin=\parindent]
aws s3 cp s3://custom-bucket-name/path/to/s3_file.ext custom/destination --endpoint-url https://custom-s3-endpoint.com --profile default
\end{lstlisting}
\end{itemize}
\subsection{Directory Copying}
\begin{itemize}
\item Local machine $\longrightarrow$ S3:
\begin{lstlisting}[style=mystylebash, label=alg:aws__dir_copying, xleftmargin=\parindent]
aws s3 sync path/to/dir s3://custom-bucket-name/path/to --endpoint-url
https://custom-s3-endpoint.com --profile default
\end{lstlisting}
\end{itemize}
\subsection{Directory/File Deletion}
\begin{itemize}
\item Deleting a folder (which is essentially a prefix in S3) and its contents in
an S3 bucket,
\begin{lstlisting}[style=mystylebash, label=alg:aws__dir_rm, xleftmargin=\parindent]
aws s3 rm s3://your-bucket-name/path-to-your-folder --recursive --endpoint-url https://custom-s3-endpoint.com --profile default
\end{lstlisting}
\item Deleting a file,
\begin{lstlisting}[style=mystylebash, label=alg:aws__file_rm, xleftmargin=\parindent]
aws s3 rm s3://your-bucket-name/path-to-your-file.out --recursive --endpoint-url https://custom-s3-endpoint.com --profile default
\end{lstlisting}
\end{itemize}
\section{Cloudpathlib}
\begin{itemize}
\item When you use the \href{https://cloudpathlib.drivendata.org/stable/}{cloudpathlib} module, and you want to specify a profile, do this:
\begin{lstlisting}[style=mystylepython, label=alg:aws__cloudpathlib_profile, xleftmargin=\parindent]
from cloudpathlib import S3Path, S3Client
# Create an S3 client with a specific AWS profile
s3_client = S3Client(
aws_access_key_id=aws_access_key_id,
aws_secret_access_key=aws_secret_access_key,
endpoint_url=endpoint_url,
profile_name="profile1", # specify profile here
)
# Make `client` default:
client.set_as_default_client()
\end{lstlisting}
\end{itemize}
\chapter{Conda}
\begin{itemize}
\item Retrieving information about currently activated conda environment,
\begin{lstlisting}[style=mystylebash, label=alg:conda_info, xleftmargin=\parindent]
conda info
\end{lstlisting}
\item Listing all installed environments,
\begin{lstlisting}[style=mystylebash, label=alg:conda_env_list, xleftmargin=\parindent]
conda env list
\end{lstlisting}
\end{itemize}
\section{Installation of Environments}
\begin{itemize}
\item Installing conda with specific python version,
\begin{lstlisting}[style=mystylebash, label=alg:conda_env__creation_activation, xleftmargin=\parindent]
# only `myenv` needs to be specified (quotation marks necessary)
env_name="myenv" && conda create -n "$env_name" python=3.11.3 -y && conda activate "$env_name"
\end{lstlisting}
As of Oct 16, I wouldn't recommend installing python 3.12.0 yet --- I got a lot of unmet dependency problems when trying to install torch 2.1 with NVIDIA Cuda version 11.8 afterwards.
\item Installation of conda environment from bash file:
\begin{lstlisting}[style=mystylepython, label=alg:conda_env__from_bash, xleftmargin=\parindent]
conda deactivate # go into base environment
source conda/filename.sh
touch .env
\end{lstlisting}
\item Completely remove conda environment,
\begin{lstlisting}[style=mystylebash, label=alg:conda_removal, xleftmargin=\parindent]
conda deactivate && conda remove -n custom-env-name --all -y
\end{lstlisting}
\end{itemize}
\section{Export}
\begin{itemize}
\item Exporting an \texttt{.yml}-file to share with others for reproducibility,
\begin{lstlisting}[style=mystylebash, label=alg:conda_export, xleftmargin=\parindent]
conda env export > environment.yml
\end{lstlisting}
\item Line \enquote{Prefix:} at end of \texttt{.yml} file can be safely deleted, for details cf.~\href{https://stackoverflow.com/questions/39280638/how-to-share-conda-environments-across-platforms}{here}
\end{itemize}
\section{Installation \& Removal of Packages}
\begin{itemize}
\item Installation of packages from \texttt{pyproject.toml} file,
\begin{lstlisting}[style=mystylebash, label=alg:pyproject_install, xleftmargin=\parindent]
pip install -e .
\end{lstlisting}
If there is not enough free space, do
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
TMPDIR=[...] pip install -e .
\end{lstlisting}
where \texttt{TMPDIR} needs to exist.
If you want to install a specific version of a package via \texttt{pip} --- and you do not use a \texttt{pyproject.toml} file --- do
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
pip install package==version
\end{lstlisting}
\item Installing specific conda package in a specific version --- note that the specification of a version number is optional,
\begin{lstlisting}[style=mystylebash, label=alg:conda__spec_env_version, xleftmargin=\parindent]
conda install -c conda-forge custom-pkg-name=version-number -y
# conda install -c conda-forge cloudpathlib=0.15.1 -y
# conda install -c conda-forge cloudpathlib -y
\end{lstlisting}
\item Removing list of packages from conda environment,
\begin{lstlisting}[style=mystylebash, xleftmargin=\parindent]
conda remove -n custom-env-name pkg1 pkg2 ... pkgN -y
# conda remove -n google_jax matplotlib -y
\end{lstlisting}
\end{itemize}
\section{Usage in VSCode}
\begin{itemize}
\item Selecting a conda environment in VSCode, do \texttt{Ctrl + Shift + P} and type \newline \texttt{Python:~select interpreter}.
\end{itemize}
\chapter{Git}