diff --git a/container/core.py b/container/core.py index fba9487b..7d0c0286 100644 --- a/container/core.py +++ b/container/core.py @@ -731,6 +731,13 @@ def conductorcmd_build(engine_name, project_name, services, cache=True, local_py environment=dict(ANSIBLE_CONTAINER=1) ) + if service.get('build_overrides'): + for key in service['build_overrides']: + if key in ['user', 'working_dir', 'command', 'privileged']: + run_kwargs[key] = service['build_overrides'][key] + + run_kwargs['environment'].update(service['build_overrides']['environment']) + if service.get('volumes'): for volume in service['volumes']: pieces = volume.split(':') diff --git a/docs/rst/container_yml/reference.rst b/docs/rst/container_yml/reference.rst index 382d0584..cd5ec3d2 100644 --- a/docs/rst/container_yml/reference.rst +++ b/docs/rst/container_yml/reference.rst @@ -220,6 +220,8 @@ with a checkmark in the *Supported* column can be used. Directive Definition Supported? ===================== ======================================================== ============ build Run Dockerfile based build +:ref:`build_over` Service level directives that apply only in container + build time |checkmark| cap_add Add container capabilities cap_drop Drop container capabilities command Command executed by the container at startup |checkmark| @@ -290,6 +292,39 @@ Implementation The following provides details about how specific directives are implemented. +.. _build_over: + +build_overrides +............. + +Use for directives that should only be applied during the execution of the ``build`` command. For example, +consider the following ``container.yml`` file: + +.. code-block:: yaml + + version: '2' + services: + web: + from: centos:7 + command: [nginx] + entrypoint: [/usr/bin/entrypoint.sh] + ports: + - 8000:8000 + build_overrides: + command: /usr/sbin/init + user: root + working_dir: /somepath + environment: + - container: docker + + +In this example, when ``ansible-container build`` is executed, the options found in *build_overrides* will +take effect, and the building container will run command ``/usr/sbin/init`` rather than default ``sh -c "while true; do sleep 1; done``, have extra environment variables ``container=docker`` mapped to the container's environment and the container's working directory will be ``/somepath``. + +The ``run`` and ``deploy`` commands ignore *build_overrides*. When ``run`` or ``deploy`` executes, the container will not run the command or use environment variables specified in ``build_overrides`` directive. + +Supported directives in ``build_overrides`` are ``command``, ``user``, ``working_dir``, ``privileged`` and ``environment`` + .. _depends_on: depends_on