Adds support for a memory flag to docker-compose build.

Signed-off-by: Samantha Miller <samantha.a.miller123@gmail.com>
This commit is contained in:
Samantha Miller 2017-11-24 22:53:48 -06:00
parent d48002a09d
commit a6cdd62726
9 changed files with 29 additions and 4 deletions

View File

@ -232,6 +232,7 @@ class TopLevelCommand(object):
--force-rm Always remove intermediate containers. --force-rm Always remove intermediate containers.
--no-cache Do not use cache when building the image. --no-cache Do not use cache when building the image.
--pull Always attempt to pull a newer version of the image. --pull Always attempt to pull a newer version of the image.
-m, --memory MEM Sets memory limit for the bulid container.
--build-arg key=val Set build-time variables for one service. --build-arg key=val Set build-time variables for one service.
""" """
service_names = options['SERVICE'] service_names = options['SERVICE']
@ -248,6 +249,7 @@ class TopLevelCommand(object):
no_cache=bool(options.get('--no-cache', False)), no_cache=bool(options.get('--no-cache', False)),
pull=bool(options.get('--pull', False)), pull=bool(options.get('--pull', False)),
force_rm=bool(options.get('--force-rm', False)), force_rm=bool(options.get('--force-rm', False)),
memory=options.get('--memory'),
build_args=build_args) build_args=build_args)
def bundle(self, config_options, options): def bundle(self, config_options, options):

View File

@ -357,10 +357,11 @@ class Project(object):
) )
return containers return containers
def build(self, service_names=None, no_cache=False, pull=False, force_rm=False, build_args=None): def build(self, service_names=None, no_cache=False, pull=False, force_rm=False, memory=None,
build_args=None):
for service in self.get_services(service_names): for service in self.get_services(service_names):
if service.can_be_built(): if service.can_be_built():
service.build(no_cache, pull, force_rm, build_args) service.build(no_cache, pull, force_rm, memory, build_args)
else: else:
log.info('%s uses an image, skipping' % service.name) log.info('%s uses an image, skipping' % service.name)

View File

@ -900,7 +900,7 @@ class Service(object):
return [build_spec(secret) for secret in self.secrets] return [build_spec(secret) for secret in self.secrets]
def build(self, no_cache=False, pull=False, force_rm=False, build_args_override=None): def build(self, no_cache=False, pull=False, force_rm=False, memory=None, build_args_override=None):
log.info('Building %s' % self.name) log.info('Building %s' % self.name)
build_opts = self.options.get('build', {}) build_opts = self.options.get('build', {})
@ -931,6 +931,9 @@ class Service(object):
target=build_opts.get('target', None), target=build_opts.get('target', None),
shmsize=parse_bytes(build_opts.get('shm_size')) if build_opts.get('shm_size') else None, shmsize=parse_bytes(build_opts.get('shm_size')) if build_opts.get('shm_size') else None,
extra_hosts=build_opts.get('extra_hosts', None), extra_hosts=build_opts.get('extra_hosts', None),
container_limits={
'memory': parse_bytes(memory) if memory else None
},
) )
try: try:

View File

@ -120,7 +120,7 @@ _docker_compose_build() {
case "$cur" in case "$cur" in
-*) -*)
COMPREPLY=( $( compgen -W "--build-arg --force-rm --help --no-cache --pull" -- "$cur" ) ) COMPREPLY=( $( compgen -W "--build-arg --force-rm --help --memory --no-cache --pull" -- "$cur" ) )
;; ;;
*) *)
__docker_compose_services_from_build __docker_compose_services_from_build

View File

@ -196,6 +196,7 @@ __docker-compose_subcommand() {
$opts_help \ $opts_help \
"*--build-arg=[Set build-time variables for one service.]:<varname>=<value>: " \ "*--build-arg=[Set build-time variables for one service.]:<varname>=<value>: " \
'--force-rm[Always remove intermediate containers.]' \ '--force-rm[Always remove intermediate containers.]' \
'--memory[Memory limit for the build container.]' \
'--no-cache[Do not use cache when building the image.]' \ '--no-cache[Do not use cache when building the image.]' \
'--pull[Always attempt to pull a newer version of the image.]' \ '--pull[Always attempt to pull a newer version of the image.]' \
'*:services:__docker-compose_services_from_build' && ret=0 '*:services:__docker-compose_services_from_build' && ret=0

View File

@ -584,6 +584,12 @@ class CLITestCase(DockerClientTestCase):
result = self.dispatch(['build', '--no-cache'], None) result = self.dispatch(['build', '--no-cache'], None)
assert 'shm_size: 96' in result.stdout assert 'shm_size: 96' in result.stdout
def test_build_memory_build_option(self):
pull_busybox(self.client)
self.base_dir = 'tests/fixtures/build-memory'
result = self.dispatch(['build', '--no-cache', '--memory', '96m', 'service'], None)
assert 'memory: 100663296' in result.stdout # 96 * 1024 * 1024
def test_bundle_with_digests(self): def test_bundle_with_digests(self):
self.base_dir = 'tests/fixtures/bundle-with-digests/' self.base_dir = 'tests/fixtures/bundle-with-digests/'
tmpdir = pytest.ensuretemp('cli_test_bundle') tmpdir = pytest.ensuretemp('cli_test_bundle')

View File

@ -0,0 +1,4 @@
FROM busybox
# Report the memory (through the size of the group memory)
RUN echo "memory:" $(cat /sys/fs/cgroup/memory/memory.limit_in_bytes)

View File

@ -0,0 +1,6 @@
version: '3.5'
services:
service:
build:
context: .

View File

@ -499,6 +499,7 @@ class ServiceTest(unittest.TestCase):
target=None, target=None,
shmsize=None, shmsize=None,
extra_hosts=None, extra_hosts=None,
container_limits={'memory': None},
) )
def test_ensure_image_exists_no_build(self): def test_ensure_image_exists_no_build(self):
@ -541,6 +542,7 @@ class ServiceTest(unittest.TestCase):
target=None, target=None,
shmsize=None, shmsize=None,
extra_hosts=None, extra_hosts=None,
container_limits={'memory': None},
) )
def test_build_does_not_pull(self): def test_build_does_not_pull(self):