Skip to content
This repository has been archived by the owner on Sep 26, 2021. It is now read-only.

Invalid command to connect Windows Command Prompt #3023

Closed
RaeesBhatti opened this issue Feb 9, 2016 · 14 comments
Closed

Invalid command to connect Windows Command Prompt #3023

RaeesBhatti opened this issue Feb 9, 2016 · 14 comments
Labels

Comments

@RaeesBhatti
Copy link

Using Docker Machine docker-machine version 0.5.6, build 61388e9 on Windows 10 x64.
The command returned by docker-machine env --shell cmd default is

SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=C:\Users\raees\.docker\machine\machines\default
SET DOCKER_MACHINE_NAME=default
REM Run this command to configure your shell:
REM     FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO %i

But if I execute the last one

FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO %i

It returns an error i was unexpected at this time.. Which means that the FOR loop is not valid.

@jeanlaurent
Copy link
Member

There are a couple of changes recently on this. Does the issue comes out with 0.6.0 version ?

@RaeesBhatti
Copy link
Author

After the upgrade, when I executed docker-machine env --shell cmd default, it said that it couldnt connect to the Docker virtual machine. So, I deleted the previous one and started Docker Quick Start Terminal and it also said that it couldnt start the VM. So, I deleted the failed VM and Hostonly interfaces in VirtualBox and started Quick Start Terminal again and that is failing also.

Running pre-create checks...
Creating machine...
(default) Copying C:\Users\raees\.docker\machine\cache\boot2docker.iso to C:\Users\raees\.docker\machine\machines\default\boot2docker.iso...
(default) Creating VirtualBox VM...
(default) Creating SSH key...
(default) Starting the VM...
(default) Check network to re-create if needed...
(default) Windows might ask for the permission to create a network adapter. Sometimes, such confirmation window is minimized in the taskbar.
(default) Found a new host-only adapter: "VirtualBox Host-Only Ethernet Adapter"
(default) Windows might ask for the permission to configure a network adapter. Sometimes, such confirmation window is minimized in the taskbar.
(default) Windows might ask for the permission to configure a dhcp server. Sometimes, such confirmation window is minimized in the taskbar.
Error creating machine: Error in driver during machine creation: Unable to start the VM: exit status 1
Looks like something went wrong... Press any key to continue...

@RaeesBhatti
Copy link
Author

Its on docker-machine version 0.6.0, build e27fb87

@jeanlaurent
Copy link
Member

Ouch, an upgrade bug.

You should probably clean that up ( in docker-machine and virtualbox ), and instead of launching Docker QuickStart, runs docker-machine -D create -d virtualbox default and check the details there

@RaeesBhatti
Copy link
Author

I had to do a whole lot of clean up. Uninstalled Docker Toolbox and VirtualBox, deleted cleaned up temp dirs and deleted .docker directory. Reinstalled Docker Toolbox and Docker is running fine now. Luckily I had no important stuff in Windows installation
I think it was a certificates issue, as I remember that when I tried to execute docker-machine env --shell cmd default for the first time after upgrade, it said that it couldn't verify the certificates and I should recreate them, so, I executed the command it suggested to recreate certificates and that didn't work also or maybe it broke it permanently.

docker-machine env --shell cmd default return

SET DOCKER_TLS_VERIFY=1
SET DOCKER_HOST=tcp://192.168.99.100:2376
SET DOCKER_CERT_PATH=C:\Users\raees\.docker\machine\machines\default
SET DOCKER_MACHINE_NAME=default
REM Run this command to configure your shell:
REM     FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO %i

and when I execute

FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO %i

It still returns i was unexpected at this time.

@jeanlaurent
Copy link
Member

@raeesiqbal your current shell is cmd.exe right ?

Does the output of docker-machine env works ? Without the --cmd shell default

@nathanleclaire
Copy link
Contributor

cc @StefanScherer please take a look at this and let us know if you can dupe!

@StefanScherer
Copy link
Member

@raeesiqbal Are you running the FOR loop interactively or in a Batch script?

Using a cmd.exe shell in a Win10x64 machine with the FOR loop

FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO %i

everything works well:

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\vagrant>FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO %i

C:\Users\vagrant>SET DOCKER_TLS_VERIFY=1

C:\Users\vagrant>SET DOCKER_HOST=tcp://192.168.99.100:2376

C:\Users\vagrant>SET DOCKER_CERT_PATH=C:\Users\vagrant\.docker\machine\machines\default

C:\Users\vagrant>SET DOCKER_MACHINE_NAME=default

C:\Users\vagrant>REM Run this command to configure your shell:

C:\Users\vagrant>REM    FOR /f "tokens=*" %i IN ('"C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env --shell cmd default') DO %i

