diff --git a/src/main/asciidoc/inc/image/_naming.adoc b/src/main/asciidoc/inc/image/_naming.adoc index f6e6bba01..fc5afa331 100644 --- a/src/main/asciidoc/inc/image/_naming.adoc +++ b/src/main/asciidoc/inc/image/_naming.adoc @@ -41,15 +41,17 @@ When specifying the container name pattern the following placeholders can be use | *%a* | The `` of an image which must be set. The alias is set in the top-level image configuration +| *%e* +| Choose an empty container name, which will let the docker engine chose a random container name automatically. This placeholder must be given as single value to `containerNamePattern` when used. + | *%n* -| A sanitized version of the imag's short name from which this container is created. "Sanitized" means that any non letter, digit, dot or dash is replaced by an underscore. +| A sanitized version of the image's short name from which this container is created. "Sanitized" means that any non letter, digit, dot or dash is replaced by an underscore. | *%t* | The build time stamp. This is the timestamp which created during the building of an image and locally cached. A rebuild of the image will update the timestamp. | *%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. - |=== 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..5a6fc1e42 100644 --- a/src/main/java/io/fabric8/maven/docker/util/ContainerNamingUtil.java +++ b/src/main/java/io/fabric8/maven/docker/util/ContainerNamingUtil.java @@ -20,7 +20,8 @@ */ public class ContainerNamingUtil { - private static final String INDEX_PLACEHOLDER = "%i"; + static final String INDEX_PLACEHOLDER = "%i"; + static final String EMPTY_NAME_PLACEHOLDER = "%e"; public static final String DEFAULT_CONTAINER_NAME_PATTERN = "%n-%i"; @@ -35,13 +36,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 (shouldUseEmptyName(containerNamePattern)) { + return null; + } + String partiallyApplied = replacePlaceholders( + containerNamePattern, + image.getName(), + image.getAlias(), + buildTimestamp); if (partiallyApplied.contains(INDEX_PLACEHOLDER)) { for (long i = 1; i < Long.MAX_VALUE; i++) { @@ -70,20 +73,18 @@ public static Collection getContainersToStop(final ImageConfiguration String containerNamePattern = extractContainerNamePattern(image, defaultContainerNamePattern); - // Only special treatment for indexed container names - if (!containerNamePattern.contains(INDEX_PLACEHOLDER)) { + if (shouldUseEmptyName(containerNamePattern) || !containerNamePattern.contains(INDEX_PLACEHOLDER)) { return containers; } final String partiallyApplied = - replacePlaceholders( - containerNamePattern, - image.getName(), - image.getAlias(), - buildTimestamp); + replacePlaceholders( + containerNamePattern, + image.getName(), + image.getAlias(), + buildTimestamp); return keepOnlyLastIndexedContainer(containers, partiallyApplied); - } // ======================================================================================================== @@ -155,4 +156,12 @@ private static String extractContainerNamePattern(ImageConfiguration image, Stri private static String cleanImageName(final String imageName) { return new ImageName(imageName).getSimpleName().replaceAll("[^a-zA-Z0-9_.-]+", "_"); } + + private static boolean shouldUseEmptyName(String pattern) { + if (pattern.contains(EMPTY_NAME_PLACEHOLDER) && !pattern.equals(EMPTY_NAME_PLACEHOLDER)) { + throw new IllegalArgumentException("Invalid use of container naming pattern " + EMPTY_NAME_PLACEHOLDER); + } + return pattern.equals(EMPTY_NAME_PLACEHOLDER); + } + } 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..9c1479052 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,28 @@ public void testAlias() { Collections.emptySet())); } + @Test + public void testRandom() { + Assert.assertEquals(null, + ContainerNamingUtil.formatContainerName( + imageConfiguration("jolokia/jolokia_demo","nameAlias", ContainerNamingUtil.EMPTY_NAME_PLACEHOLDER), + null, + new Date(123456), + Collections.emptySet())); + } + + @Test + public void testMixedRandom() { + Assert.assertThrows(IllegalArgumentException.class, () -> { + Assert.assertEquals(null, + ContainerNamingUtil.formatContainerName( + imageConfiguration("jolokia/jolokia_demo","nameAlias", ContainerNamingUtil.EMPTY_NAME_PLACEHOLDER + "-extra"), + null, + new Date(123456), + Collections.emptySet())); + }); + } + @Test public void testTimestamp() { Assert.assertEquals("123456", @@ -74,7 +96,7 @@ public void testIndex() { public void testAll() { Assert.assertEquals("prefix-1-nameAlias-jolokia_demo-123456-postfix", ContainerNamingUtil.formatContainerName( - imageConfiguration("jolokia/jolokia_demo","nameAlias", "prefix-%i-%a-%n-%t-postfix"), + imageConfiguration("jolokia/jolokia_demo","nameAlias", "prefix-" + ContainerNamingUtil.INDEX_PLACEHOLDER + "-%a-%n-%t-postfix"), null, new Date(123456), Collections.emptySet()));