Java 如何使用docker映像设置嵌入式MongoDB

Java 如何使用docker映像设置嵌入式MongoDB,java,spring,mongodb,spring-boot,docker,Java,Spring,Mongodb,Spring Boot,Docker,我有以下几行代码用于设置带有docker映像的嵌入式mongodb @Container public static GenericContainer MONGO_CONTAINER = new GenericContainer<>("URL_OF_DOCKER") .withExposedPorts(27017); static class Initializer implements ApplicationContextIniti

我有以下几行代码用于设置带有docker映像的嵌入式mongodb

    @Container
    public static GenericContainer MONGO_CONTAINER = new GenericContainer<>("URL_OF_DOCKER")
            .withExposedPorts(27017);

    static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            final String propertyOverride = format("mongo.servers: %s:%s", MONGO_CONTAINER.getContainerIpAddress(), MONGO_CONTAINER.getMappedPort(27017));
            TestPropertyValues.of(propertyOverride).applyTo(configurableApplicationContext.getEnvironment());
        }
    }

有人知道这个问题吗?

您能从命令行客户端而不是Java代码访问MongoDB吗?如何开始集装箱化MongoDB?端口访问可能有问题。嵌入式mongo作为docker。。。这是相当矛盾的。嵌入式类似于嵌入式mongoDB的活板门。您正在docker中运行MongoDB实例。建议您使用
MONGO_CONTAINER.getHost()
,而不是
getContainerIpAddress
。刚刚在类上方找到了所需的TestContainer和ContextConfiguration注释
Caused by: org.springframework.dao.DataAccessResourceFailureException: Timed out after 2000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 2000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]