Share via


Setting up vscode to use the docker extension with bash for windows as terminal shell

I have been using the vscode with the docker extension on my MAC for quite some time now. I wanted to replicate similar setup on my windows machine, using my preferred bash shell as the integrated vscode terminal shell. After making the configurations described in this post you will be able to execute command like docker-compose from within vscode (using the docker extension) and the docker commands will be executed by vscode in bash for windows as the terminal shell. I use this very often for running docker-compose to check application is working fine when composed (inner loop of containerized development) on my dev machine, and after verifying that it is working fine I commit and push / submit pull request to the git branch from where the image builds and other flows are triggered (outerloop of containerized development). The animated gif below shows what the final outcome after the configurations described in the rest of this post are done.

For this post I am assuming that you are using a windows 10 machine, you have vscode installed and you have docker engine running on your windows machine ( using docker for windows or docker-machine or minikube). Following configurations need to be done:

  1. Enable bash for windows : Follow the steps mentioned in Enable bash for Windows. Once you have bash has been configured we can proceed with the next steps.

  2. Installing docker client for bash on windows: Follow the steps mention in Install docker client for bash on windows. The commands executed by me are shown below (please follow link for latest instructions) sudo apt-get update sudo apt-get install apt-transport-https ca-certificates curl software-properties-common curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - sudo apt-key fingerprint 0EBFCD88 sudo apt-get update sudo apt-get install docker-ce

  3. Installing docker-compose for bash on windows: Follow the steps mention in Install docker-compose in bash on windows. The commands executed by me are shown below (please follow link for latest instructions) sudo curl -L https://github.com/docker/compose/releases/download/1.16.1/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose

  4. Configuring bash using .bashrc to connect to the docker daemon running on the windows machine: This step will differ depending on how the docker engine is running on your machine, i.e. whether you are using docker for windows, docker-machine, or minikube. If you dont have any of these then would recommend you install docker for windows following the steps mentioned in the link Install docker for windows. Depending on the engine you will need to make different enteries in your ~/.bashrc file, lets take a look:

    • If using minikube: We first fire the command minikube docker-env command, and then set the corresponding environment variables in our bash for windows ~/.bashrc file. You can execute the following minikube command from cmdshell , powershell or gitbash i.e. from where ever you normally execute minikube commands
      $ minikube docker-env export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="C:\Users\username\.minikube\certs" export DOCKER_API_VERSION="1.23" # Run this command to configure your shell: # eval $(minikube docker-env) Now we set corresponding values in the ~/bashrc file. We will need to change the value of the DOCKER_CERT_PATH, as C:\ is mounted as /mnt/c for bash on windows .
      # value set at bottom of ~/.bashrc export DOCKER_TLS_VERIFY="1" export DOCKER_HOST="tcp://192.168.99.100:2376" export DOCKER_CERT_PATH="/mnt/c/Users/username/.minikube/certs" export DOCKER_API_VERSION="1.23"
    • If using docker for windows: In this case first we can open docker for windows settings and enable the "
      Expose daemon on tcp://localhost:2375 without TLS" option (Note when enabling this you need to aware about possibility of remote execution attacks). Once this option has been enabled you can add the following to the end of the ~/.bashrc file export DOCKER_HOST=tcp://0.0.0.0:2375
    • if using docker-machine: Similar to the minikube steps we will first execute docker-machine env machine-name command, and then set the environment variables in the ~/.bashrc file. $ docker-machine env dmach SET DOCKER_TLS_VERIFY=1 SET DOCKER_HOST=tcp://192.168.99.102:2376 SET DOCKER_CERT_PATH=C:\Users\username\.docker\machine\machines\dmach SET DOCKER_MACHINE_NAME=dmach SET COMPOSE_CONVERT_WINDOWS_PATHS=true REM Run this command to configure your shell: REM @FOR /f "tokens=*" %i IN ('docker-machine env dmach') DO @%i. We will modify the DOCKER_CERT_PATH and then set the environment variables as follows:
      # value set at bottom of ~/.bashrc export DOCKER_TLS_VERIFY=1 export DOCKER_HOST=tcp://192.168.99.102:2376 export DOCKER_CERT_PATH=/mnt/c/Users/username/.docker/machine/machines/dmach export DOCKER_MACHINE_NAME=dmach export COMPOSE_CONVERT_WINDOWS_PATHS=true

    We can verify if we have configured this step correctly by opening a new bash shell and typing docker ps, this should show us the currently running containers.

  5. Installing the vscode docker extension: You can read more about the vscode docker extension at vscode docker extension. To install you can press F1 in vscode and type "ext install vscode-docker". Alternately you can use the vscode extensions tab to install the extension.

  6. Configuring vscode to use bash as the default terminal shell: Press Ctrl+, or go File->Preferences->Settings. Then add the value below to the JSON file "terminal.integrated.shell.windows": "C:\\Windows\\sysnative\\bash.exe". After this step if you open up a new terminal in vscode a bash shell should open up. .

That is it, configuration done. You can now type Ctrl+Shift+P type docker, this will show you the possible docker commands including docker-compose command to execute, once the command is selected it will get executed it will get executed in the vscode bash shell.

Thanks for reading my post. I hope you liked it. Please feel free to write your comments and views about the same over here or at @manisbindra.

Comments

  • Anonymous
    November 04, 2017
    The comment has been removed
    • Anonymous
      November 05, 2017
      Thanks @Justin. As per your suggestion have added link to install docker for windows.