问题
I am using test containers to run some tests and using https://www.testcontainers.org/modules/docker_compose/ to spin some containers used for my tests. Similar to generic containers support, it's also possible to run a bespoke set of services specified in a docker-compose.yml file.
This is intended to be useful on projects where Docker Compose is already used in dev or other environments to define services that an application may be dependent upon.
Here is my code to include docker-compose.yml
@ClassRule
public static DockerComposeContainer environment =
new DockerComposeContainer(new File("src/test/resources/docker-compose.yml"));
Here is the content of my yml file:
version: '2.0'
services:
vault:
image: vault:0.10.2
container_name: vault
ports:
- "8200:8200"
environment:
- VAULT_ADDR=http://0.0.0.0:8200
- VAULT_DEV_LISTEN_ADDRESS=0.0.0.0:8200
- VAULT_DEV_ROOT_TOKEN_ID=stupid-token
volumes:
- /config/vault/start.sh:/start.sh
command: sh -c "./start.sh"
consul:
image: consul:0.8.5
container_name: consul
ports:
- "8300:8300"
- "8400:8400"
- "8500:8500"
- "8600:8600"
- "8600:8600/udp"
environment:
- SERVICE_IGNORE=true
entrypoint: sh -c "BIND_HOST=$$(echo \"$DOCKER_HOST\" | awk -F ':' '{print $$2}' | sed 's/\\/\\///'); echo $$BIND_HOST;/bin/consul agent -server -bootstrap --client=0.0.0.0 --data-dir=/tmp/consul -ui -advertise=$$BIND_HOST -domain=consul-dev"
registrator:
image: gliderlabs/registrator:v7
container_name: registrator
depends_on:
- consul
volumes:
- /var/run/docker.sock:/tmp/docker.sock:ro
entrypoint: sh -c "BIND_HOST=$$(echo \"$DOCKER_HOST\" | awk -F ':' '{print $$2}' | sed 's/\\/\\///'); echo $$BIND_HOST;/bin/registrator -ip $$BIND_HOST consul://consul:8500"
postgres:
image: registry.xxx.com/xxx-postgres:9.5.9
container_name: postgres
ports:
- "5432:5432"
depends_on:
- registrator
volumes:
- /config/postgres/docker-entrypoint.sh:/docker-entrypoint.sh:ro
# - postgres-data:/var/lib/postgresql/data
environment:
- SERVICE_NAME=postgres
command: -c max_connections=4000
test-dbmigrate:
image: registry.xxx.com/test-db:master
container_name: test-dbmigrate
depends_on:
- postgres
environment:
- DEV_LOGGING=1
- DB_INIT=1
- DB_HOST=postgres
- DB_PORT=5432
- DB_USER=test
- DB_PASSWORD=xxx
- PGPASSWORD=xxx
#volumes:
# postgres-data:
I am getting this error {
"timestamp": "2019-06-11T21:08:32.320Z",
"level": "ERROR",
"thread": "Test worker",
"logger": "🐳 [docker/compose:1.8.0]",
"message": "Could not start container",
"context": "default",
"exception": "java.lang.IllegalStateException: Container did not start correctly.\n\tat org.testcontainers.containers.GenericContainer.tryStart(GenericContainer.java:255)\n\tat org.testcontainers.containers.GenericContainer.lambda$doStart$0(GenericContainer.java:212)\n\tat org.rnorth.ducttape.unreliables.Unreliables.retryUntilSuccess(Unreliables.java:76)\n\tat org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:210)\n\tat org.testcontainers.containers.GenericContainer.start(GenericContainer.java:199)\n\tat org.testcontainers.containers.ContainerisedDockerCompose.invoke(DockerComposeContainer.java:548)\n\tat org.testcontainers.containers.DockerComposeContainer.runWithCompose(DockerComposeContainer.java:211)\n\tat org.testcontainers.containers.DockerComposeContainer.createServices(DockerComposeContainer.java:165)\n\tat org.testcontainers.containers.DockerComposeContainer.start(DockerComposeContainer.java:152)\n\tat org.testcontainers.containers.DockerComposeContainer.starting(DockerComposeContainer.java:121)\n\tat org.testcontainers.containers.FailureDetectingExternalResource$1.evaluate(FailureDetectingExternalResource.java:29)\n\tat org.junit.rules.RunRules.evaluate(RunRules.java:20)\n\tat org.junit.runners.ParentRunner.run(ParentRunner.java:363)\n\tat org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)\n\tat org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.runTestClass(JUnitTestClassExecutor.java:106)\n\tat org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:58)\n\tat org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecutor.execute(JUnitTestClassExecutor.java:38)\n\tat org.gradle.api.internal.tasks.testing.junit.AbstractJUnitTestClassProcessor.processTestClass(AbstractJUnitTestClassProcessor.java:66)\n\tat org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:498)\n\tat org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)\n\tat org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)\n\tat org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)\n\tat org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)\n\tat com.sun.proxy.$Proxy2.processTestClass(Unknown Source)\n\tat org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:117)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:498)\n\tat org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)\n\tat org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)\n\tat org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:155)\n\tat org.gradle.internal.remote.internal.hub.MessageHubBackedObjectConnection$DispatchWrapper.dispatch(MessageHubBackedObjectConnection.java:137)\n\tat org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:404)\n\tat org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:63)\n\tat org.gradle.internal.concurrent.ManagedExecutorImpl$1.run(ManagedExecutorImpl.java:46)\n\tat java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)\n\tat java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)\n\tat org.gradle.internal.concurrent.ThreadFactoryImpl$ManagedThreadRunnable.run(ThreadFactoryImpl.java:55)\n\tat java.lang.Thread.run(Thread.java:748)\n"
}
{
"timestamp": "2019-06-11T21:08:32.348Z",
"level": "ERROR",
"thread": "Test worker",
"logger": "🐳 [docker/compose:1.8.0]",
"message": "Log output from the failed container:\nCreating network \"k66igshwvupa_default\" with the default driver\n\nCreating consul\n\nCreating vault\n\nCreating registrator\n\nCreating postgres\n\n\n\nERROR: for postgres Cannot start service postgres: oci runtime error: container_linux.go:247: starting container process caused \"process_linux.go:359: container init caused \\\"rootfs_linux.go:54: mounting \\\\\\\"/config/postgres/docker-entrypoint.sh\\\\\\\" to rootfs \\\\\\\"/mnt/sda1/var/lib/docker/aufs/mnt/df4d5e2dae646eab8079cee1b8a562c1b7d2d374340d01fecf4894e6ba64be8e\\\\\\\" at \\\\\\\"/mnt/sda1/var/lib/docker/aufs/mnt/df4d5e2dae646eab8079cee1b8a562c1b7d2d374340d01fecf4894e6ba64be8e/usr/local/bin/docker-entrypoint.sh\\\\\\\" caused \\\\\\\"not a directory\\\\\\\"\\\"\"\n\n: Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type\n\nEncountered errors while bringing up the project.\n",
"context": "default"
}
回答1:
It worked when I gave a relative path:
volumes:
- ./docker-entrypoint.sh:/docker-entrypoint.sh
来源:https://stackoverflow.com/questions/56551917/how-fix-mounting-issue-while-using-docker-compose-yml-on-test-containers