Overview
Testcontainers 에서 Colima 를 사용하기 위해 필요한 설정 및 발생하는 Trouble Shooting들을 정리합니다.
실행환경
- macOS Ventura 13.1
- IntelliJ IDEA 2022.1.4 (Ultimate Edition)
- MySQL 8.0 Official Docker image
Configuration
1. .zshrc 나 .bash_profile에 아래 설정을 추가
export TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock
export DOCKER_HOST="unix://${HOME}/.colima/docker.sock"
export TESTCONTAINERS_RYUK_DISABLED=true # [Optional] MySql 컨테이너 로딩시 warn log 방지를 위해 추가
2. 테스트컨테이너를 사용하는 프로젝트 폴더로 이동, 아래 명령어를 실행하여 docker container가 잘 동작하는지 확인
$ ./gradlew clean -p {target-module} test --tests '*RepositoryTest'
Trouble Shooting
Q) 터미널이 아닌 IntelliJ 안에서 JUnit 테스트 시 Could not find a valid Docker environment. 발생
Run tests using 설정을 IntelliJ IDEA 로 설정했을 경우에 해당합니다.
2023-01-19 17:20:46.892 INFO 66213 --- [ main] o.t.utility.ImageNameSubstitutor : Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
2023-01-19 17:20:46.923 INFO 66213 --- [ main] o.t.d.DockerClientProviderStrategy : Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
2023-01-19 17:20:47.024 INFO 66213 --- [ main] .t.d.DockerMachineClientProviderStrategy : docker-machine executable was not found on PATH ([/usr/bin, /bin, /usr/sbin, /sbin])
2023-01-19 17:20:47.036 ERROR 66213 --- [ main] o.t.d.DockerClientProviderStrategy : Could not find a valid Docker environment. Please check configuration. Attempted configurations were:
2023-01-19 17:20:47.036 ERROR 66213 --- [ main] o.t.d.DockerClientProviderStrategy : UnixSocketClientProviderStrategy: failed with exception InvalidConfigurationException (Could not find unix domain socket). Root cause NoSuchFileException (/var/run/docker.sock)
2023-01-19 17:20:47.036 ERROR 66213 --- [ main] o.t.d.DockerClientProviderStrategy : As no valid configuration was found, execution cannot continue
17:20:47.037 [main] DEBUG com.zaxxer.hikari.pool.PoolBase - HikariPool-1 - Failed to create/setup connection: Could not find a valid Docker environment. Please see logs and check configuration
17:20:47.042 [main] DEBUG com.zaxxer.hikari.pool.HikariPool - HikariPool-1 - Cannot acquire connection from data source
java.lang.IllegalStateException: Could not find a valid Docker environment. Please see logs and check configuration
at org.testcontainers.dockerclient.DockerClientProviderStrategy.lambda$getFirstValidStrategy$6(DockerClientProviderStrategy.java:242)
at java.util.Optional.orElseThrow(Optional.java:290)
A) IntelliJ 에서 .zshrc 에 export된 TestContainers 설정을 가져오지 못해 error가 나는 상황입니다. 아래와 같이 설정하시면 해결 가능합니다.
- Run > Edit Configurations > 왼쪽 하단의 Edit configuration templates... 클릭
- JUnit 선택 후 Environment variables: 항목에 아래 내용 추가 후 Apply & OK
/Users/xxxx 는 각자 컴퓨터에 설정된 HOME 디렉토리를 적어주시면 됩니다.
DOCKER_HOST=unix:///Users/xxxx/.colima/docker.sock;TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock;TESTCONTAINERS_RYUK_DISABLED=true
- 추후 생성되는 테스트들은 위의 설정을 override 하여 생성 됩니다.
Reference