bash - 如何强制 Git for Windows 的 bash-shell 不将路径字符串转换为 Windows 路径?

标签 bash docker-compose environment-variables git-bash

我正在使用 Git for Windows for Windows for Docker toolbox for Windows 提供的 bash shell。我想将表示 unix 路径的字符串导出到环境变量,然后在 docker 容器中使用。像这样的东西:

export MY_VAR=/my/path; docker-compose up

问题是在我的容器中变量会是这样的:

echo $MY_VAR # prints c:/Program Files/Git/my/path

所以 shell(我的猜测)似乎将字符串识别为路径并将其转换为 Windows 格式。有办法阻止这种情况吗?

我尝试使用 MSYS_NO_PATHCONV=1:

MSYS_NO_PATHCONV=1; export LOG_PATH=/my/path; docker-compose up

但是没有任何效果。

我认为我的 docker-compose 和 dockerfile 没有问题,但如果有人感兴趣,我会附上它们。

我的Dockerfile:

FROM node:8-slim
RUN mkdir /test \
    && chown node:node /test
USER node
ENTRYPOINT [ "/bin/bash" ]

我的docker-compose.yml:

version: '2'
services:
  test:
    build:
      context: .
    image: test
    environment:
      - MY_VAR
    volumes:
      - ${MY_VAR}:/test
    command: -c 'sleep 100000'

这里的最终目标是使主机上的目录可从 docker 容器访问(用于日志等)。该目录应由环境变量设置。在 docker-compose.yml 中设置目录确实有效,但不适用于我的用例。

最佳答案

似乎一种解决方法是从字符串中删除第一个 / 并将其添加到 docker-compose.yml 中。

docker-compose.yml:

version: '2'
services:
  test:
    build:
      context: .
    image: test
    environment:
      - MY_VAR
    volumes:
      - /${MY_VAR}:/test # added '/' to the beginning of the line
    command: -c 'sleep 100000'

然后启动容器:

export MY_VAR=my/path; docker-compose up  # removed the '/' from the beginning of the path.

这看起来更像是一个“幸运”的解决方法,而不是一个完美的解决方案,因为当我在其他系统上构建它时,我将不得不提醒自己删除 /。可行但有点烦人。也许有人有更好的主意。

关于bash - 如何强制 Git for Windows 的 bash-shell 不将路径字符串转换为 Windows 路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52944738/

相关文章:

linux - PATH 的导出不是永久性的

linux - 脚本在 Bash 和 cron 执行时产生不同的结果

linux - 与 BC 合作

mysql - 在 AWS Elastic Beanstalk 上使用 Docker 编写 NodeJs 和 MySQL

java - Ant 使用了错误的 Java 编译器,但认为它是正确的

Kubernetes - 所有 Pod 的共享环境变量

perl - 使用系统命令将 stdout 和 stderr 输出重定向到文件在 perl 中不起作用

macos - 关闭 screen 时 Gnu screen 要求输入密码 : what password does it want?

docker - 将 docker 卷导出到另一台机器

security -/var/run/docker.sock 的 Docker 安全风险是什么?