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
|
|
|
|
import docker
|
|
|
|
from .base import BuildPack
|
|
|
|
|
|
|
|
|
|
|
|
class DockerBuildPack(BuildPack):
|
2018-03-19 23:41:48 +00:00
|
|
|
"""Docker BuildPack"""
|
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"""
|
2017-11-30 07:20:24 +00:00
|
|
|
return os.path.exists(self.binder_path('Dockerfile'))
|
|
|
|
|
|
|
|
def render(self):
|
2018-03-19 23:41:48 +00:00
|
|
|
"""Render the Dockerfile using by reading it from the source repo"""
|
2017-11-30 07:20:24 +00:00
|
|
|
Dockerfile = self.binder_path('Dockerfile')
|
|
|
|
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-02-19 09:45:48 +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."""
|
2017-12-01 01:14:42 +00:00
|
|
|
limits = {
|
|
|
|
# Always disable memory swap for building, since mostly
|
|
|
|
# nothing good can come of that.
|
2017-12-01 01:17:03 +00:00
|
|
|
'memswap': -1
|
2017-12-01 01:14:42 +00:00
|
|
|
}
|
|
|
|
if memory_limit:
|
|
|
|
limits['memory'] = 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,
|
|
|
|
decode=True,
|
|
|
|
forcerm=True,
|
|
|
|
rm=True,
|
|
|
|
container_limits=limits,
|
|
|
|
cache_from=cache_from
|
|
|
|
)
|
|
|
|
|
|
|
|
build_kwargs.update(extra_build_kwargs)
|
|
|
|
|
|
|
|
for line in client.build(**build_kwargs):
|
2017-11-30 07:20:24 +00:00
|
|
|
yield line
|