private void dockerWriteTo(OutputStream out) throws IOException {
    TarStreamBuilder tarStreamBuilder = new TarStreamBuilder();
    DockerManifestEntryTemplate manifestTemplate = new DockerManifestEntryTemplate();

    // Adds all the layers to the tarball and manifest.
    for (Layer layer : image.getLayers()) {
      String layerName = layer.getBlobDescriptor().getDigest().getHash() + LAYER_FILE_EXTENSION;

      tarStreamBuilder.addBlobEntry(
          layer.getBlob(),
          layer.getBlobDescriptor().getSize(),
          layerName,
          TAR_ENTRY_MODIFICATION_TIME);
      manifestTemplate.addLayerFile(layerName);
    }

    // Adds the container configuration to the tarball.
    JsonTemplate containerConfiguration =
        new ImageToJsonTranslator(image).getContainerConfiguration();
    tarStreamBuilder.addByteEntry(
        JsonTemplateMapper.toByteArray(containerConfiguration),
        CONTAINER_CONFIGURATION_JSON_FILE_NAME,
        TAR_ENTRY_MODIFICATION_TIME);

    // Adds the manifest to tarball.
    for (String tag : allTargetImageTags) {
      manifestTemplate.addRepoTag(imageReference.withQualifier(tag).toStringWithQualifier());
    }
    tarStreamBuilder.addByteEntry(
        JsonTemplateMapper.toByteArray(Collections.singletonList(manifestTemplate)),
        MANIFEST_JSON_FILE_NAME,
        TAR_ENTRY_MODIFICATION_TIME);

    tarStreamBuilder.writeAsTarArchiveTo(out);
  }
Writes image tar bar in configured {@link Image#getImageFormat()} of OCI or Docker to output
stream.

@param out the target output stream
@throws IOException if an error occurs writing out the image to stream
jib-core/src/main/java/com/google/cloud/tools/jib/image/ImageTarball.java