mirror of
https://github.com/docker/compose.git
synced 2025-07-29 16:44:20 +02:00
Merge pull request #4564 from shin-/dattran-vn01-issue/3790-support-build-arg-for-build-command
support --build-arg for build command
This commit is contained in:
commit
05e1b3aa1d
@ -22,6 +22,7 @@ from ..bundle import MissingDigests
|
|||||||
from ..bundle import serialize_bundle
|
from ..bundle import serialize_bundle
|
||||||
from ..config import ConfigurationError
|
from ..config import ConfigurationError
|
||||||
from ..config import parse_environment
|
from ..config import parse_environment
|
||||||
|
from ..config import resolve_build_args
|
||||||
from ..config.environment import Environment
|
from ..config.environment import Environment
|
||||||
from ..config.serialize import serialize_config
|
from ..config.serialize import serialize_config
|
||||||
from ..config.types import VolumeSpec
|
from ..config.types import VolumeSpec
|
||||||
@ -209,18 +210,29 @@ class TopLevelCommand(object):
|
|||||||
e.g. `composetest_db`. If you change a service's `Dockerfile` or the
|
e.g. `composetest_db`. If you change a service's `Dockerfile` or the
|
||||||
contents of its build directory, you can run `docker-compose build` to rebuild it.
|
contents of its build directory, you can run `docker-compose build` to rebuild it.
|
||||||
|
|
||||||
Usage: build [options] [SERVICE...]
|
Usage: build [options] [--build-arg key=val...] [SERVICE...]
|
||||||
|
|
||||||
Options:
|
Options:
|
||||||
--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.
|
||||||
|
--build-arg key=val Set build-time variables for one service.
|
||||||
"""
|
"""
|
||||||
|
service_names = options['SERVICE']
|
||||||
|
build_args = options.get('--build-arg', None)
|
||||||
|
if build_args:
|
||||||
|
environment = Environment.from_env_file(self.project_dir)
|
||||||
|
build_args = resolve_build_args(build_args, environment)
|
||||||
|
|
||||||
|
if not service_names and build_args:
|
||||||
|
raise UserError("Need service name for --build-arg option")
|
||||||
|
|
||||||
self.project.build(
|
self.project.build(
|
||||||
service_names=options['SERVICE'],
|
service_names=service_names,
|
||||||
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)),
|
||||||
|
build_args=build_args)
|
||||||
|
|
||||||
def bundle(self, config_options, options):
|
def bundle(self, config_options, options):
|
||||||
"""
|
"""
|
||||||
|
@ -9,3 +9,4 @@ from .config import find
|
|||||||
from .config import load
|
from .config import load
|
||||||
from .config import merge_environment
|
from .config import merge_environment
|
||||||
from .config import parse_environment
|
from .config import parse_environment
|
||||||
|
from .config import resolve_build_args
|
||||||
|
@ -602,8 +602,8 @@ def resolve_environment(service_dict, environment=None):
|
|||||||
return dict(resolve_env_var(k, v, environment) for k, v in six.iteritems(env))
|
return dict(resolve_env_var(k, v, environment) for k, v in six.iteritems(env))
|
||||||
|
|
||||||
|
|
||||||
def resolve_build_args(build, environment):
|
def resolve_build_args(buildargs, environment):
|
||||||
args = parse_build_arguments(build.get('args'))
|
args = parse_build_arguments(buildargs)
|
||||||
return dict(resolve_env_var(k, v, environment) for k, v in six.iteritems(args))
|
return dict(resolve_env_var(k, v, environment) for k, v in six.iteritems(args))
|
||||||
|
|
||||||
|
|
||||||
@ -1051,7 +1051,7 @@ def normalize_build(service_dict, working_dir, environment):
|
|||||||
build.update(service_dict['build'])
|
build.update(service_dict['build'])
|
||||||
if 'args' in build:
|
if 'args' in build:
|
||||||
build['args'] = build_string_dict(
|
build['args'] = build_string_dict(
|
||||||
resolve_build_args(build, environment)
|
resolve_build_args(build.get('args'), environment)
|
||||||
)
|
)
|
||||||
|
|
||||||
service_dict['build'] = build
|
service_dict['build'] = build
|
||||||
|
@ -307,10 +307,10 @@ class Project(object):
|
|||||||
'Restarting')
|
'Restarting')
|
||||||
return containers
|
return containers
|
||||||
|
|
||||||
def build(self, service_names=None, no_cache=False, pull=False, force_rm=False):
|
def build(self, service_names=None, no_cache=False, pull=False, force_rm=False, 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)
|
service.build(no_cache, pull, force_rm, build_args)
|
||||||
else:
|
else:
|
||||||
log.info('%s uses an image, skipping' % service.name)
|
log.info('%s uses an image, skipping' % service.name)
|
||||||
|
|
||||||
|
@ -803,13 +803,18 @@ 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):
|
def build(self, no_cache=False, pull=False, force_rm=False, 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', {})
|
||||||
path = build_opts.get('context')
|
|
||||||
|
build_args = build_opts.get('args', {}).copy()
|
||||||
|
if build_args_override:
|
||||||
|
build_args.update(build_args_override)
|
||||||
|
|
||||||
# python2 os.stat() doesn't support unicode on some UNIX, so we
|
# python2 os.stat() doesn't support unicode on some UNIX, so we
|
||||||
# encode it to a bytestring to be safe
|
# encode it to a bytestring to be safe
|
||||||
|
path = build_opts.get('context')
|
||||||
if not six.PY3 and not IS_WINDOWS_PLATFORM:
|
if not six.PY3 and not IS_WINDOWS_PLATFORM:
|
||||||
path = path.encode('utf8')
|
path = path.encode('utf8')
|
||||||
|
|
||||||
@ -822,8 +827,8 @@ class Service(object):
|
|||||||
pull=pull,
|
pull=pull,
|
||||||
nocache=no_cache,
|
nocache=no_cache,
|
||||||
dockerfile=build_opts.get('dockerfile', None),
|
dockerfile=build_opts.get('dockerfile', None),
|
||||||
buildargs=build_opts.get('args', None),
|
|
||||||
cache_from=build_opts.get('cache_from', None),
|
cache_from=build_opts.get('cache_from', None),
|
||||||
|
buildargs=build_args
|
||||||
)
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
@ -597,12 +597,30 @@ class ServiceTest(DockerClientTestCase):
|
|||||||
with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
|
with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
|
||||||
f.write("FROM busybox\n")
|
f.write("FROM busybox\n")
|
||||||
f.write("ARG build_version\n")
|
f.write("ARG build_version\n")
|
||||||
|
f.write("RUN echo ${build_version}\n")
|
||||||
|
|
||||||
service = self.create_service('buildwithargs',
|
service = self.create_service('buildwithargs',
|
||||||
build={'context': text_type(base_dir),
|
build={'context': text_type(base_dir),
|
||||||
'args': {"build_version": "1"}})
|
'args': {"build_version": "1"}})
|
||||||
service.build()
|
service.build()
|
||||||
assert service.image()
|
assert service.image()
|
||||||
|
assert "build_version=1" in service.image()['ContainerConfig']['Cmd']
|
||||||
|
|
||||||
|
def test_build_with_build_args_override(self):
|
||||||
|
base_dir = tempfile.mkdtemp()
|
||||||
|
self.addCleanup(shutil.rmtree, base_dir)
|
||||||
|
|
||||||
|
with open(os.path.join(base_dir, 'Dockerfile'), 'w') as f:
|
||||||
|
f.write("FROM busybox\n")
|
||||||
|
f.write("ARG build_version\n")
|
||||||
|
f.write("RUN echo ${build_version}\n")
|
||||||
|
|
||||||
|
service = self.create_service('buildwithargs',
|
||||||
|
build={'context': text_type(base_dir),
|
||||||
|
'args': {"build_version": "1"}})
|
||||||
|
service.build(build_args_override={'build_version': '2'})
|
||||||
|
assert service.image()
|
||||||
|
assert "build_version=2" in service.image()['ContainerConfig']['Cmd']
|
||||||
|
|
||||||
def test_start_container_stays_unprivileged(self):
|
def test_start_container_stays_unprivileged(self):
|
||||||
service = self.create_service('web')
|
service = self.create_service('web')
|
||||||
@ -1057,7 +1075,7 @@ class ServiceTest(DockerClientTestCase):
|
|||||||
one_off_container = service.create_container(one_off=True)
|
one_off_container = service.create_container(one_off=True)
|
||||||
self.assertNotEqual(one_off_container.name, 'my-web-container')
|
self.assertNotEqual(one_off_container.name, 'my-web-container')
|
||||||
|
|
||||||
@pytest.mark.skipif(True, reason="Broken on 1.11.0rc1")
|
@pytest.mark.skipif(True, reason="Broken on 1.11.0 - 17.03.0")
|
||||||
def test_log_drive_invalid(self):
|
def test_log_drive_invalid(self):
|
||||||
service = self.create_service('web', logging={'driver': 'xxx'})
|
service = self.create_service('web', logging={'driver': 'xxx'})
|
||||||
expected_error_msg = "logger: no log driver named 'xxx' is registered"
|
expected_error_msg = "logger: no log driver named 'xxx' is registered"
|
||||||
|
@ -1522,7 +1522,7 @@ class ConfigTest(unittest.TestCase):
|
|||||||
assert actual == {
|
assert actual == {
|
||||||
'image': 'alpine:edge',
|
'image': 'alpine:edge',
|
||||||
'volumes': ['.:/app'],
|
'volumes': ['.:/app'],
|
||||||
'ports': ['5432']
|
'ports': types.ServicePort.parse('5432')
|
||||||
}
|
}
|
||||||
|
|
||||||
def test_merge_service_dicts_heterogeneous_2(self):
|
def test_merge_service_dicts_heterogeneous_2(self):
|
||||||
@ -1541,40 +1541,7 @@ class ConfigTest(unittest.TestCase):
|
|||||||
assert actual == {
|
assert actual == {
|
||||||
'image': 'alpine:edge',
|
'image': 'alpine:edge',
|
||||||
'volumes': ['.:/app'],
|
'volumes': ['.:/app'],
|
||||||
'ports': ['5432']
|
'ports': types.ServicePort.parse('5432')
|
||||||
}
|
|
||||||
|
|
||||||
def test_merge_build_args(self):
|
|
||||||
base = {
|
|
||||||
'build': {
|
|
||||||
'context': '.',
|
|
||||||
'args': {
|
|
||||||
'ONE': '1',
|
|
||||||
'TWO': '2',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
override = {
|
|
||||||
'build': {
|
|
||||||
'args': {
|
|
||||||
'TWO': 'dos',
|
|
||||||
'THREE': '3',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
actual = config.merge_service_dicts(
|
|
||||||
base,
|
|
||||||
override,
|
|
||||||
DEFAULT_VERSION)
|
|
||||||
assert actual == {
|
|
||||||
'build': {
|
|
||||||
'context': '.',
|
|
||||||
'args': {
|
|
||||||
'ONE': '1',
|
|
||||||
'TWO': 'dos',
|
|
||||||
'THREE': '3',
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
def test_merge_logging_v1(self):
|
def test_merge_logging_v1(self):
|
||||||
@ -2877,7 +2844,7 @@ class EnvTest(unittest.TestCase):
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
resolve_build_args(build, Environment.from_env_file(build['context'])),
|
resolve_build_args(build['args'], Environment.from_env_file(build['context'])),
|
||||||
{'arg1': 'value1', 'empty_arg': '', 'env_arg': 'value2', 'no_env': None},
|
{'arg1': 'value1', 'empty_arg': '', 'env_arg': 'value2', 'no_env': None},
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -461,7 +461,7 @@ class ServiceTest(unittest.TestCase):
|
|||||||
forcerm=False,
|
forcerm=False,
|
||||||
nocache=False,
|
nocache=False,
|
||||||
rm=True,
|
rm=True,
|
||||||
buildargs=None,
|
buildargs={},
|
||||||
cache_from=None,
|
cache_from=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -498,7 +498,7 @@ class ServiceTest(unittest.TestCase):
|
|||||||
forcerm=False,
|
forcerm=False,
|
||||||
nocache=False,
|
nocache=False,
|
||||||
rm=True,
|
rm=True,
|
||||||
buildargs=None,
|
buildargs={},
|
||||||
cache_from=None,
|
cache_from=None,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -513,6 +513,23 @@ class ServiceTest(unittest.TestCase):
|
|||||||
self.assertEqual(self.mock_client.build.call_count, 1)
|
self.assertEqual(self.mock_client.build.call_count, 1)
|
||||||
self.assertFalse(self.mock_client.build.call_args[1]['pull'])
|
self.assertFalse(self.mock_client.build.call_args[1]['pull'])
|
||||||
|
|
||||||
|
def test_build_with_override_build_args(self):
|
||||||
|
self.mock_client.build.return_value = [
|
||||||
|
b'{"stream": "Successfully built 12345"}',
|
||||||
|
]
|
||||||
|
|
||||||
|
build_args = {
|
||||||
|
'arg1': 'arg1_new_value',
|
||||||
|
}
|
||||||
|
service = Service('foo', client=self.mock_client,
|
||||||
|
build={'context': '.', 'args': {'arg1': 'arg1', 'arg2': 'arg2'}})
|
||||||
|
service.build(build_args_override=build_args)
|
||||||
|
|
||||||
|
called_build_args = self.mock_client.build.call_args[1]['buildargs']
|
||||||
|
|
||||||
|
assert called_build_args['arg1'] == build_args['arg1']
|
||||||
|
assert called_build_args['arg2'] == 'arg2'
|
||||||
|
|
||||||
def test_config_dict(self):
|
def test_config_dict(self):
|
||||||
self.mock_client.inspect_image.return_value = {'Id': 'abcd'}
|
self.mock_client.inspect_image.return_value = {'Id': 'abcd'}
|
||||||
service = Service(
|
service = Service(
|
||||||
|
Loading…
x
Reference in New Issue
Block a user