英文:
GoLang postgres testcontainers init script doesn't work
问题
我想要使用初始化脚本启动postgres容器。
request := testcontainers.ContainerRequest{
Image: "postgres:14.1-alpine",
Entrypoint: nil,
Env: map[string]string{
"POSTGRES_DB": "postgres",
"PGUSER": "postgres",
"POSTGRES_PASSWORD": "postgres",
//"PGDATA": "postgres",
},
ExposedPorts: []string{"5432"},
BindMounts: map[string]string{
"/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations": "/docker-entrypoint-initdb.d",
},
Name: "postgres",
User: "postgres",
WaitingFor: wait.ForLog("database system is ready to accept connections"),
AutoRemove: true,
}
container, err = testcontainers.GenericContainer(
test.CTX,
testcontainers.GenericContainerRequest{
ContainerRequest: request,
Started: true,
},
)
if err != nil {
log.Panicln(err)
}
我在log.Panicln(err)中得到了以下错误信息:
failed to create container
Error response from daemon
invalid mount config for type "bind": bind source path does not exist: /docker-entrypoint-initdb.d
问题是它在docker-compose.yml中完美地工作。如何修复这个问题?
英文:
I want to start postgres container with init script.
request := testcontainers.ContainerRequest{
Image: "postgres:14.1-alpine",
Entrypoint: nil,
Env: map[string]string{
"POSTGRES_DB": "postgres",
"PGUSER": "postgres",
"POSTGRES_PASSWORD": "postgres",
//"PGDATA": "postgres",
},
ExposedPorts: []string{"5432"},
BindMounts: map[string]string{
"/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations": "/docker-entrypoint-initdb.d",
},
Name: "postgres",
User: "postgres",
WaitingFor: wait.ForLog("database system is ready to accept connections"),
AutoRemove: true,
}
container, err = testcontainers.GenericContainer(
test.CTX,
testcontainers.GenericContainerRequest{
ContainerRequest: request,
Started: true,
},
)
if err != nil {
log.Panicln(err)
}
I got the following panic messages in log.Panicln(err):
failed to create container
Error response from daemon
invalid mount config for type "bind": bind source path does not exist: /docker-entrypoint-initdb.d
The point is that it perfectly works from docker-compose.yml.
How to fix this?
答案1
得分: 2
看起来,根据源代码,TestContainers希望在BindMounts
中使用container_path: host_path
的形式。如果你尝试以下代码会发生什么:
BindMounts: map[string]string{
"/docker-entrypoint-initdb.d": "/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations",
},
看起来,TestContainers的更新版本已经完全移除了BindMounts
,并用一个更通用的Mounts
字段来替代。
英文:
Looking at the source, it appears that TestContainers wants container_path: host_path
in BindMounts
. What happens if you try:
BindMounts: map[string]string{
"/docker-entrypoint-initdb.d": "/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations",
},
It looks like more recent versions of TestContainers have removed BindMounts
completely and replaced it with a more generic Mounts
field.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论