Docker-不支持撰写版本

Docker-Compose version is unsupported

我正在使用 TestContainers 来 运行 dgraph。

这是我的测试代码:

package net.dgraph.java.client

import io.dgraph.DgraphAsyncClient
import io.dgraph.DgraphClient
import org.testcontainers.containers.DockerComposeContainer
import org.testcontainers.containers.GenericContainer
import org.testcontainers.spock.Testcontainers
import spock.lang.Shared
import spock.lang.Specification

import java.time.Duration
import java.time.temporal.ChronoUnit

@Testcontainers
public class DGraphTest extends Specification {
private SyncSigmaDgraphClient syncClient
private AsyncSigmaDGraphClient asyncClient
private static address
static DockerComposeContainer compose

def setup() {
   syncClient  = SigmaDgraphClientBuilder
            .create()
            .withHost(address)
            .withPort(port1)
            .buildSync()
}
static {
    compose =
            new DockerComposeContainer(
                    new File("src/test/resources/docker-compose.yaml"))
    compose.start()
    this.address = compose.getServiceHost("dgraph", 8080)
    this.port1 = compose.getServicePort("dgraph",8080)
}

我的 docker-compose.yaml 文件看起来像:

version: "3.2"
services:
  zero:
    image: dgraph/dgraph:latest
    volumes:
      - /tmp/data:/dgraph
    ports:
      - 5080:5080
      - 6080:6080
    restart: on-failure
    command: dgraph zero --my=zero:5080
  alpha:
    image: dgraph/dgraph:latest
    volumes:
      - /tmp/data:/dgraph
    ports:
      - 8080:8080
      - 9080:9080
    restart: on-failure
    command: dgraph alpha --my=alpha:7080 --lru_mb=2048 --zero=zero:5080
  ratel:
    image: dgraph/dgraph:latest
    ports:
      - 8000:8000
    command: dgraph-ratel

我的 docker 版本是 Docker version 19.03.2, build 6a30dfc,我的 docker-compose 版本是 docker-compose version 1.24.1, build 4667896b

但是我收到以下错误:

[main] ERROR  [docker/compose:1.8.0] - Log output from the failed container:
Version in "src/test/resources/docker-compose.yaml" is unsupported. You might be seeing this error because you're using the wrong Compose file version. Either specify a version of "2" (or "2.0") and place your service definitions under the `services` key, or omit the `version` key and place your service definitions at the root of the file to use version 1.

我觉得有趣的一部分是错误日志显示 docker/compose:1.8.0,它比我目前 运行ning 的版本旧。我曾尝试在我的 docker-compose 中更改版本,但这似乎不起作用。我查看了具有相同错误的其他问题,并且 none 他们的解决方案有效。我觉得 TestContainer 库使用的 docker-compose 版本比我使用的要旧,但如果这是问题所在,那么我不知道如何解决它。

我相信你想要本地撰写模式:

compose =
        new DockerComposeContainer(
                new File("src/test/resources/docker-compose.yaml")).withLocalCompose(true)

有关详细信息,请参阅 local compose mode 文档:

You can override Testcontainers' default behaviour and make it use a docker-compose binary installed on the local machine. This will generally yield an experience that is closer to running docker-compose locally, with the caveat that Docker Compose needs to be present on dev and CI machines.

这是我最终采用的方法:

我使用 Network.newNetwork() 将零和 alpha 实例联系在一起。我使用调试和 docker logs 查看 dgraph 零需要等待的消息才能成功启动。

 static {
    Network network = Network.newNetwork()
    dgraph_zero = new GenericContainer<>("dgraph/dgraph")
            .withExposedPorts(5080)
            .withNetworkAliases("zero")
            .withStartupTimeout(Duration.of(1, ChronoUnit.MINUTES))
            .withCommand("dgraph zero --my=zero:5080")
            .withNetwork(network)
            .waitingFor(Wait.forLogMessage('.* Updated Lease id: 1.*\n',1))
    dgraph_zero.start()

  dgraph_alpha = new GenericContainer<>("dgraph/dgraph")
        .withExposedPorts(9080)
        .withStartupTimeout(Duration.of(1, ChronoUnit.MINUTES))
        .withNetworkAliases("alpha")
        .withCommand("dgraph alpha --my=alpha:7080 --lru_mb=2048 --zero=zero:5080")
        .withNetwork(network)
        .waitingFor(Wait.forLogMessage(".*Server is ready.*\n",1))
    dgraph_alpha.start()

    this.address = dgraph_alpha.containerIpAddress
    this.port1 = dgraph_alpha.getMappedPort(9080)

    ManagedChannel channel = ManagedChannelBuilder
            .forAddress(address,port1)
            .usePlaintext()
            .build();
    DgraphGrpc.DgraphStub stub = DgraphGrpc.newStub(channel);
    this.dgraphclient = new DgraphClient(stub) ;
    Transaction txn = this.dgraphclient.newTransaction();