2018-03-19 23:41:48 +00:00
|
|
|
"""Generates a variety of Dockerfiles based on an input matrix
|
2017-11-30 07:20:24 +00:00
|
|
|
"""
|
|
|
|
import os
|
2022-10-31 22:32:14 +00:00
|
|
|
|
2017-11-30 07:20:24 +00:00
|
|
|
import docker
|
2022-10-31 22:32:14 +00:00
|
|
|
|
2017-11-30 07:20:24 +00:00
|
|
|
from .base import BuildPack
|
|
|
|
|
|
|
|
|
|
|
|
class DockerBuildPack(BuildPack):
|
2018-03-19 23:41:48 +00:00
|
|
|
"""Docker BuildPack"""
|
2019-05-31 09:10:17 +00:00
|
|
|
|
2017-11-30 07:20:24 +00:00
|
|
|
dockerfile = "Dockerfile"
|
|
|
|
|
|
|
|
def detect(self):
|
2018-03-19 23:41:48 +00:00
|
|
|
"""Check if current repo should be built with the Docker BuildPack"""
|
2019-05-31 09:10:17 +00:00
|
|
|
return os.path.exists(self.binder_path("Dockerfile"))
|
2017-11-30 07:20:24 +00:00
|
|
|
|
2020-10-15 12:58:12 +00:00
|
|
|
def render(self, build_args=None):
|
2018-03-19 23:41:48 +00:00
|
|
|
"""Render the Dockerfile using by reading it from the source repo"""
|
2019-05-31 09:10:17 +00:00
|
|
|
Dockerfile = self.binder_path("Dockerfile")
|
2017-11-30 07:20:24 +00:00
|
|
|
with open(Dockerfile) as f:
|
2018-03-07 10:27:54 +00:00
|
|
|
return f.read()
|
2017-11-30 07:20:24 +00:00
|
|
|
|
2019-05-31 09:10:17 +00:00
|
|
|
def build(
|
|
|
|
self,
|
|
|
|
client,
|
|
|
|
image_spec,
|
|
|
|
memory_limit,
|
|
|
|
build_args,
|
|
|
|
cache_from,
|
|
|
|
extra_build_kwargs,
|
|
|
|
):
|
2018-03-19 23:41:48 +00:00
|
|
|
"""Build a Docker image based on the Dockerfile in the source repo."""
|
2019-05-08 06:11:38 +00:00
|
|
|
# If you work on this bit of code check the corresponding code in
|
|
|
|
# buildpacks/base.py where it is duplicated
|
2019-05-08 11:20:31 +00:00
|
|
|
if not isinstance(memory_limit, int):
|
2019-05-31 09:10:17 +00:00
|
|
|
raise ValueError(
|
2022-10-23 17:18:37 +00:00
|
|
|
"The memory limit has to be specified as an "
|
|
|
|
f"integer but is '{type(memory_limit)}'"
|
2019-05-31 09:10:17 +00:00
|
|
|
)
|
2019-05-08 06:11:38 +00:00
|
|
|
limits = {}
|
2017-12-01 01:14:42 +00:00
|
|
|
if memory_limit:
|
2019-05-08 16:44:58 +00:00
|
|
|
# We want to always disable swap. Docker expects `memswap` to
|
|
|
|
# be total allowable memory, *including* swap - while `memory`
|
|
|
|
# points to non-swap memory. We set both values to the same so
|
|
|
|
# we use no swap.
|
2019-05-31 09:10:17 +00:00
|
|
|
limits = {"memory": memory_limit, "memswap": memory_limit}
|
2019-02-19 09:45:48 +00:00
|
|
|
|
|
|
|
build_kwargs = dict(
|
|
|
|
path=os.getcwd(),
|
|
|
|
dockerfile=self.binder_path(self.dockerfile),
|
|
|
|
tag=image_spec,
|
|
|
|
buildargs=build_args,
|
|
|
|
container_limits=limits,
|
2019-05-31 09:10:17 +00:00
|
|
|
cache_from=cache_from,
|
2021-11-23 18:42:24 +00:00
|
|
|
labels=self.get_labels(),
|
2019-02-19 09:45:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
build_kwargs.update(extra_build_kwargs)
|
|
|
|
|
2022-10-23 16:09:11 +00:00
|
|
|
yield from client.build(**build_kwargs)
|