Testcontainers-java: Can not set custom container name

Created on 11 Dec 2016  路  16Comments  路  Source: testcontainers/testcontainers-java

Tried to set container name via .setContainerName(), however upon start container still has some random name assigned to it automatically. Did anyone test this? Works from command line with docker run -d --name bla my/image

typquestion

Most helpful comment

Dont mind. I found a nice workaround:

            .withCreateContainerCmdModifier(new Consumer<CreateContainerCmd>() {
                @Override
                public void accept(CreateContainerCmd createContainerCmd) {
                    createContainerCmd.withHostName(CNAME);
                    createContainerCmd.withName(CNAME);
                }
            })

All 16 comments

Managed to rename container after it got started with this snippet:

DockerClientFactory.instance().client().renameContainerCmd(oldName).withName(newName).exec();

Why setContainerName() does not do the same?

Where did you find setContainerName? :D it's not a part of TestContainers API, so I assume you were using, I don't know, docker-java directly?

maybe it got removed later? I can see it in 1.1.7 http://share.pho.to/AahDI

Eugh: I bet this is Lombok exposing a setter for what was originally intended to be a read-only field :(
No wonder it doesn't work.

We'll probably need to put AccessLevel.NONE on a couple of the internal fields like this one.

Also, we should probably think about explicitly letting people set the container name via a withContainerName(...) fluent setter. My main concern, though, would be the potential for name collisions. This might be something we want to steer ordinary users away from...

IMHO there is always a way to screw things up when configuring containers and hiding existing docker functionality isn't gonna make testcontainers more valuable. Also it would make sense adding container labelling.

I have to add that I find it very valuable to have the option to set the name using 'withContainerName' on GenericContainer. It seems to be a very small codebase addition to support it.

Any chance this will be implemented? I have a very frustrating Jenkins Setup here on my hands. The StartUp checks (Wait.forHostPort()) doesnt work because the mapped port of the fresh started container is not reachable. The only think that would work is an access to $containerName:$internalPort. But it only works when the container was started with "-- name $NAME". Using the random generated name or the containerId oder the containerIP doesnt work. I think its because the strange Jenkins VM Network settings here. No chance to get around this.

Dont mind. I found a nice workaround:

            .withCreateContainerCmdModifier(new Consumer<CreateContainerCmd>() {
                @Override
                public void accept(CreateContainerCmd createContainerCmd) {
                    createContainerCmd.withHostName(CNAME);
                    createContainerCmd.withName(CNAME);
                }
            })

I'd like to see this too, the use case for me is being able to set the names so they match config that works because when the containers are started on the same network I can do DNS based service discovery. Would like this ability on the Postgres container as well as the Generic if that's possible.

Thanks for the workaround @HaVonTe1 I'll try that for now

@RobMaskell if you use Networks you can (and should) use withNetworkAliases to provide a DNS name for your container, see:
https://github.com/testcontainers/testcontainers-java/blob/4471878f332dd8e7880fc8a55879a45834556e5e/core/src/test/java/org/testcontainers/containers/NetworkTest.java#L23

Thanks @bsideup will try quick question, how can I .withNetworkAliases() on a PostgreSQLContainer like so

@ClassRule
public static PostgreSQLContainer postgres = new PostgreSQLContainer("postgres:alpine")
        .withDatabaseName("postgres")
        .withUsername("postgres")
        .withPassword("password")
        .withNetworkAliases("db");

because doing this I get an incompatible type error in intellij

@RobMaskell This has to do with the usage of Generics in Testcontainers. It will work if you would use GenericContainer as a Type on the left side.

However, what exactly are you trying to achieve? You haven't added a network in this case, other containers will only be able to access this container using the alias if they are inside the same network. Or are you trying to access the container from your host? In this case, you should rely on the dnymaic port binding and use getMappedPorts() or getJdbcUrl() in this case.

@kiview thanks, I saw the network was needed in the examples so I'll add that, and now I know the options I should be able to make it work. Not trying to access the container from the host, this is container to container, I need to start up two (db, api) to be able to test a third from the host.

I'm initializing cluster of 3 etcd nodes, where I should link them during start command and this .withNetworkAliases("foo") works perfectly for me, thanks!

Given the presence of withCreateContainerCmdModifier I will close this issue

The documentation of withCreateContainerCmdModifier says:

Warning: this does expose the underlying docker-java API so might change outside of our control.

To me, this seems like a strong indication for extending the interface of GenericContainer.

Was this page helpful?
0 / 5 - 0 ratings