Table of Contents
Question content
Solution
tl;dr
Question 2
Home Backend Development Golang Unable to write valid mount path for docker container in go

Unable to write valid mount path for docker container in go

Feb 11, 2024 pm 02:27 PM
go language

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What libraries are used for floating point number operations in Go? What libraries are used for floating point number operations in Go? Apr 02, 2025 pm 02:06 PM

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

What is the problem with Queue thread in Go's crawler Colly? What is the problem with Queue thread in Go's crawler Colly? Apr 02, 2025 pm 02:09 PM

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. �...

In Go, why does printing strings with Println and string() functions have different effects? In Go, why does printing strings with Println and string() functions have different effects? Apr 02, 2025 pm 02:03 PM

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 provided by well-known open source projects? Which libraries in Go are developed by large companies or provided by well-known open source projects? Apr 02, 2025 pm 04:12 PM

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, ...

How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? How to solve the user_id type conversion problem when using Redis Stream to implement message queues in Go language? Apr 02, 2025 pm 04:54 PM

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? What should I do if the custom structure labels in GoLand are not displayed? Apr 02, 2025 pm 05:09 PM

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...

What is the difference between `var` and `type` keyword definition structure in Go language? What is the difference between `var` and `type` keyword definition structure in Go language? Apr 02, 2025 pm 12:57 PM

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...

Why is it necessary to pass pointers when using Go and viper libraries? Why is it necessary to pass pointers when using Go and viper libraries? Apr 02, 2025 pm 04:00 PM

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...

See all articles