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

时间:2018-10-23 08:42:20

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

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

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

问题在于,在我的容器中,变量将类似于:

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

因此,外壳程序(我的猜测)似乎将字符串识别为路径并将其转换为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中设置目录确实可以,但不适用于我的用例。

1 个答案:

答案 0 :(得分:0)

似乎一种解决方法是从字符串中删除第一个/,然后将其添加到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.

这似乎比完全的解决方案更像是一种“幸运的”解决方法,因为当我将其构建在其他系统上时,我必须提醒自己删除/。可行,但有点烦人。也许有人有更好的主意。