未找到Docker客户端策略

nbysray5  于 2023-08-03  发布在  Docker
关注(0)|答案(2)|浏览(124)

我正在尝试在NestJS应用程序中实现E2E测试。我在Docker容器中使用Postgres。对于其他测试,它运行良好。唯一的问题是E2E测试。我得到以下错误:

No Docker client strategy found

      at node_modules/testcontainers/dist/docker/docker-client.js:46:11
      at fulfilled (node_modules/testcontainers/dist/docker/docker-client.js:5:58)

  ● Test suite failed to run

    TypeError: Cannot read properties of undefined (reading 'close')

      59 |
      60 |   afterAll(async () => {
    > 61 |     await app.close();
         |               ^
      62 |     await container.stop();
      63 |   });
      64 |

      at Object.<anonymous> (test/event.e2e-spec.ts:61:15)

字符串
我的代码如下:

describe('EventAPI (e2e)', () => {
  jest.setTimeout(180_000);
  let app: INestApplication;
  let service: Service;
  let container: E2EPgContainer;

  beforeAll(async () => {
    container = new E2EPgContainer();
    await container.init();

    // eslint-disable-next-line @typescript-eslint/no-var-requires
    const AppModule = require('../src/server/app/app.module').AppModule;
    const module: TestingModule = await Test.createTestingModule({
      imports: [AppModule],
    }).compile();

    app = module.createNestApplication();
    app.useGlobalPipes(new ValidationPipe({ transform: true }));
    service = module.get<Service>(Service);
    await app.init();
  });
  afterEach(() => {
    jest.clearAllMocks();
  });

  afterAll(async () => {
    await app.close();
    await container.stop();
  });

  describe('GET-POINT API', () => {
     it('GET /:customerId --> should return success 200 after get points', async () => {
        // test code
     });
  });
});


如何解决这个问题?

ztmd8pv5

ztmd8pv51#

我假设你的docker容器正在运行,但不是无根的。请确保您正在以无根方式运行Docker。您可以尝试以下操作:

$ sudo groupadd docker
$ sudo gpasswd -a $USER docker

字符串
现在确保设置了以下环境变量(或将它们添加到~/.bashrc

export PATH=/home/ubuntu/bin:$PATH
export DOCKER_HOST=unix:///run/user/1001/docker.sock


现在尝试重新启动docker

$ sudo service docker restart


完成所有步骤后,尝试在终端中执行docker命令,而不使用sudo:

$ docker ps


如果你能够看到你的docker容器,那么它将解决你的问题。

7fhtutme

7fhtutme2#

在将Docker升级到4.21.1版本后,我在Mac上遇到了同样的问题,我通过创建此问题中建议的符号链接进行了修复

sudo ln -sf "$HOME/.docker/run/docker.sock" /var/run/docker.sock

字符串

相关问题