0

Docker terminal command

Trying to fallow docker tutorial [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"

I have issue with the above line of code. I am running this line in Cmder, GitBash and Windows 10 PowerShell terminal.

Console 1 - Cmder - ERROR:

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

Console 2 - GitBash - `ERROR`:

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

Possible reasons:

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

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

Console 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 logs

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

Anyone make it to work in GitBash or Cmder before or is this my Env issue? Guesing that most of you will run it on Mack OS or Linux distro instead. Perhaps vegrant + docker is the way to go? What is your setup on Windows?

Back to Console 2 - GitBash - Trying to resolve possible path issue:

So fallowing the above problem i modify the line adding aditional "/" (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

Now the docker container was created but did not run... see the below issue

$ docker logs

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.

Now we have ... error Couldn't find a package.json file in "/app" anyone solve the issue in gitbash?

DevWL
  • 11,946
  • 5
  • 71
  • 69
  • It looks like that `docker run` command is trying to run Node on the contents of the current directory...would [installing Node locally](https://nodejs.org/en/download/) be easier than trying to insert Docker into the mix? – David Maze Jan 10 '21 at 18:21

2 Answers2

1

Console 2 - GITBASH - SOLUTION

According to How to stop MinGW and MSYS from mangling path names given at the command line we could make it work on Git bash by running MSYS_NO_PATHCONV=1 Before the actual command. This will disable the path translation for that command. There is also a way to tur off path conversion globally, but because I don't know the exact consequences I choose to add this bit before each command.

Console 2 - GitBash - OK

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

docker logs 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

Console 1 - CMDER - /app not found - container created but not runing

To mekce this command work in cmndr terminal I had to add `/` before the `$(pwd)` as you see bellow:
docker run -dp 3000:3000 -w /app -v /$(pwd):/app node:12-alpine sh -c "yarn install && yarn run dev"

Unfortuntly this did not totaly worked. This time it run and no error was shown but the file under the /app path was not found. DOcker container was created but dod not show up under 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.
DevWL
  • 11,946
  • 5
  • 71
  • 69
0

The problem is executables on windows wants to see path(s) as "windows" (e.g. c:\whatever\is\here) but instead what they get the "posix" equivalent ( e.g. c:/whatever/is/here). Even though you run your docker executable in "git bash" the underlying executable is still a windows version of docker which makes it hiccup.

On Powershell this works because Powershell creates the path as it should (windows version) on CMD the shell does not understand this command.

following this solution, you can modify the command to run like this:

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"

and it might actually work.

as for your question about my configuration on windows. I work both on windows and linux but I strive to understand and change the commands to suit me and not follow blindly the commands I've been given in some tutorial (it's a habit worth acquiring)

Koko Jumbo
  • 138
  • 5