在 intellij 中使用 sudo 运行 java 测试

thm*_*y95 6 java intellij-idea docker testcontainers

语境

我刚刚找到了 testContainers,这是一个可以启动 docker 容器的库,我想在 Intellij 中编写一个简单的测试来测试它。

问题

问题是,当我在 Intellij 中运行配置时,代码会抛出异常,因为运行 docker 指令需要 sudo 权限。

23:18:52.180 [main] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - EnvironmentAndSystemPropertyClientProviderStrategy: failed with exception InvalidConfigurationException (ping failed)
23:18:52.186 [main] DEBUG org.testcontainers.dockerclient.DockerClientProviderStrategy - UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (ping failed). Root cause NoSuchFileException (/var/run/docker.sock)
23:18:52.187 [main] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy - Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
23:18:52.187 [main] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy -     EnvironmentAndSystemPropertyClientProviderStrategy: failed with exception InvalidConfigurationException (ping failed)
23:18:52.187 [main] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy -     EnvironmentAndSystemPropertyClientProviderStrategy: failed with exception InvalidConfigurationException (ping failed)
23:18:52.187 [main] ERROR org.testcontainers.dockerclient.DockerClientProviderStrategy -     UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (ping failed). Root cause NoSuchFileException (/var/run/docker.sock)
Run Code Online (Sandbox Code Playgroud)

尝试将当前用户添加到 docker 组, sudo usermod -aG docker $USER 即使我使用 sudo 启动它,我仍然无法在 Intellij 中运行该应用程序。

工作替代方案

sudo mvn test直接在我有pom的地方使用。

问题:

我可以让它在 Intellij 中工作吗?