Unable to write valid mount path for docker container in go

王林
Release: 2024-02-11 14:27:18
forward
1041 people have browsed it

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)
    }
...
Copy after login

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},
Copy after login

and

bindmounts: map[string]string{"/docker-entrypoint-initdb.d": path},
Copy after login

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.

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"},
Copy after login

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"},
Copy after login

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.

Consider upgrading

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"),
),
Copy after login

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!

Related labels:
source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!