0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Docker Desktopが使えない環境でRauncher Desktopを使ってtestcontainersをMS SQL Serverで試してみたメモ

Last updated at Posted at 2025-04-10

概要

Windows Docker Desktopは個人では無料だが、ある程度の規模の組織だと有料になる。

この時の回避策としては下記のいずれかになる

  • WSL2上にDocker CE をインストール *
  • Rauncher Desktop をインストール *

Docker CEはtestcontainersから起動しようとすると//./pipe/docker_engineと表示されてうまく動かなかった。なので、Rauncher Desktopを使用することとする

Rauncher Desktopの準備

インストール

chocolateyで配布しているのでそれで準備した。10Gb弱となかなか重い。

choco install rancher-desktop

インストールできたら起動しておく

タイムアウト設定

SQLServerは結構重いので pull 時間を延ばしておく。 *

~/.testcontainers.properties
pull.timeout=120
pull.pause.timeout=30

configration

コード

dbをfunctionsの外で定義している環境を想定。このとき、BeforeAllでコンテナを作っては間に合わない

test/setupDbContainer.ts
import fs from 'fs';
import { afterAll } from 'vitest';
import { StartedMSSQLServerContainer, MSSQLServerContainer } from '@testcontainers/mssqlserver';
import * as sql from 'mssql';

const mssqlContainer: StartedMSSQLServerContainer = await new MSSQLServerContainer('mcr.microsoft.com/mssql/server:2022-CU13-ubuntu-22.04').withWaitForMessage(/.*Attribute synchronization manager init*/).acceptLicense().withEnvironment({ACCEPT_EULA: 'Y', MSSQL_SA_PASSWORD: 'hoge', MSSQL_PID: 'Developer',MSSQL_COLLATION:'Japanese_CI_AS'}).start();

const connectionString = mssqlContainer.getConnectionUri();
process.env.SQL_CONNECTION_STRING = connectionString;

await sql.connect(connectionString);
await sql.query(fs.readFileSync('./migration.sql','utf8'));

afterAll(async () => {
  await mssqlContainer.stop();
});

vitest.workspace.ts
import { defineWorkspace } from 'vitest/config'
export default defineWorkspace([
 {
  test:{
    env: {TZ:'UTC', SQL_CONNECTION_STRING: ''},
    name: 'integration-test',
    include: ['**/*.db.test.ts'],
    setupFiles: ['test/setupDbContainer.ts'],
    deps: { interopDefault: true },
    environment: 'node',
    // コンテナの起動を含めるためタイムアウトを延長
    hookTimeout: 60000,
    testTimeout: 60000,
    poolOptions: {forks: {singleFork:true}}
}
]);

実行

rauncher desktop用の設定を入れて起動する必要がある
cross-env DOCKER_HOST=unix://${HOME}/.rd/docker.sock TESTCONTAINERS_DOCKER_SOCKET_OVERRIDE=/var/run/docker.sock DEBUG=testcontainers* vitest

参考

使い捨てテストDB環境のtestcontainersをPostgresql + Honoで試してみたメモ
[Windowsにゼロから WSL2 + Docker CE + React環境を構築する全手順]
Rauncher Desktop
Rancher Desktopの使い方|Docker Desktopとの違いや移行手順を解説
sqlserver docker
sqlserver env

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?