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!

go语言有缩进。在go语言中,缩进直接使用gofmt工具格式化即可(gofmt使用tab进行缩进);gofmt工具会以标准样式的缩进和垂直对齐方式对源代码进行格式化,甚至必要情况下注释也会重新格式化。

go语言叫go的原因:想表达这门语言的运行速度、开发速度、学习速度(develop)都像gopher一样快。gopher是一种生活在加拿大的小动物,go的吉祥物就是这个小动物,它的中文名叫做囊地鼠,它们最大的特点就是挖洞速度特别快,当然可能不止是挖洞啦。

是,TiDB采用go语言编写。TiDB是一个分布式NewSQL数据库;它支持水平弹性扩展、ACID事务、标准SQL、MySQL语法和MySQL协议,具有数据强一致的高可用特性。TiDB架构中的PD储存了集群的元信息,如key在哪个TiKV节点;PD还负责集群的负载均衡以及数据分片等。PD通过内嵌etcd来支持数据分布和容错;PD采用go语言编写。

go语言能编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言。对Go语言程序进行编译的命令有两种:1、“go build”命令,可以将Go语言程序代码编译成二进制的可执行文件,但该二进制文件需要手动运行;2、“go run”命令,会在编译后直接运行Go语言程序,编译过程中会产生一个临时文件,但不会生成可执行文件。

go语言需要编译。Go语言是编译型的静态语言,是一门需要编译才能运行的编程语言,也就说Go语言程序在运行之前需要通过编译器生成二进制机器码(二进制的可执行文件),随后二进制文件才能在目标机器上运行。

删除字符串的方法:1、用TrimSpace()来去除字符串空格;2、用Trim()、TrimLeft()、TrimRight()、TrimPrefix()或TrimSuffix()来去除字符串中全部、左边或右边指定字符串;3、用TrimFunc()、TrimLeftFunc()或TrimRightFunc()来去除全部、左边或右边指定规则字符串。


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

Dreamweaver CS6
Visual web development tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
