GoLang的postgres testcontainers初始化脚本不起作用。

huangapple go评论112阅读模式
英文:

GoLang postgres testcontainers init script doesn't work

问题

我想要使用初始化脚本启动postgres容器。

  1. request := testcontainers.ContainerRequest{
  2. Image: "postgres:14.1-alpine",
  3. Entrypoint: nil,
  4. Env: map[string]string{
  5. "POSTGRES_DB": "postgres",
  6. "PGUSER": "postgres",
  7. "POSTGRES_PASSWORD": "postgres",
  8. //"PGDATA": "postgres",
  9. },
  10. ExposedPorts: []string{"5432"},
  11. BindMounts: map[string]string{
  12. "/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations": "/docker-entrypoint-initdb.d",
  13. },
  14. Name: "postgres",
  15. User: "postgres",
  16. WaitingFor: wait.ForLog("database system is ready to accept connections"),
  17. AutoRemove: true,
  18. }
  19. container, err = testcontainers.GenericContainer(
  20. test.CTX,
  21. testcontainers.GenericContainerRequest{
  22. ContainerRequest: request,
  23. Started: true,
  24. },
  25. )
  26. if err != nil {
  27. log.Panicln(err)
  28. }

我在log.Panicln(err)中得到了以下错误信息:

  1. failed to create container
  2. Error response from daemon
  3. 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.

  1. request := testcontainers.ContainerRequest{
  2. Image: "postgres:14.1-alpine",
  3. Entrypoint: nil,
  4. Env: map[string]string{
  5. "POSTGRES_DB": "postgres",
  6. "PGUSER": "postgres",
  7. "POSTGRES_PASSWORD": "postgres",
  8. //"PGDATA": "postgres",
  9. },
  10. ExposedPorts: []string{"5432"},
  11. BindMounts: map[string]string{
  12. "/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations": "/docker-entrypoint-initdb.d",
  13. },
  14. Name: "postgres",
  15. User: "postgres",
  16. WaitingFor: wait.ForLog("database system is ready to accept connections"),
  17. AutoRemove: true,
  18. }
  19. container, err = testcontainers.GenericContainer(
  20. test.CTX,
  21. testcontainers.GenericContainerRequest{
  22. ContainerRequest: request,
  23. Started: true,
  24. },
  25. )
  26. if err != nil {
  27. log.Panicln(err)
  28. }

I got the following panic messages in log.Panicln(err):

  1. failed to create container
  2. Error response from daemon
  3. 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的形式。如果你尝试以下代码会发生什么:

  1. BindMounts: map[string]string{
  2. "/docker-entrypoint-initdb.d": "/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations",
  3. },

看起来,TestContainers的更新版本已经完全移除了BindMounts,并用一个更通用的Mounts字段来替代。

英文:

Looking at the source, it appears that TestContainers wants container_path: host_path in BindMounts. What happens if you try:

  1. BindMounts: map[string]string{
  2. "/docker-entrypoint-initdb.d": "/media/mihovelgod/Новый том1/GoProjects/microservices/sql/go-sql/resources/migrations",
  3. },

It looks like more recent versions of TestContainers have removed BindMounts completely and replaced it with a more generic Mounts field.

huangapple
  • 本文由 发表于 2022年2月1日 06:12:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/70933442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定