Wim Deblauwe
Wim Deblauwe

Reputation: 26878

Testcontainers with volume mounts on custom Azure DevOps build agent running in Docker

I am having trouble running some integration tests that use Testcontainers on a self-managed custom Azure DevOps agent that is already running in Docker.

The host is an Ubuntu 20.04 Virtual Machine on Azure. It has Docker installed and there are 2 docker images running, one for our Java-based builds and one for the Angular-based builds. Inside the Java-based one, we run Maven with Testcontainers.

The Docker image that is created for the build agent itself, is based on the documentation at https://learn.microsoft.com/en-us/azure/devops/pipelines/agents/docker?view=azure-devops#linux. It runs Ubuntu 18.04, has a WORKDIR /azp and ENTRYPOINT [ "./start.sh" ].

The build agent is started manually on the host via:

docker run -d --name build-agent-java-1 \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -e AZP_URL=https://dev.azure.com/my-organisation \
  -e AZP_TOKEN=<mytoken> \
  -e AZP_AGENT_NAME=agent-1 \ 
  custombuildagentcr.azurecr.io/ubuntu1804-java11

The -v /var/run/docker.sock:/var/run/docker.sock is added following the Testcontainers documentation

This works for some Testcontainers tests, but not for others. More specially, it does not work for a test where the container started by testcontainers needs access to the compiled class files (which are present on the build agent docker).

After searching through a lot of other questions, I believe the problem is that the "inner docker" cannot "see" the files on the "outer docker" (the build agent). See Docker volume mounts not working in Azure DevOps Pipeline and Selfhosted Azure DevOps Agents volume mapping

However, it is not clear to me how to apply this exactly.

I tried to add -v $PWD:$PWD -w $PWD when starting the build agent Docker (Because the Testcontainer documentation indicates this). However, this makes the image fail to start as ./start.sh as ENTRYPOINT no longer can be resolved as the WORKDIR is changed.

I also tried to change the ENTRYPOINT to use an absolute path /azp/start.sh (and also do a cd /azp inside that start.sh file), but that still does not work.

If I check the mounts that are available, it returns this:

devops-agent-host-user@shared-devops-agent-host:~$ docker inspect build-agent-java-1 | jq '.[0].Mounts'
[
  {
    "Type": "bind",
    "Source": "/home/devops-agent-host-user",
    "Destination": "/home/devops-agent-host-user",
    "Mode": "",
    "RW": true,
    "Propagation": "rprivate"
  },
  {
    "Type": "bind",
    "Source": "/var/run/docker.sock",
    "Destination": "/var/run/docker.sock",
    "Mode": "",
    "RW": true,
    "Propagation": "rprivate"
  }
]

Azure itself has Mounting volumes using Docker within a Docker container, but I unfortunately fail to understand how to apply this to my situation.

Upvotes: 1

Views: 1391

Answers (1)

Wim Deblauwe
Wim Deblauwe

Reputation: 26878

I don't have a generic solution, but the problem I had went away for me with testcontainers-keycloak 1.9.0 as it no longer uses a bind mount, but copies the class files into the container (See https://github.com/dasniko/testcontainers-keycloak/issues/44).

Upvotes: 0

Related Questions