SQL测试容器无法在MacBook M1上启动



根据Docker官方网站,Docker不支持M1处理器的SQL镜像;但是,它支持通过传递"——平台linux/x86_64 mysql".

实际上,SQL docker是通过以下命令成功拉出的

docker pull --platform linux/x86_64 mysql

输出:

Using default tag: latest
latest: Pulling from library/mysql
a330b6cecb98: Pull complete 
9c8f656c32b8: Pull complete 
88e473c3f553: Pull complete 
062463ea5d2f: Pull complete 
daf7e3bdf4b6: Pull complete 
1839c0b7aac9: Pull complete 
cf0a0cfee6d0: Pull complete 
1b42041bb11e: Pull complete 
10459d86c7e6: Pull complete 
b7199599d5f9: Pull complete 
1d6f51e17d45: Pull complete 
50e0789bacad: Pull complete 
Digest: sha256:99e0989e7e3797cfbdb8d51a19d32c8d286dd8862794d01a547651a896bcf00c
Status: Downloaded newer image for mysql:latest
docker.io/library/mysql:late

现在,我想在我的集成测试中使用SQL容器,但是docker无法拉出SQL。

初始化SQL测试容器的代码

@DynamicPropertySource
static void dynamicPropertySource(DynamicPropertyRegistry dynamicPropertyRegistry) {
MySQLContainer<?> container = new MySQLContainer<>("mysql:8.0.26")
.withDatabaseName("sympl")
.withUsername("test")
.withPassword("test")
.withCommand("docker pull --platform linux/x86_64 mysql");
dynamicPropertyRegistry.add("spring.datasource.url", () -> container.getJdbcUrl());
dynamicPropertyRegistry.add("spring.datasource.username", () -> container.getUsername());
dynamicPropertyRegistry.add("spring.datasource.password", () -> container.getPassword());
container.start();
}

我试着写代码withCommand()和没有.withCommand。在我运行测试用例之后,它给出了以下失败日志:

