Sasha Shpota
Sasha Shpota

Reputation: 10300

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

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?

Upvotes: 7

Views: 2475

Answers (1)

Ermiya Eskandary
Ermiya Eskandary

Reputation: 23602

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.

Upvotes: 4

Related Questions