Exemple #1
0
 def testGenerate(self):
     generated_dockerfile_path = labels.DOCKERFILE_NAME
     dockerfile.Dockerfile(filename=generated_dockerfile_path)
     self.assertTrue(
         filecmp.cmp(
             os.path.join(os.path.dirname(__file__), 'testdata',
                          'test_dockerfile'), generated_dockerfile_path))
Exemple #2
0
 def testGenerateWithBaseOverride(self):
     generated_dockerfile_path = labels.DOCKERFILE_NAME
     dockerfile.Dockerfile(filename=generated_dockerfile_path,
                           base_image='my_customized_image:latest')
     self.assertTrue(
         filecmp.cmp(
             os.path.join(self._testdata_dir, 'test_dockerfile_with_base'),
             generated_dockerfile_path))
Exemple #3
0
def build(target_image: str,
          base_image: Optional[str] = None,
          dockerfile_name: Optional[str] = None,
          setup_py_filename: Optional[str] = None) -> str:
    """Build containers.

  Generates a dockerfile if needed and build a container image using docker SDK.

  Args:
    target_image: the target image path to be built.
    base_image: the image path to use as the base image.
    dockerfile_name: the dockerfile name, which is stored in the workspace
      directory. The default workspace directory is '.' and cannot be changed
      for now.
    setup_py_filename: the setup.py file name, which is used to build a python
      package for the workspace directory. If not specified, the whole directory
      is copied and PYTHONPATH is configured.

  Returns:
    Built image name with sha256 id.
  """
    dockerfile_name = dockerfile_name or os.path.join(labels.BUILD_CONTEXT,
                                                      labels.DOCKERFILE_NAME)

    dockerfile.Dockerfile(filename=dockerfile_name,
                          setup_py_filename=setup_py_filename,
                          base_image=base_image)

    # Uses Low-level API for log streaming.
    docker_low_client = docker.APIClient(**docker_utils.kwargs_from_env(),
                                         timeout=_BUILD_TIMEOUT_SECS)
    log_stream = docker_low_client.build(
        path=labels.BUILD_CONTEXT,
        dockerfile=dockerfile_name,
        tag=target_image,
        rm=True,
        decode=True,
    )
    _print_docker_log_stream(log_stream, 'stream')

    docker_client = docker.from_env(timeout=_BUILD_TIMEOUT_SECS)
    log_stream = docker_client.images.push(repository=target_image,
                                           stream=True,
                                           decode=True)
    _print_docker_log_stream(log_stream, 'status', newline=True)

    image_id = docker_client.images.get_registry_data(target_image).id
    return f'{_get_image_repo(target_image)}@{image_id}'
Exemple #4
0
    def test_generate(self):
        # change to a temporary working dir such that there is no setup.py
        # in the working dir.
        old_working_dir = os.getcwd()
        tmp_working_dir = tempfile.mkdtemp()
        os.chdir(tmp_working_dir)

        test_dockerfile_name = 'test_dockerfile'
        default_dockerfile_path = os.path.join(os.path.dirname(__file__),
                                               'testdata',
                                               test_dockerfile_name)
        generated_dockerfile_path = labels.DOCKERFILE_NAME
        dockerfile.Dockerfile(filename=generated_dockerfile_path)
        self.assertTrue(
            filecmp.cmp(default_dockerfile_path, generated_dockerfile_path))

        # clean up
        os.chdir(old_working_dir)
Exemple #5
0
 def testDevVersionRequirement(self):
     with self._patchVersion('0.23.0.dev'):
         with self.assertRaisesRegex(
                 ValueError, 'Cannot find a base image automatically'):
             dockerfile.Dockerfile(filename=labels.DOCKERFILE_NAME)
Exemple #6
0
 def testGenerate(self):
     generated_dockerfile_path = labels.DOCKERFILE_NAME
     with self._patchVersion(_FAKE_VERSION):
         dockerfile.Dockerfile(filename=generated_dockerfile_path)
         self.assertTrue(
             filecmp.cmp(self._test_dockerfile, generated_dockerfile_path))
Exemple #7
0
 def testGenerate(self):
     generated_dockerfile_path = labels.DOCKERFILE_NAME
     dockerfile.Dockerfile(filename=generated_dockerfile_path)
     self.assertTrue(
         filecmp.cmp(self._test_dockerfile, generated_dockerfile_path))