AWS CodeBuild: How to run tests that require starting Docker?
Asked Answered
P

1

7

I have a Node.js project with Testcontainers used for staring Redis and Prostgress for tests.

I want to run these tests as a part of CI. For that purpose, I use an alpine image of Node.js with Docker installed on top of it. I can run tests that don't require Docker there, but if it involves Docker, I get this error from Testcontainers:

No Docker client strategy found

Here is what I get in logs:

2022-05-25T13:09:11.494Z testcontainers DEBUG Found applicable Docker client strategy: UnixSocketStrategy
2022-05-25T13:09:11.531Z testcontainers DEBUG Testing Docker client strategy URI: unix:///var/run/docker.sock
2022-05-25T13:09:11.539Z testcontainers DEBUG No registry auth locator found for registry: "https://index.docker.io/v1/"
2022-05-25T13:09:11.543Z testcontainers WARN  Docker daemon is not reachable: Error: connect ENOENT /var/run/docker.sock
2022-05-25T13:09:11.543Z testcontainers WARN  Docker client strategy UnixSocketStrategy is not reachable
2022-05-25T13:09:11.544Z testcontainers ERROR Failed to list images: Error: No Docker client strategy found
2022-05-25T13:09:11.544Z testcontainers ERROR Failed to pull image "postgres:14.2-alpine": Error: No Docker client strategy found

The tests run fine when I start them locally.

I haven't found anything in the CodeBuild documentation explaining connecting to the Docker Engine from inside a builder container. I found these instructions in the Testcontainers documentation, but I don't understand how to apply this in AWS Codebuild.

Q: How to run tests that require starting Docker in AWS CodeBuild?

Premonition answered 17/5, 2022 at 17:1 Comment(5)
Can you make sure that Docker is available in your build environment, by running Docker CLI commands directly? Given this issue, I know that we have multiple Testcontainers users using AWS CodeBuild: github.com/testcontainers/testcontainers-java/issues/4594Halette
Thank you @KevinWittek. I can successfully run docker --version which would print Docker version 20.10.16, build aa7e414fdcb23a66e8fabbef0a560ef1769eace5. Do I need something else?Premonition
How are you running the tests? Can you please share your buildspec yaml?Capablanca
I just realize that you are using the Testcontainers Node version and not the Java one. It might be possible, that the Node version does not support the AWS CodeBuild Docker setup. The docs on testcontainers.org are tc-java specific. I would suggest raising an issue in the project: github.com/testcontainers/testcontainers-nodeHalette
@KevinWittek thank you, I opened an issue github.com/testcontainers/testcontainers-node/issues/369Premonition
C
4

WARN Docker daemon is not reachable: Error: connect ENOENT /var/run/docker.sock

CodeBuild environments by default:

  1. do not have access to interact with the Docker daemon, dockerd
  2. do not have the Docker daemon running

The error is hinting at this by stating that it cannot reach the daemon.

To address the above:

  1. Set your buildspec version to 0.2
  2. Run your build with privilegedMode set to true, to grant access to the daemon
  3. Ensure the daemon is running

The key point to note here is when using AWS CodeBuild provided official Docker images, the Docker daemon is automatically started via the dockerd-entrypoint.sh. You do not need to do this manually.

However, when using custom images, you must start the daemon manually.


You can manually initialise the Docker daemon during the install phase of your build by adding the below commands to your buildspec.yml file.

For an Alpine-based image:

version: 0.2

phases:
  install:
    commands:
      - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
      - timeout -t 15 sh -c "until docker info; do echo .; sleep 1; done"
...

For a Ubuntu-based image:

version: 0.2

phases:
  install:
    commands:
      - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://127.0.0.1:2375 --storage-driver=overlay2 &
      - timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
...

You should now be able to run Docker containers within your build.

If that still doesn't work, it will be related to Testcontainers.

As per the docs you have linked, ensure your docker run ... command which is running the container includes -v $PWD:$PWD -w $PWD -v /var/run/docker.sock:/var/run/docker.sock.

Capablanca answered 26/5, 2022 at 18:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.