private void createDockerfileFromTemplate(Archive<?> archive, File location) throws FileNotFoundException { File templateDockerfile = new File(location, DOCKERFILE_TEMPLATE); String deployableFilename = archive.getName(); Map<String, String> values = new HashMap<String, String>(); values.put("deployableFilename", deployableFilename); String templateContent = IOUtil.asStringPreservingNewLines(new FileInputStream(templateDockerfile)); //But because deployable file is created by shrinkwrap we need to replace the deploy file name to the one created. String dockerfileContent = IOUtil.replacePlaceholders(templateContent, values); File dockerfile = new File(location, "Dockerfile"); if (dockerfile.exists()) { log.fine( "Dockerfile file is already found in current build directory and is going to be renamed to Dockerfile.old."); dockerfile.renameTo(new File(location, "Dockerfile.old")); dockerfile = new File(location, "Dockerfile"); } dockerfile.deleteOnExit(); //The content is written to real Dockerfile which will be used during built time. IOUtil.toFile(dockerfileContent, dockerfile); File deployableOutputFile = new File(location, deployableFilename); deployableOutputFile.deleteOnExit(); //file is saved to Dockerfile directory so can be copied inside image. if (archive instanceof GenericArchive) { //In case of generic archives a tgz exporter should be used so Docker can uncompress it automatically. archive.as(TarExporter.class).exportTo(deployableOutputFile, true); } else { archive.as(ZipExporter.class).exportTo(deployableOutputFile, true); } }
public static Payload tarredDockerfile() throws IOException { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ShrinkWrap.create(GenericArchive.class, "archive.tar") .add(new ClassLoaderAsset("Dockerfile"), "Dockerfile") .as(TarExporter.class).exportTo(bytes); return Payloads.newByteArrayPayload(bytes.toByteArray()); } }