C:\Users\vagrant>set dock
DOCKER_CERT_PATH=C:\Users\vagrant\.docker\machine\machines\default
DOCKER_HOST=tcp://192.168.99.100:2376
DOCKER_MACHINE_NAME=default
DOCKER_TLS_VERIFY=1

C:\Users\vagrant>

But using this FOR loop FOR /f "tokens=*" %i IN ('docker-machine env --shell cmd default') DO %i in a .bat file it doesn't work:

C:\Users\vagrant>notepad setenv.bat

C:\Users\vagrant>setenv.bat
i was unexpected at this time.

C:\Users\vagrant>FOR /f "tokens=*" i

The problem is that in a .bat script you have to use two %% signs for these variables. So changing the setenv.bat script to

FOR /f "tokens=*" %%i IN ('docker-machine env --shell cmd default') DO %%i

works.

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\vagrant>setenv.bat

C:\Users\vagrant>FOR /F "tokens=*" %i IN ('docker-machine env --shell cmd default') DO %i

C:\Users\vagrant>SET DOCKER_TLS_VERIFY=1

C:\Users\vagrant>SET DOCKER_HOST=tcp://192.168.99.100:2376

C:\Users\vagrant>SET DOCKER_CERT_PATH=C:\Users\vagrant\.docker\machine\machines\default

C:\Users\vagrant>SET DOCKER_MACHINE_NAME=default

C:\Users\vagrant>REM Run this command to configure your shell:

C:\Users\vagrant>REM    FOR /f "tokens=*" %i IN ('"C:\ProgramData\chocolatey\lib\docker-machine\bin\docker-machine.exe" env --shell cmd default') DO %i

C:\Users\vagrant>set doc
DOCKER_CERT_PATH=C:\Users\vagrant\.docker\machine\machines\default
DOCKER_HOST=tcp://192.168.99.100:2376
DOCKER_MACHINE_NAME=default
DOCKER_TLS_VERIFY=1

But we can't just add the second % sign as this does not work interactively in a cmd.exe shell:

Microsoft Windows [Version 10.0.10586]
(c) 2015 Microsoft Corporation. All rights reserved.

C:\Users\vagrant>FOR /f "tokens=*" %%i IN ('docker-machine env default') DO %%i
%%i was unexpected at this time.

C:\Users\vagrant>

PS: With docker-machine 0.6.0 you can normally skip the --shell cmd

  • CMD interactive
    • FOR /f "tokens=*" %i IN ('docker-machine env default') DO %i
  • CMD Batch scripts
    • FOR /f "tokens=*" %%i IN ('docker-machine env default') DO %%i
  • PowerShell:
    • docker-machine env default | iex

@RaeesBhatti
Copy link
Author

Yes, my current shell is cmd.exe. And yes I'm actually using a batch script to configure ssh-agent in the background whenever a new cmd is spawned. I though it was a good idea to configure docker within that also, and I was using single % for variables in batch.
Now when I execute FOR /f "tokens=*" %i IN ('docker-machine env default') DO %i, it seems to work fine in the interactive shell.
But when I put FOR /f "tokens=*" %%i IN ('docker-machine env default') DO %%i in the batch script. It just hangs there, and CPU usage goes up to ~50%. Maybe because its stuck in an infinite loop?

@StefanScherer
Copy link
Member

@raeesiqbal Don't know. I have tested a small batch script and a shortcut as follows:

Shortcut target: C:\Windows\System32\cmd.exe /K %USERPROFILE%\setenv.bat

setenv.bat:

@echo off
echo Set Docker envs
FOR /f "tokens=*" %%i IN ('docker-machine env default') DO %%i
echo Done!

Opening the CMD shell works fine and using docker works.

cmd-setenv-bat-docker-env

What is your setup?

@RaeesBhatti
Copy link
Author

Ah.... found the problem. It worked just fine for me when executed after the cmd had loaded. But as weird as it gets, FOR runs a new command processor for every loop! and since I was using FOR /f "tokens=*" %%i IN ('docker-machine env default') DO %%i in the startup script of cmd. It was causing an infinite loop.
So, here is what I did.

IF DEFINED DOCKER_MACHINE_EXECUTING (GOTO :eod)
set DOCKER_MACHINE_EXECUTING=1

:configureDocker
 FOR /f "tokens=*" %%i IN ('docker-machine env default') DO %%i

:eod

@RaeesBhatti
Copy link
Author

@StefanScherer and @jeanlaurent thank you so much for your time.

@matthuisman
Copy link

matthuisman commented Sep 7, 2016

I like to use

@echo off
echo Set Docker envs
FOR /f "tokens=*" %%i IN ('docker-machine env %1') DO %%i
echo Done!

That way I can do

setenv (will be default)
or
setenv machine2

etc

@Lissuan
Copy link

Lissuan commented Oct 16, 2017

It works for me thanks ...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

6 participants