3

I'm using the bash shell provided by Git for Windows for Docker toolbox for Windows. I want to export a string representing a unix path to a environment variable to then use in a docker container. Something like:

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

The problem is that in my container the variable will be something like:

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

So it seems the shell (my guess) recognizes the string as a path and converts it to windows format. Is there a way to stop this?

I've attempted to use MSYS_NO_PATHCONV=1:

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

But it did not have any effect.

I don't think it's an issue with my docker-compose and dockerfile but I'll attach them if someone is interested.

My Dockerfile:

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

My docker-compose.yml:

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

The Final goal here is to make a directory on the host machine accessible from the docker container (for logs and such). The directory should be set by an environment variable. Setting the directory in the docker-compose.yml does work, just not for my use case.

Christian Eriksson
  • 2,038
  • 2
  • 22
  • 28
  • maybe a duplicate : https://stackoverflow.com/questions/34647591/passing-windows-slash-based-parameters-to-a-program-from-bash-script – Nahuel Fouilleul Oct 23 '18 at 09:12
  • or this: https://stackoverflow.com/questions/7250130/how-to-stop-mingw-and-msys-from-mangling-path-names-given-at-the-command-line – Nahuel Fouilleul Oct 23 '18 at 09:22

2 Answers2

1

If you want your command docker-compose up to be run with MSYS_NO_PATHCONV=1; you have two options:

  • export LOG_PATH=/c/Windows; export MSYS_NO_PATHCONV=1; docker-compose up This will affect your bash session as the variable is exported

  • export LOG_PATH=/c/Windows; MSYS_NO_PATHCONV=1 docker-compose up; (note I removed one semi-colon intentionally) This will set MSYS_NO_PATHCONV only in the context of the command to run

Test it with:

$ export LOG_PATH=/c/Windows ; cmd "/c echo %LOG_PATH%";
C:/Windows  --> Fails

$ export LOG_PATH=/c/Windows ; MSYS_NO_PATHCONV=1 cmd "/c echo %LOG_PATH%"
/c/Windows --> Success

$ export LOG_PATH=/c/Windows ; export MSYS_NO_PATHCONV=1; cmd "/c echo %LOG_PATH%";
/c/Windows --> Success but MSYS_NO_PATHCONV is now "permanently" set 
Gerardo Grignoli
  • 14,058
  • 7
  • 57
  • 68
0

Seems a workaround is to remove the first / from the string and add it in the docker-compose.yml instead.

new 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'

and then starting the container with:

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

This does seem more like a "lucky" workaround than a perfect solution as when I'll build this on other systems I'll have to remind myself to remove the /. Doable but a bit annoying. Maybe someone has a better idea.

Christian Eriksson
  • 2,038
  • 2
  • 22
  • 28