通用容器不会选取文件进行处理



GenericContainer 在测试容器之外运行时不处理文件 工作正常

似乎容器以某种方式受到限制,资源不足或以某种方式被阻止,或者文件监视在绑定时行为不正确。

public class SimpleIntegrationTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(SimpleIntegrationTest.class);
    @Rule
    public GenericContainer container = new GenericContainer<>(
            new ImageFromDockerfile()
                    .withDockerfileFromBuilder(builder ->
                            builder
                                    .from("ourproduct:latest")
                                    .workDir("/opt/ourproduct")
                                    .entryPoint("./Scripts/start.sh")
                                    .build()))
            .withExposedPorts(8080)
            .withFileSystemBind("/home/greg/share", "/share", BindMode.READ_WRITE)
            .withCreateContainerCmdModifier(cmd -> cmd.withHostName("somehost.com"))
            .waitingFor(Wait.forLogMessage(".*Ourproduct is Up.*\n", 1).withStartupTimeout(Duration.ofSeconds(60)));
    @Test
    public void simpleExchangeTest() throws IOException, InterruptedException {
        LOGGER.info("Starting simple exchange test...");
        // copy input file
        InputStream request = ClassLoader.getSystemResourceAsStream("message.txt");
        File target = new File("/home/greg/share/input/message.txt");
        FileUtils.copyToFile(request, target);
        FileUtils.touch(target);
        // watch for response
        Path path = Paths.get("/home/greg/share/output");
        WatchService watchService = path.getFileSystem().newWatchService();
        path.register(watchService, StandardWatchEventKinds.ENTRY_CREATE);
        LOGGER.info("Waiting up to [{}] seconds for response file...", 30);
        WatchKey watchKey = watchService.poll(30, TimeUnit.SECONDS);
        if (watchKey != null) {
            watchKey.pollEvents().stream().forEach(event -> LOGGER.info(event.context().toString()));
        }
        LOGGER.info("Container logs...");
        LOGGER.info(container.getLogs());
    }
}

显然,我期待/home/greg/share/output的回应,但它永远不会到来。

当我这样做时工作得很好:

  1. docker run -itd --name cont --hostname somehost.com -p 8080:8080 --mount type=bind,source=/home/greg/share,target=/share ourproduct:latest
  2. docker exec -it cont bash

在容器中

  1. cd /opt/ourproduct
  2. ./Scripts/start.sh

主机上的外部容器

  1. cp message.txt /home/greg/share/input/

几秒钟后,我有home/greg/share/output响应TestContainers的情况并非如此...

编辑:当我添加测试时:

Container.ExecResult execResult = container.execInContainer("./Scripts/status.sh");

我得到:

com.github.dockerjava.api.exception.ConflictException: {"message":"Container aac697315e3e22ccee4cdf805e6b1b325663bae054ab1104021c4da724cb4a5a is not running"}

知道出了什么问题以及为什么它没有运行吗?

添加尾

巴解决了问题
.entryPoint("./Scripts/start.sh && tail -f /dev/null")

最新更新