不支持 Docker-Compose 版本
Posted
技术标签:
【中文标题】不支持 Docker-Compose 版本【英文标题】:Docker-Compose version is unsupported 【发布时间】:2020-02-13 12:59:54 【问题描述】:我正在使用 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,这是一个比我当前运行的版本更旧的版本。我曾尝试在我的 docker-compose 中更改版本,但这似乎不起作用。我查看了其他具有相同错误的问题,但他们的解决方案均无效。我觉得 TestContainer 库使用的 docker-compose 比我旧版本,但如果这是问题,那么我不知道如何解决它。
【问题讨论】:
【参考方案1】:我相信你想要本地撰写模式:
compose =
new DockerComposeContainer(
new File("src/test/resources/docker-compose.yaml")).withLocalCompose(true)
更多详情请参阅local compose mode 文档:
您可以覆盖 Testcontainers 的默认行为并使其使用 安装在本地机器上的 docker-compose 二进制文件。这会 通常会产生更接近于运行 docker-compose 的体验 在本地,需要注意 Docker Compose 需要存在 开发和 CI 机器。
【讨论】:
谢谢,这是正确的方向,但是现在我收到错误Caused by: org.testcontainers.containers.ContainerLaunchException: Local Docker Compose exited abnormally with code 1 whilst running command: up -d
。有什么想法吗?
确保撰写文件在 TetsContainers 之外运行时正常工作。【参考方案2】:
这是我最终采用的方法:
我使用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();
【讨论】:
以上是关于不支持 Docker-Compose 版本的主要内容,如果未能解决你的问题,请参考以下文章
无法从 docker-compose 启动 postgres docker 容器