Skip to content

Instantly share code, notes, and snippets.

@jwilson8767
Last active May 14, 2020 17:17
Show Gist options
  • Save jwilson8767/00a46f5ca63327d5bfd802f87b702c8d to your computer and use it in GitHub Desktop.
Save jwilson8767/00a46f5ca63327d5bfd802f87b702c8d to your computer and use it in GitHub Desktop.
Docker Toolbox for Windows and Windows Subsystem for Linux (aka Bash on Windows)
#
# This script installs and configures WSL to work with Docker Toolbox for Windows.
# 1. Install WSL (check out [bowinstaller](https://github.com/xezpeleta/bowinstaller) for programmatic installation.
# 2. Run the contents of this script in Bash. (copy and paste works fine, no need to save)
#
sudo -sEH << 'EOM'
# Install the docker client and docker-compose
apt-get update && apt-get install -y curl ca-certificates
curl -sSL https://get.docker.com/ | sh
curl -L "https://github.com/docker/compose/releases/download/1.11.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
# Add the current user to the docker group.
usermod -aG docker $(id -un)
# Symlink /c/ to /mnt/c/ as Docker Toolbox is expects /c/ path mappings.
[[ ! -e /c/ ]] && ln -s /mnt/c /
EOM
#Set docker-machine to run on terminal start.
cat >> ~/.bashrc << 'EOM'
# Start the docker machine
export VBOX_MSI_INSTALL_PATH='/c/Program Files/Oracle/VirtualBox/'
pushd '/c/Program Files/Docker Toolbox/' > /dev/null
./start.sh exit
# Get env variables from docker-machine, convert paths, ignore comments, and strip double quotes.
$(./docker-machine.exe env --shell bash | sed 's/C:/\/c/' | sed 's/\\/\//g' | sed 's:#.*$::g' | sed 's/"//g' )
popd > /dev/null
# Change /mnt/c/ to /c/ in current working directory path
cd $(pwd | sed 's/\/mnt\/c\//\/c\//')
EOM
@strarsis
Copy link

strarsis commented Jan 16, 2018

There is a nice docker-env wrapper script that does the ENV/export conversion (well enough, had no issues yet):
https://gist.github.com/mmarchini/bc9df7b82127fea13612edf8bffdf96f

I made a similar HOWTO (though inferior to this approach I think), maybe it is still helpful for you for fixing some other issues:
https://gist.github.com/strarsis/44ded0d254066d9cb125ebbb04650d6c

@austinkelleher
Copy link

austinkelleher commented Feb 1, 2018

I am also having trouble with volumes. The files are missing when using docker-compose up. Example:

version: '3'
services:
  myservice:
    image: some/image
    build .
    volumes:
     - .:/usr/my-service
     - /usr/my-service/node_modules

Example Dockerfile:

FROM node:8.9.4-alpine

ENV HOME=/usr/my-service
RUN mkdir -p $HOME
WORKDIR $HOME

ADD package.json $HOME
ADD package-lock.json $HOME

RUN npm install --silent

ADD . $HOME

EXPOSE 8080

CMD npm run start:docker

Any ideas? /cc @fishnux @strarsis @jwilson8767

@schindld
Copy link

A recent commit in docker toolbox breaks this. Two changes in this commit are problematic.

  1. They no longer hard-code the docker toolbox and docker-machine.exe paths. Simple fix is to do export DOCKER_TOOLBOX_INSTALL_PATH='/c/Program Files/Docker Toolbox' before calling ./start.sh exit.
  2. On line 80 of the commit, they convert the exports to SETX syntax, which won't work in wsl. Not sure of a good fix for this. Personally, I just deleted the line from my start.sh, but I'd rather have a fix in my .bashrc without touching start.sh.

@carlin-q-scott
Copy link

Unfortunately, start.sh uses a windows path to docker-machine.exe. I fixed it locally to use which to find the executable but then that exposed another issue: you cannot run docker-machine.exe commands without the variables set by docker-machine.exe --shell bash. So you can't run the command that sets the variables that you need to run the command.

@sabarnix
Copy link

source <(docker-machine.exe env default --shell bash | sed 's?\\?/?g;s?C:/?/mnt/c/?g') run this before docker-compose.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment