【问题标题】:Docker using GitBash / Cmder - Path issueDocker 使用 GitBash / Cmder - 路径问题
【发布时间】:2021-04-15 18:38:56
【问题描述】:

Docker 终端命令

尝试休闲 docker 教程 [https://docs.docker.com/get-started/06_bind_mounts/][1]
docker run -dp 3000:3000 -w /app -v "$(pwd):/app" node:12-alpine sh -c "yarn install && yarn run dev"

我对上面的代码行有疑问。我在 Cmder、GitBash 和 Windows 10 PowerShell 终端中运行这一行。

控制台 1 - Cmder - 错误:

docker: Error response from daemon: create $(pwd): "$(pwd)" includes invalid characters for a local volume name, only "[a-zA-Z0-9][a-zA-Z0-9_.-]" are allowed. If you intended to pass a host directory, use absolute path.
See 'docker run --help'.

控制台 2 - GitBash - `错误`:

docker: Error response from daemon: the working directory 'C:/Program Files/Git/app' is invalid, it needs to be an absolute path.
See 'docker run --help'.

可能的原因:

https://github.com/docker/cli/issues/2204

How to stop MinGW and MSYS from mangling path names given at the command line

控制台 3 - Windows PowerShell - `SUCCESS`:

df1ad0a4f71016f7832b6d9d02f963f33cc2cc8d5740e1013561287d875fb5de

$ docker ps

CONTAINER ID   IMAGE                    COMMAND                  CREATED         STATUS         PORTS                    NAMES
df1ad0a4f710   node:12-alpine           "docker-entrypoint.s…"   3 minutes ago   Up 3 minutes   0.0.0.0:3000->3000/tcp   exciting_liskov
37d12fa854d7   mysql:5.7                "docker-entrypoint.s…"   3 hours ago     Up 3 hours     3306/tcp, 33060/tcp      admiring_faraday
23803e6325db   docker/getting-started   "/docker-entrypoint.…"   13 hours ago    Up 13 hours    0.0.0.0:80->80/tcp       boring_banach

$ 码头工人日志

$ docker
df1ad0a4f71016f7832b6d9d02f963f33cc2cc8d5740e1013561287d875fb5de
yarn install v1.22.5
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.52s.
yarn run v1.22.5
$ nodemon src/index.js
[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/index.js`
Using sqlite database at /etc/todos/todo.db
Listening on port 3000
PS C:\WEB APPS\app\app>

之前有人让它在 GitBash 或 Cmder 中工作,还是这是我的 Env 问题?猜测你们中的大多数人会在 Mack OS 或 Linux 发行版上运行它。也许 vegrant + docker 是要走的路?您在 Windows 上的设置是什么?

返回控制台 2 - GitBash - 尝试解决可能的路径问题:

因此,解决上述问题,我修改了添加附加“/”(How to stop MinGW and MSYS from mangling path names given at the command line)的行

docker run -dp 3000:3000 -w //app -v "$(pwd)/app" node:12-alpine sh -c "yarn install && yarn run dev"
cfde24fc30e8a8d3e83cade4a00ee318e37ced1d90aa831f0d56b9dc7148be22

现在 docker 容器已创建但未运行...请参阅以下问题

$ 码头工人日志

cfde24fc30e8a8d3e83cade4a00ee318e37ced1d90aa831f0d56b9dc7148be22
yarn install v1.22.5
info No lockfile found.
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
[4/4] Building fresh packages...
success Saved lockfile.
Done in 0.04s.
yarn run v1.22.5
error Couldn't find a package.json file in "/app"
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

现在我们有 ... 错误 Couldn't find a package.json file in "/app" 有人在 gitbash 中解决问题吗?

【问题讨论】:

  • 看起来docker run 命令正在尝试在当前目录的内容上运行 Node...installing Node locally 会比尝试将 Docker 插入到组合中更容易吗?

标签: docker powershell windows-10 git-bash cmder


【解决方案1】:

控制台 2 - GITBASH - 解决方案

根据How to stop MinGW and MSYS from mangling path names given at the command line,我们可以通过在实际命令之前运行MSYS_NO_PATHCONV=1 使其在Git bash 上运行。这将禁用该命令的路径转换。还有一种方法可以全局关闭路径转换,但是因为我不知道确切的结果,所以我选择在每个命令之前添加这个位。

控制台 2 - GitBash - 好的

MSYS_NO_PATHCONV=1 docker run -dp 3000:3000 -w /app -v "$(pwd):/app" node:12-alpine sh -c "yarn install && yarn run dev"
717d12b9fe5211f0189ccbed0ba056ca242647812627682d0149ede29af472a4

码头工人日志 c4a0bcc82c1b

yarn install v1.22.5
warning package-lock.json found. Your project contains lock files generated by tools other than Yarn. It is advised not to mix package managers in order to avoid resolution inconsistencies caused by unsynchronized lock files. To clear this warning, remove package-lock.json.
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.64s.
yarn run v1.22.5
$ nodemon src/index.js
[nodemon] 1.19.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node src/index.js`
Using sqlite database at /etc/todos/todo.db
Listening on port 3000

控制台 1 - CMDER - 未找到 /app - 容器已创建但未运行

为了让这个命令在 cmndr 终端中工作,我必须在 `$(pwd)` 之前添加 `/`,如下所示:
docker run -dp 3000:3000 -w /app -v /$(pwd):/app node:12-alpine sh -c "yarn install && yarn run dev"

不幸的是,这并没有完全奏效。这次它运行并没有显示错误,但找不到 /app 路径下的文件。 DOcker 容器已创建,但未显示在 docker ps

error Couldn't find a package.json file in "/app"
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

【讨论】:

    【解决方案2】:

    问题是 Windows 上的可执行文件希望将路径视为“windows”(例如c:\whatever\is\here),而不是将它们获得的“posix”等效项(例如c:/whatever/is/here)。即使你在“git bash”中运行你的 docker 可执行文件,底层的可执行文件仍然是一个 windows 版本的 docker,这让它打嗝。

    在 Powershell 上这是可行的,因为 Powershell 在 CMD 上创建了应有的路径(windows 版本),shell 不理解此命令。

    按照this solution,可以修改命令运行如下:

    docker run -dp 3000:3000 -w /app -v "$(pwd | sed -e 's/^\///' -e 's/\//\\/g' -e 's/^./\0:/'):/app" node:12-alpine sh -c "yarn install && yarn run dev"
    

    它可能真的有效。

    关于我在 Windows 上的配置的问题。我在 windows 和 linux 上工作,但我努力理解和更改命令以适合我,而不是盲目地遵循我在某些教程中给出的命令(这是一个值得养成的习惯)

    【讨论】:

      猜你喜欢
      • 2017-09-25
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-07
      • 1970-01-01
      • 1970-01-01
      • 2010-12-11
      相关资源
      最近更新 更多