/** * Adds a new layer to the container with {@code files} as the source files and {@code * pathInContainer} as the path to copy the source files to in the container file system. * * @param files the source files to copy to a new layer in the container * @param pathInContainer the path in the container file system corresponding to the {@code * sourceFile} * @return this * @throws IOException if an exception occurred when recursively listing any directories * @throws IllegalArgumentException if {@code pathInContainer} is not an absolute Unix-style path * @see #addLayer(List, AbsoluteUnixPath) */ public JibContainerBuilder addLayer(List<Path> files, String pathInContainer) throws IOException { return addLayer(files, AbsoluteUnixPath.get(pathInContainer)); }
@VisibleForTesting JibContainer containerize( Containerizer containerizer, Supplier<ExecutorService> defaultExecutorServiceFactory) throws InterruptedException, ExecutionException, IOException, CacheDirectoryCreationException { boolean shutdownExecutorService = !containerizer.getExecutorService().isPresent(); ExecutorService executorService = containerizer.getExecutorService().orElseGet(defaultExecutorServiceFactory); BuildConfiguration buildConfiguration = toBuildConfiguration(containerizer, executorService); try { BuildResult result = containerizer.getTargetImage().toBuildSteps(buildConfiguration).run(); return new JibContainer(result.getImageDigest(), result.getImageId()); } finally { if (shutdownExecutorService) { executorService.shutdown(); } } }
/** * Returns a new {@link JibContainerBuilder} using the parameters specified on the {@link * JavaContainerBuilder}. * * @return a new {@link JibContainerBuilder} using the parameters specified on the {@link * JavaContainerBuilder} */ public JibContainerBuilder toContainerBuilder() { if (mainClass == null) { throw new IllegalStateException( "mainClass is null on JavaContainerBuilder; specify the main class using " + "JavaContainerBuilder#setMainClass(String), or consider using a " + "jib.frontend.MainClassFinder to infer the main class"); } if (classpath.isEmpty()) { throw new IllegalStateException( "Failed to construct entrypoint because no files were added to the JavaContainerBuilder"); } jibContainerBuilder.setEntrypoint( JavaEntrypointConstructor.makeEntrypoint(new ArrayList<>(classpath), jvmFlags, mainClass)); jibContainerBuilder.setLayers(layerConfigurationsBuilder.build().getLayerConfigurations()); return jibContainerBuilder; }
.setLayers(projectProperties.getJavaLayerConfigurations().getLayerConfigurations()) .setEntrypoint(computeEntrypoint(rawConfiguration, projectProperties)) .setProgramArguments(rawConfiguration.getProgramArguments().orElse(null)) .setEnvironment(rawConfiguration.getEnvironment()) .setExposedPorts(ExposedPortsParser.parse(rawConfiguration.getPorts())) .setVolumes(getVolumesSet(rawConfiguration)) .setLabels(rawConfiguration.getLabels()) .setUser(rawConfiguration.getUser().orElse(null)); getWorkingDirectoryChecked(rawConfiguration) .ifPresent(jibContainerBuilder::setWorkingDirectory); LogEvent.warn( "Setting image creation time to current time; your image may not be reproducible.")); jibContainerBuilder.setCreationTime(Instant.now());
final JibContainerBuilder builder = Jib.from(ImageReference.parse(fromImage)); builder .setCreationTime(creationTime == null || creationTime.trim().isEmpty() ? Instant.now() : Instant.parse(creationTime)); builder .setWorkingDirectory(AbsoluteUnixPath .get(workingDirectory == null ? createWorkingDirectory() : workingDirectory)); if (environment != null) { builder.setEnvironment(environment); .setLabels(labels .entrySet() .stream() registryImage.addCredential(credentials.getUsername(), credentials.getPassword()); builder.containerize(configureContainer(Containerizer.to(registryImage), executor)); getLog().info("Pushed image='" + imageName + "', tag='" + tag + "'");
.distinct() .sorted(comparing(Layer::getSize).reversed()) .peek(it -> builder.addLayer(it.layerConfiguration)) .peek(it -> getLog().info("Prepared layer for '" + it.gav + "' dependencies (" + toSize(it.size) + ")")) .map(it -> it.gav) .addEntry(registryLocation.toPath().toAbsolutePath(), AbsoluteUnixPath.get(workingDirectory).resolve(registryLocation.getName())); builder.addLayer(componentsLayerBuilder.build()); getLog() .info("Prepared layer for components " + cars.toString().replace(":car", "") + " (" }) .collect(toList()); builder.addLayer(dependenciesLayer.build()); getLog().info("Prepared layer for main dependencies (" + toSize(mainDepSize.get()) + ")"); classpath.add(mainPath.toString()); builder .addLayer(LayerConfiguration .builder() .setName(project.getArtifactId() + " @" + project.getVersion()) final List<String> newEntrypoint = entryPoint.stream().map(it -> replaceEntrypointPlaceholders(it, cp)).collect(toList()); builder.setEntrypoint(newEntrypoint); if (getLog().isDebugEnabled()) { getLog().debug("Entrypoint set to " + newEntrypoint);
/** * Starts building the container from a base image. * * @param registryImage the {@link RegistryImage} that defines base container registry and * credentials * @return a new {@link JibContainerBuilder} to continue building the container */ public static JibContainerBuilder from(RegistryImage registryImage) { return new JibContainerBuilder(registryImage); }
/** * Sets the container entrypoint. * * @param entrypoint the entrypoint command * @return this * @see #setEntrypoint(List) */ public JibContainerBuilder setEntrypoint(String... entrypoint) { return setEntrypoint(Arrays.asList(entrypoint)); }
private void toLocalDocker(final ExecutorService executor, final JibContainerBuilder builder, final String tag, final String imageName) throws InvalidImageReferenceException, InterruptedException, ExecutionException, IOException, CacheDirectoryCreationException { final DockerDaemonImage docker = DockerDaemonImage.named(imageName); if (dockerEnvironment != null) { docker.setDockerEnvironment(dockerEnvironment); } if (dockerExecutable != null) { docker.setDockerExecutable(dockerExecutable.toPath()); } builder.containerize(configureContainer(Containerizer.to(docker), executor)); getLog().info("Built local image='" + imageName + "', tag='" + tag + "'"); }
/** * Sets the layers. This replaces any previously-added layers. * * @param layerConfigurations the {@link LayerConfiguration}s * @return this */ public JibContainerBuilder setLayers(LayerConfiguration... layerConfigurations) { return setLayers(Arrays.asList(layerConfigurations)); }
/** * Sets the ports to expose from the container. This replaces any previously-set exposed ports. * * @param ports the ports to expose * @return this * @see #setExposedPorts(Set) */ public JibContainerBuilder setExposedPorts(Port... ports) { return setExposedPorts(new HashSet<>(Arrays.asList(ports))); }
/** * Sets the container entrypoint program arguments. * * @param programArguments program arguments tokens * @return this * @see #setProgramArguments(List) */ public JibContainerBuilder setProgramArguments(String... programArguments) { return setProgramArguments(Arrays.asList(programArguments)); }
.setLayers(projectProperties.getJavaLayerConfigurations().getLayerConfigurations()) .setEntrypoint(computeEntrypoint(rawConfiguration, projectProperties)) .setProgramArguments(rawConfiguration.getProgramArguments().orElse(null)) .setEnvironment(rawConfiguration.getEnvironment()) .setExposedPorts(ExposedPortsParser.parse(rawConfiguration.getPorts())) .setVolumes(getVolumesSet(rawConfiguration)) .setLabels(rawConfiguration.getLabels()) .setUser(rawConfiguration.getUser().orElse(null)); getWorkingDirectoryChecked(rawConfiguration) .ifPresent(jibContainerBuilder::setWorkingDirectory); LogEvent.warn( "Setting image creation time to current time; your image may not be reproducible.")); jibContainerBuilder.setCreationTime(Instant.now());
/** * Starts building the container from a base image. The base image should be publicly-available. * For a base image that requires credentials, use {@link #from(RegistryImage)}. * * @param baseImageReference the base image reference * @return a new {@link JibContainerBuilder} to continue building the container */ public static JibContainerBuilder from(ImageReference baseImageReference) { return new JibContainerBuilder(RegistryImage.named(baseImageReference)); }
/** * Sets the container entrypoint. * * @param entrypoint the entrypoint command * @return this * @see #setEntrypoint(List) */ public JibContainerBuilder setEntrypoint(String... entrypoint) { return setEntrypoint(Arrays.asList(entrypoint)); }
JibContainer jibContainer = jibContainerBuilder.containerize(containerizer);
/** * Sets the layers. This replaces any previously-added layers. * * @param layerConfigurations the {@link LayerConfiguration}s * @return this */ public JibContainerBuilder setLayers(LayerConfiguration... layerConfigurations) { return setLayers(Arrays.asList(layerConfigurations)); }
/** * Sets the ports to expose from the container. This replaces any previously-set exposed ports. * * @param ports the ports to expose * @return this * @see #setExposedPorts(Set) */ public JibContainerBuilder setExposedPorts(Port... ports) { return setExposedPorts(new HashSet<>(Arrays.asList(ports))); }
/** * Sets the container entrypoint program arguments. * * @param programArguments program arguments tokens * @return this * @see #setProgramArguments(List) */ public JibContainerBuilder setProgramArguments(String... programArguments) { return setProgramArguments(Arrays.asList(programArguments)); }
@VisibleForTesting JibContainer containerize( Containerizer containerizer, Supplier<ExecutorService> defaultExecutorServiceFactory) throws InterruptedException, ExecutionException, IOException, CacheDirectoryCreationException { boolean shutdownExecutorService = !containerizer.getExecutorService().isPresent(); ExecutorService executorService = containerizer.getExecutorService().orElseGet(defaultExecutorServiceFactory); BuildConfiguration buildConfiguration = toBuildConfiguration(containerizer, executorService); try { BuildResult result = containerizer.getTargetImage().toBuildSteps(buildConfiguration).run(); return new JibContainer(result.getImageDigest(), result.getImageId()); } finally { if (shutdownExecutorService) { executorService.shutdown(); } } }