Unable to write valid mount path for docker container in go
php editor Zimo found that some developers encountered a problem when writing docker containers using Go language, that is, they could not write a valid mounting path for the container. This issue may cause errors or failures during file read and write operations in the container. For developers, this is undoubtedly a frustrating problem. Next, we’ll explore the cause of this issue and possible solutions to help developers solve this challenge.
Question content
I'm trying to start a test container to test my database. I'm using a test container. Here is a snippet of how I set up the container:
func createContainer(ctx context.Context) (testcontainers.Container, *pgxpool.Pool, string, error) { var env = map[string]string{ "POSTGRES_PASSWORD": DbPass, "POSTGRES_USER": DbUser, "POSTGRES_DB": DbName, } var port = "5432/tcp" // /Users/<path>:/<container path> path := `/c/Users/pizhlo21/Desktop/Folder/golang/TgBotReminder/internal/db/postgresql/migration:/usr/app` req := testcontainers.GenericContainerRequest{ ContainerRequest: testcontainers.ContainerRequest{ Image: "postgres:latest", ExposedPorts: []string{port}, Env: env, WaitingFor: wait.ForLog("database system is ready to accept connections"), VolumeMounts: map[string]string{"/docker-entrypoint-initdb.d": path}, SkipReaper: true, }, Started: true, } container, err := testcontainers.GenericContainer(ctx, req) if err != nil { return container, nil, "", fmt.Errorf("unable to start container: %v", err) } ...
But I got the error from docker: failed to setup testunable to start container: failed to create container: error response from daemon: create /docker-entrypoint-initdb.d: "/docker-entrypoint-initdb. d" includes invalid characters for local volume names, only "[a-za-z0-9][a-za-z0-9_.-]" is allowed. If you plan to pass the host directory, use the absolute path
.
Sometimes this error looks like this: Unable to set up test Unable to start container: Unable to create container: Error response from daemon: Invalid mount configuration of type 'volume': Invalid mount path: 'c' /desktop/folder/golang/tgbotreminder/internal/db/postgresql/migration/000001_init_schema.up"' The mount path must be absolute
I tried many different paths, for example:
//c/user/...
c/user/...
/�%/Desktop/...
$home/Desktop/Folder/...
But nothing helped me.
How to execute it correctly?
Solution
tl;dr
replace
volumemounts: map[string]string{"/docker-entrypoint-initdb.d": path},
and
bindmounts: map[string]string{"/docker-entrypoint-initdb.d": path},
Question 1
"/docker-entrypoint-initdb.d" contains invalid characters for a local volume name, only "[a-za-z0-9][a-za-z0-9_.-]" is allowed. p>
It is important to know There are three types of mounts :
- Volumes are stored in part of the host file system managed by docker (
/var/lib/docker/volumes/
on linux). Non-docker processes should not modify this part of the file system. Volumes are the best way to save data in docker. - Bind mounts can be stored anywhere on the host system. They might even be important system files or directories. They can be modified at any time by non-Docker processes on the docker host or docker container.
tmpfs
The mount is stored only in the host system's memory and is never written to the host system's file system.
volumemounts
Used to specify volume mounts. From github.com/testcontainers/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="94e0f1e7e0f7fbfae0f5fdfaf1e6e7b9f3fbd4e2a4baa5a6baa4">[email protected]</a> The value of
The entry in volumemounts
stores the volume name , which only allows [a-za-z0-9][a-za-z0-9_. -]
, that's why you're seeing the error message. BTW, to see the error message above, your code must look like this (note that /docker-entrypoint-initdb.d
is a value not a key):
volumemounts: map[string]string{path: "/docker-entrypoint-initdb.d"},
Question 2
Invalid mount configuration for type "volume": Invalid mount path: "c/desktop/folder/golang/tgbotreminder/internal/db/postgresql/migration/000001_init_schema.up" mount path must be absolute
To see this error message, your code must look like this:
volumemounts: map[string]string{`"/c/desktop/folder/golang/tgbotreminder/internal/db/postgresql/migration/000001_init_schema.up"`: "/docker-entrypoint-initdb.d"},
asgithub.com/testcontainers/ <a href="/cdn-cgi/l/email-protection" class="__cf_email__" __cf_email__" data-cfemail="4443021373027272b2a302a30252a2a2a2a2113632132b21132b21132b211 32b2b2a2b2a2b2a2b2b2b2binging a>
, # The keystore mount path for the entry in ##volumemounts. Since the mount path contains double quotes (
"), it is invalid. The docker daemon first validates the mount path. That's why you see the error message.
github.com/testcontainers/testcontainers-go
In version v0.13.0, containerrequest.bindmounts and
containerrequest.volumemounts were replaced by
containerrequest.mounts. This is because "map-based data structures are somewhat confusing. This change avoids confusion by introducing dedicated types for all components to get help from the IDE and compiler". (See
pr#386).
升级到 github.com/testcontainers/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9aeeffe9eef9f5f4eefbf3f4ffe8e9b7fdf5daecaab4a8abb4aa">[电子邮件受保护]</a>
后,可以使用以下方式指定绑定安装:
Mounts: testcontainers.Mounts( testcontainers.BindMount(path, "/docker-entrypoint-initdb.d"), ),
The above is the detailed content of Unable to write valid mount path for docker container in go. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The library used for floating-point number operation in Go language introduces how to ensure the accuracy is...

Queue threading problem in Go crawler Colly explores the problem of using the Colly crawler library in Go language, developers often encounter problems with threads and request queues. �...

The difference between string printing in Go language: The difference in the effect of using Println and string() functions is in Go...

Which libraries in Go are developed by large companies or well-known open source projects? When programming in Go, developers often encounter some common needs, ...

The problem of using RedisStream to implement message queues in Go language is using Go language and Redis...

What should I do if the custom structure labels in GoLand are not displayed? When using GoLand for Go language development, many developers will encounter custom structure tags...

Two ways to define structures in Go language: the difference between var and type keywords. When defining structures, Go language often sees two different ways of writing: First...

Go pointer syntax and addressing problems in the use of viper library When programming in Go language, it is crucial to understand the syntax and usage of pointers, especially in...
