我正在try 使用测试容器运行容器.以下是我的代码:
func createContainer(ctx context.Context) (testcontainers.Container, *gorm.DB, string, error) {
var env = map[string]string{
"POSTGRES_PASSWORD": DbPass,
"POSTGRES_USER": DbUser,
"POSTGRES_DB": DbName,
}
var port = "5432/tcp"
path := `C:/Desktop/Folder/golang/TgBotReminder/internal/db/postgresql/migration/notes.sql`
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "postgres:latest",
ExposedPorts: []string{port},
Env: env,
WaitingFor: wait.ForLog("database system is ready to accept connections"),
SkipReaper: true,
Mounts: testcontainers.Mounts(
testcontainers.BindMount(path, "/docker-entrypoint-initdb.d"),
),
},
Started: true,
}
...
我在PATH变量中提到的目录存在.但我得到的错误是:Error response from daemon: invalid mount config for type "bind": bind source path does not exist: /pipe/docker_engine: creating reaper failed: failed to create container
.
即使我用挂载注释行,我也会收到这个错误.
如何修复它?另外,在PATH变量("notes.sql")中输入文件名是否正确?或者我应该只在这里输入目录?
先谢谢你
Update 1个
我现在也try 了这个代码:
container, err := tspostgres.RunContainer(ctx,
testcontainers.WithImage("postgres:latest"),
tspostgres.WithInitScripts(filepath.Join(".", "test_migration", "notes.sql")),
tspostgres.WithDatabase("test-db"),
tspostgres.WithUsername("postgres"),
tspostgres.WithPassword("postgres"),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).WithStartupTimeout(5*time.Second)),
)
if err != nil {
log.Fatal(err)
}
收到相同的错误
Update 2个
下面是我try 过的另一段代码:
path := `/${PWD}/desktop/folder/golang/tgbotreminder/internal/db/postgresql/migration/notes.sql`
target := "/docker-entrypoint-initdb.d/"
...
Mounts: testcontainers.ContainerMounts{
testcontainers.ContainerMount{
Source: testcontainers.GenericBindMountSource{
HostPath: path,
},
Target: testcontainers.ContainerMountTarget(target),
},
},
再也没有运气了:
Update 3个
这是我的另一次try :
Mounts: testcontainers.Mounts(testcontainers.ContainerMount{
Source: testcontainers.GenericBindMountSource{
HostPath: path,
},
Target: testcontainers.ContainerMountTarget(target),
}),
它也不起作用