@@ -117,31 +117,25 @@ def _prepare_dependency_entries(
117
117
)
118
118
119
119
if requirements_path is not None :
120
- ret += _generate_copy_command (
121
- requirements_path ,
122
- "./requirements.txt" ,
123
- comment = "requirements.txt file specified, thus copy it to the docker container." ,
124
- ) + textwrap .dedent (
120
+ ret += textwrap .dedent (
125
121
"""
126
- RUN {} install --no-cache-dir {} -r ./requirements.txt
122
+ RUN {} install --no-cache-dir {} -r {}
127
123
""" .format (
128
124
pip_command ,
129
125
"--force-reinstall" if force_reinstall else "" ,
126
+ requirements_path ,
130
127
)
131
128
)
132
129
133
130
if extra_packages is not None :
134
- for extra in extra_packages :
135
- package_name = os .path .basename (extra )
131
+ for package in extra_packages :
136
132
ret += textwrap .dedent (
137
133
"""
138
- {}
139
134
RUN {} install --no-cache-dir {} {}
140
135
""" .format (
141
- _generate_copy_command (extra , package_name ),
142
136
pip_command ,
143
137
"--force-reinstall" if force_reinstall else "" ,
144
- quote (package_name ),
138
+ quote (package ),
145
139
)
146
140
)
147
141
@@ -190,24 +184,18 @@ def _prepare_entrypoint(package: Package, python_command: str = "python") -> str
190
184
return "\n ENTRYPOINT {}\n " .format (exec_str )
191
185
192
186
193
- def _prepare_package_entry (package : Package ) -> str :
194
- """Returns the Dockerfile entries required to append at the end before entrypoint.
195
-
196
- Including:
197
- - copy the parent directory of the main executable into a docker container.
198
- - inject an entrypoint that executes a script or python module inside that
199
- directory.
187
+ def _copy_source_directory () -> str :
188
+ """Returns the Dockerfile entry required to copy the package to the image.
200
189
201
- Args:
202
- package (Package):
203
- Required. The main application copied to and run in the container.
190
+ The Docker build context has been changed to host_workdir. We copy all
191
+ the files to the working directory of images.
204
192
205
193
Returns:
206
- The generated package related command used in Dockerfile.
194
+ The generated package related copy command used in Dockerfile.
207
195
"""
208
196
copy_code = _generate_copy_command (
209
197
"." , # Dockefile context location has been changed to host_workdir
210
- Path ( package . package_path ). name ,
198
+ "." , # Copy all the files to the working directory of images.
211
199
comment = "Copy the source directory into the docker container." ,
212
200
)
213
201
@@ -275,14 +263,18 @@ def _get_relative_path_to_workdir(
275
263
The relative path to the workdir or None if path is None.
276
264
277
265
Raises:
278
- ValueError: If the path is not relative to the workdir.
266
+ ValueError: If the path does not exist or is not relative to the workdir.
279
267
"""
280
268
if path is None :
281
269
return None
282
270
271
+ if not Path (path ).is_file ():
272
+ raise ValueError (f'The { value_name } "{ path } " must exist.' )
283
273
if not path_utils ._is_relative_to (path , workdir ):
284
274
raise ValueError (f'The { value_name } "{ path } " must be in "{ workdir } ".' )
285
- return Path (path ).relative_to (workdir ).as_posix ()
275
+ abs_path = Path (path ).expanduser ().resolve ()
276
+ abs_workdir = Path (workdir ).expanduser ().resolve ()
277
+ return Path (abs_path ).relative_to (abs_workdir ).as_posix ()
286
278
287
279
288
280
def make_dockerfile (
@@ -382,8 +374,10 @@ def make_dockerfile(
382
374
environment_variables = environment_variables
383
375
)
384
376
385
- # Installs packages from requirements_path which copies requirements_path
386
- # to the image before installing.
377
+ # Copies user code to the image.
378
+ dockerfile += _copy_source_directory ()
379
+
380
+ # Installs packages from requirements_path.
387
381
dockerfile += _prepare_dependency_entries (
388
382
requirements_path = requirements_path ,
389
383
setup_path = None ,
@@ -394,9 +388,6 @@ def make_dockerfile(
394
388
pip_command = pip_command ,
395
389
)
396
390
397
- # Copies user code to the image.
398
- dockerfile += _prepare_package_entry (main_package )
399
-
400
391
# Installs additional packages from user code.
401
392
dockerfile += _prepare_dependency_entries (
402
393
requirements_path = None ,
0 commit comments