33
44def make_template (
55 kernels : list [str ] = ["python" , "r" , "javascript" , "deno" , "bash" , "java" ],
6- set_user_workdir : bool = False ,
6+ is_docker : bool = False ,
77):
88 enabled_kernels = set (["python" , "javascript" ] + kernels )
99 # Start with base template
1010 template = (
1111 Template ()
1212 .from_image ("python:3.12" )
1313 .set_user ("root" )
14- .set_workdir ("/" )
14+ .set_workdir ("/root " )
1515 .set_envs (
1616 {
1717 "PIP_DEFAULT_TIMEOUT" : "100" ,
@@ -117,12 +117,10 @@ def make_template(
117117 )
118118 )
119119
120- if set_user_workdir :
121- template = template .set_user ("user" ).set_workdir ("/home/user" )
122-
123120 # Copy configuration files
124121 template = (
125- template .copy ("matplotlibrc" , ".config/matplotlib/.matplotlibrc" )
122+ template
123+ .copy ("matplotlibrc" , ".config/matplotlib/.matplotlibrc" )
126124 .copy ("start-up.sh" , ".jupyter/start-up.sh" )
127125 .run_cmd ("chmod +x .jupyter/start-up.sh" )
128126 .copy ("jupyter_server_config.py" , ".jupyter/" )
@@ -131,6 +129,18 @@ def make_template(
131129 .copy ("startup_scripts" , ".ipython/profile_default/startup" )
132130 )
133131
132+ if is_docker :
133+ # create user user and /home/user
134+ template = template .run_cmd ("useradd -m user" )
135+ template = template .run_cmd ("mkdir -p /home/user" )
136+ template = template .run_cmd ("chown -R user:user /home/user" )
137+ # add to sudoers
138+ template = template .run_cmd ("echo 'user ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers" )
139+ # own everything in /root to user
140+ template = template .run_cmd ("chown -R user:user /root" )
141+
142+ template = template .set_user ("user" ).set_workdir ("/home/user" )
143+
134144 return template .set_start_cmd (
135- ".jupyter/start-up.sh" , wait_for_url ("http://localhost:49999/health" )
145+ "sudo /root/ .jupyter/start-up.sh" , wait_for_url ("http://localhost:49999/health" )
136146 )
0 commit comments