mirror of
https://github.com/docker/compose.git
synced 2025-07-26 15:14:04 +02:00
Merge pull request #5698 from docker/artagnon-build-args-services
Allow buildargs for multi-service builds
This commit is contained in:
commit
cd3ffe651b
@ -234,19 +234,21 @@ class TopLevelCommand(object):
|
|||||||
--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 build container.
|
-m, --memory MEM Sets memory limit for the build container.
|
||||||
--build-arg key=val Set build-time variables for one service.
|
--build-arg key=val Set build-time variables for services.
|
||||||
"""
|
"""
|
||||||
service_names = options['SERVICE']
|
service_names = options['SERVICE']
|
||||||
build_args = options.get('--build-arg', None)
|
build_args = options.get('--build-arg', None)
|
||||||
if build_args:
|
if build_args:
|
||||||
|
if not service_names and docker.utils.version_lt(self.project.client.api_version, '1.25'):
|
||||||
|
raise UserError(
|
||||||
|
'--build-arg is only supported when services are specified for API version < 1.25.'
|
||||||
|
' Please use a Compose file version > 2.2 or specify which services to build.'
|
||||||
|
)
|
||||||
environment = Environment.from_env_file(self.project_dir)
|
environment = Environment.from_env_file(self.project_dir)
|
||||||
build_args = resolve_build_args(build_args, environment)
|
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=service_names,
|
service_names=options['SERVICE'],
|
||||||
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)),
|
||||||
|
@ -658,6 +658,33 @@ class CLITestCase(DockerClientTestCase):
|
|||||||
result = self.dispatch(['build', '--no-cache', '--memory', '96m', 'service'], None)
|
result = self.dispatch(['build', '--no-cache', '--memory', '96m', 'service'], None)
|
||||||
assert 'memory: 100663296' in result.stdout # 96 * 1024 * 1024
|
assert 'memory: 100663296' in result.stdout # 96 * 1024 * 1024
|
||||||
|
|
||||||
|
def test_build_with_buildarg_from_compose_file(self):
|
||||||
|
pull_busybox(self.client)
|
||||||
|
self.base_dir = 'tests/fixtures/build-args'
|
||||||
|
result = self.dispatch(['build'], None)
|
||||||
|
assert 'Favorite Touhou Character: mariya.kirisame' in result.stdout
|
||||||
|
|
||||||
|
def test_build_with_buildarg_cli_override(self):
|
||||||
|
pull_busybox(self.client)
|
||||||
|
self.base_dir = 'tests/fixtures/build-args'
|
||||||
|
result = self.dispatch(['build', '--build-arg', 'favorite_th_character=sakuya.izayoi'], None)
|
||||||
|
assert 'Favorite Touhou Character: sakuya.izayoi' in result.stdout
|
||||||
|
|
||||||
|
@mock.patch.dict(os.environ)
|
||||||
|
def test_build_with_buildarg_old_api_version(self):
|
||||||
|
pull_busybox(self.client)
|
||||||
|
self.base_dir = 'tests/fixtures/build-args'
|
||||||
|
os.environ['COMPOSE_API_VERSION'] = '1.24'
|
||||||
|
result = self.dispatch(
|
||||||
|
['build', '--build-arg', 'favorite_th_character=reimu.hakurei'], None, returncode=1
|
||||||
|
)
|
||||||
|
assert '--build-arg is only supported when services are specified' in result.stderr
|
||||||
|
|
||||||
|
result = self.dispatch(
|
||||||
|
['build', '--build-arg', 'favorite_th_character=hong.meiling', 'web'], None
|
||||||
|
)
|
||||||
|
assert 'Favorite Touhou Character: hong.meiling' in result.stdout
|
||||||
|
|
||||||
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')
|
||||||
|
4
tests/fixtures/build-args/Dockerfile
vendored
Normal file
4
tests/fixtures/build-args/Dockerfile
vendored
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
FROM busybox:latest
|
||||||
|
LABEL com.docker.compose.test_image=true
|
||||||
|
ARG favorite_th_character
|
||||||
|
RUN echo "Favorite Touhou Character: ${favorite_th_character}"
|
7
tests/fixtures/build-args/docker-compose.yml
vendored
Normal file
7
tests/fixtures/build-args/docker-compose.yml
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
version: '2.2'
|
||||||
|
services:
|
||||||
|
web:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
args:
|
||||||
|
- favorite_th_character=mariya.kirisame
|
Loading…
x
Reference in New Issue
Block a user