使用docker-java管理docker,卷映射似乎不起作用

问题描述 投票:0回答:0

环境: 软呢帽 39 openjdk-17 码头工人25.01

我的目的是运行 ffmpeg 将多个视频文件合并到一个文件中,我选择使用 docker(由 com.github.docker-java:docker-java:3.3.4 管理)来做到这一点。

但是我遇到了一个问题:我声明了两个卷映射并将它们绑定到容器,然后立即启动容器,容器中似乎不存在映射目录。

下面是一个 springboot 测试用例:

@Test
    public void testDockerVolumeMapping() throws Exception {
        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost("unix:///var/run/docker.sock")
                .build();
        DockerClient dockerClient = DockerClientBuilder.getInstance(config)
                .withDockerHttpClient(new ApacheDockerHttpClient.Builder().dockerHost(config.getDockerHost()).build())
                .build();
        String imageTag = "jrottenberg/ffmpeg:4.1-ubuntu";
        dockerClient.pullImageCmd(imageTag).exec(new PullImageResultCallback()).awaitCompletion(5, TimeUnit.MINUTES);
        Volume inputVM = new Volume("/home/rick/temp/video_demo/input:/video_input");
        Volume outputVM = new Volume("/home/rick/temp/video_demo/output:/video_output");
        String containerId = dockerClient.createContainerCmd(imageTag)
                .withVolumes(inputVM, outputVM)
                .withEntrypoint("/bin/bash")
                .withCmd("-c", "ls /video_output")
                .withAttachStdout(true)
                .withAttachStderr(true)
                .exec().getId();
        dockerClient.startContainerCmd(containerId).exec();
    }

运行docker命令:

docker logs ${containerId}

出现错误消息:“ls:无法访问'/video_output':没有这样的文件或目录”

使用命令再次启动容器:

docker start ${containerId}
docker logs ${containerId}

又多了一条错误信息,内容和上一条一样。

但是当我使用 docker run 命令时:

docker run -v /home/rick/temp/video_demo/input:/video_input -v /home/rick/temp/video_demo/output:/video_output --entrypoint "/bin/bash" jrottenberg/ffmpeg:4.1-ubuntu -c "ls /video_output"

目录内容显示正确。

原因是什么?

java docker ffmpeg docker-java
© www.soinside.com 2019 - 2024. All rights reserved.