diff --git a/src/main/asciidoc/inc/image/_naming.adoc b/src/main/asciidoc/inc/image/_naming.adoc index f6e6bba01..5f508b0b8 100644 --- a/src/main/asciidoc/inc/image/_naming.adoc +++ b/src/main/asciidoc/inc/image/_naming.adoc @@ -50,6 +50,9 @@ When specifying the container name pattern the following placeholders can be use | *%i* | An index which is incremented if a container has already been created. With this parameter it is easily possible to have multiple, similar containers. See the example below for more details. +| *%r* +| A random container name choosen by the docker engine. + |=== You can combine the placeholders in any combination and will be resolved during `docker:start`, `docker:stop` and `docker:watch`. diff --git a/src/main/java/io/fabric8/maven/docker/util/ContainerNamingUtil.java b/src/main/java/io/fabric8/maven/docker/util/ContainerNamingUtil.java index d8e0c90c0..301efe0f2 100644 --- a/src/main/java/io/fabric8/maven/docker/util/ContainerNamingUtil.java +++ b/src/main/java/io/fabric8/maven/docker/util/ContainerNamingUtil.java @@ -35,15 +35,15 @@ public static String formatContainerName(final ImageConfiguration image, String containerNamePattern = extractContainerNamePattern(image, defaultContainerNamePattern); Set existingContainersNames = extractContainerNames(existingContainers); - final String partiallyApplied = - replacePlaceholders( - containerNamePattern, - image.getName(), - image.getAlias(), - buildTimestamp); - - - if (partiallyApplied.contains(INDEX_PLACEHOLDER)) { + // don't replace if we want a random container name + String partiallyApplied = containerNamePattern == null ? null : + replacePlaceholders( + containerNamePattern, + image.getName(), + image.getAlias(), + buildTimestamp); + + if (partiallyApplied != null && partiallyApplied.contains(INDEX_PLACEHOLDER)) { for (long i = 1; i < Long.MAX_VALUE; i++) { final String withIndexApplied = partiallyApplied.replaceAll(INDEX_PLACEHOLDER, String.valueOf(i)); if (!existingContainersNames.contains(withIndexApplied)) { @@ -143,7 +143,7 @@ private static String extractContainerNamePattern(ImageConfiguration image, Stri RunImageConfiguration runConfig = image.getRunConfiguration(); if (runConfig != null) { if (runConfig.getContainerNamePattern() != null) { - return runConfig.getContainerNamePattern(); + return getContainerNamingPattern(runConfig.getContainerNamePattern()); } if (runConfig.getNamingStrategy() == RunImageConfiguration.NamingStrategy.alias) { return "%a"; @@ -151,8 +151,19 @@ private static String extractContainerNamePattern(ImageConfiguration image, Stri } return defaultContainerNamePattern != null ? defaultContainerNamePattern : DEFAULT_CONTAINER_NAME_PATTERN; } - + private static String cleanImageName(final String imageName) { return new ImageName(imageName).getSimpleName().replaceAll("[^a-zA-Z0-9_.-]+", "_"); } + + private static String getContainerNamingPattern(String pattern) { + if (pattern.contains("%r")) { + if (pattern.length() > 2) { + throw new IllegalArgumentException("Invalid use of container naming pattern '%r'"); + } + return null; + } + + return pattern; + } } diff --git a/src/test/java/io/fabric8/maven/docker/util/ContainerNamingUtilTest.java b/src/test/java/io/fabric8/maven/docker/util/ContainerNamingUtilTest.java index 20eab4b8e..65d664ead 100644 --- a/src/test/java/io/fabric8/maven/docker/util/ContainerNamingUtilTest.java +++ b/src/test/java/io/fabric8/maven/docker/util/ContainerNamingUtilTest.java @@ -40,6 +40,16 @@ public void testAlias() { Collections.emptySet())); } + @Test + public void testRandom() { + Assert.assertEquals(null, + ContainerNamingUtil.formatContainerName( + imageConfiguration("jolokia/jolokia_demo","nameAlias", "%r"), + null, + new Date(123456), + Collections.emptySet())); + } + @Test public void testTimestamp() { Assert.assertEquals("123456",