17:18:25.003 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sympl.customer.api.CustomerApiIntegrationTests]
17:18:25.003 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sympl.customer.api.CustomerApiIntegrationTests]
17:18:25.005 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sympl.customer.api.CustomerApiIntegrationTests]
17:18:25.005 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sympl.customer.api.CustomerApiIntegrationTests]
17:18:25.015 [main] DEBUG org.springframework.test.context.support.AbstractDirtiesContextTestExecutionListener - Before test class: context [DefaultTestContext@3faf2e7d testClass = CustomerApiIntegrationTests, testInstance = [null], testMethod = [null], testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@4648ce9 testClass = CustomerApiIntegrationTests, locations = '{}', classes = '{class com.sympl.customer.CustomersApplication}', contextInitializerClasses = '[]', activeProfiles = '{test}', propertySourceLocations = '{}', propertySourceProperties = '{org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}', contextCustomizers = set[org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@78fa769e, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@2c78324b, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@3fd11d55, org.springframework.boot.test.web.client.TestRestTemplateContextCustomizer@6ca18a14, org.springframework.boot.test.autoconfigure.actuate.metrics.MetricsExportContextCustomizerFactory$DisableMetricExportContextCustomizer@1ec9bd38, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@0, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizerFactory$Customizer@cd1e646, org.springframework.test.context.support.DynamicPropertiesContextCustomizer@aa1b54d2, org.springframework.boot.test.context.SpringBootTestArgs@1, org.springframework.boot.test.context.SpringBootTestWebEnvironment@5890e879], resourceBasePath = 'src/main/webapp', contextLoader = 'org.springframework.boot.test.context.SpringBootContextLoader', parent = [null]], attributes = map['org.springframework.test.context.web.ServletTestExecutionListener.activateListener' -> false]], class annotated with @DirtiesContext [false] with mode [null].
17:18:25.021 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved @ProfileValueSourceConfiguration [null] for test class [com.sympl.customer.api.CustomerApiIntegrationTests]
17:18:25.021 [main] DEBUG org.springframework.test.annotation.ProfileValueUtils - Retrieved ProfileValueSource type [class org.springframework.test.annotation.SystemProfileValueSource] for class [com.sympl.customer.api.CustomerApiIntegrationTests]
17:18:25.088 [main] DEBUG org.springframework.core.env.StandardEnvironment - Activating profiles [test]
17:18:25.094 [main] DEBUG org.springframework.test.context.support.TestPropertySourceUtils - Adding inlined properties to environment: {spring.jmx.enabled=false, org.springframework.boot.test.context.SpringBootTestContextBootstrapper=true}
.   ____          _            __ _ _
/\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |___, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::                (v2.5.3)
2021-09-19 17:18:26.039  INFO 9715 --- [           main] o.t.d.DockerClientProviderStrategy       : Loaded org.testcontainers.dockerclient.UnixSocketClientProviderStrategy from ~/.testcontainers.properties, will try it first
2021-09-19 17:18:27.294  INFO 9715 --- [           main] o.t.d.DockerClientProviderStrategy       : Found Docker environment with local Unix socket (unix:///var/run/docker.sock)
2021-09-19 17:18:27.299  INFO 9715 --- [           main] org.testcontainers.DockerClientFactory   : Docker host IP address is localhost
2021-09-19 17:18:27.365  INFO 9715 --- [           main] org.testcontainers.DockerClientFactory   : Connected to docker: 
Server Version: 20.10.8
API Version: 1.41
Operating System: Docker Desktop
Total Memory: 1988 MB
2021-09-19 17:18:27.369  INFO 9715 --- [           main] o.t.utility.ImageNameSubstitutor         : Image name substitution will be performed by: DefaultImageNameSubstitutor (composite of 'ConfigurationFileImageNameSubstitutor' and 'PrefixingImageNameSubstitutor')
2021-09-19 17:18:27.547  INFO 9715 --- [           main] o.t.utility.RegistryAuthLocator          : Credential helper/store (docker-credential-desktop) does not have credentials for index.docker.io
2021-09-19 17:18:28.202  INFO 9715 --- [           main] org.testcontainers.DockerClientFactory   : Ryuk started - will monitor and terminate Testcontainers containers on JVM exit
2021-09-19 17:18:28.203  INFO 9715 --- [           main] org.testcontainers.DockerClientFactory   : Checking the system...
2021-09-19 17:18:28.203  INFO 9715 --- [           main] org.testcontainers.DockerClientFactory   : ✔︎ Docker server version should be at least 1.6.0
2021-09-19 17:18:28.322  INFO 9715 --- [           main] org.testcontainers.DockerClientFactory   : ✔︎ Docker environment should have more than 2GB free disk space
2021-09-19 17:18:28.388  INFO 9715 --- [           main] 🐳 [mysql:8.0.26]                        : Pulling docker image: mysql:8.0.26. Please be patient; this may take some time but only needs to be done once.
2021-09-19 17:18:32.326  INFO 9715 --- [stream--1235448] 🐳 [mysql:8.0.26]                        : Starting to pull image
2021-09-19 17:18:32.369  INFO 9715 --- [stream--1235448] 🐳 [mysql:8.0.26]                        : Pulling image layers:  0 pending,  0 downloaded,  0 extracted, (0 bytes/0 bytes)
2021-09-19 17:18:32.422 ERROR 9715 --- [           main] o.s.boot.SpringApplication               : Application run failed
org.testcontainers.containers.ContainerLaunchException: Container startup failed
at org.testcontainers.containers.GenericContainer.doStart(GenericContainer.java:330) ~[testcontainers-1.15.3.jar:na]
at org.testcontainers.containers.GenericContainer.start(GenericContainer.java:311) ~[testcontainers-1.15.3.jar:na]
at com.sympl.customer.api.CustomerApiIntegrationTests.dynamicPropertySource(CustomerApiIntegrationTests.java:79) ~[test-classes/:na]
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:na]

这是docker日志

我确信有一个解决这个问题的方法,但是我直到现在才找到它。

我是微软Azure SQL Edge的现任PM,顺便来分享一个更新。Docker Desktop v4.16引入了对虚拟化框架的测试版支持,这意味着你现在可以在M1/M2芯片上运行基于x86/amd64的SQL Server容器。这将是我们推荐的在基于ARM的macOS上运行SQL server容器的路径,因为它将使您能够针对SQL server 2017-2022以及Azure SQL Edge进行开发。有关如何启用该特性并在macOS上运行SQL Server 2022容器的详细介绍,请查看https://devblogs.microsoft.com/azure-sql/development-with-sql-in-containers-on-macos/

最新更新