Running Docker containers on Bash on Windows

When the Windows Subsystem for Linux (WSL) – or, as most people even at Microsoft often refer to it – Bash on Ubuntu on Windows – was announced on Microsoft’s Build conference 2016, a world of new tools opened up to us Windows devs. Personally, I love being able to choose between PowerShell, Bash or plain old cmd when I want to script something. And it’s always bugged me that I couldn’t get Docker working from Bash on Windows – until now.

The original title of this post was “Running Docker from Bash on Windows”, but that would have been a slight overstatement. Docker requires access to quite a of lot system calls which aren’t necessarily all implemented on Windows, so getting the engine running under the WSL is probably not so easy. Instead, we’ll run the Docker Engine on Windows, and connect to it from Bash. This also has the advantage that you can start a container from PowerShell and interact with it from Bash, or the other way around – in other words, your computer will still feel just like one machine.

Here’s how to do it:

1. Install Docker on Windows

To install the Docker engine on Windows, just go to docker.com and download the appropriate distribution. Also, make sure hardware virtualization is enabled and Hyper-V is installed, lest the engine won’t start.

Shortcut: Install Windows 10 Creators Update

With Windows 10 Creators Update*, accomplishing all of this has become a lot simpler, since it allows you to run Windows executables from Bash. Just add these two lines to your .bashrc and you’re done!

You can now run docker --version from Bash, and you don’t even have to read the rest of this blog post :)

*) Windows 10 Creators Update is available to Insiders since April 11, 2017, and will be released to the public on April 25, 2017.

Making it work on Windows 10 Anniversary Edition

To install Docker on the WSL, you’ll need to jump through a few more hoops. There’s a description for Ubuntu in general here, which works for the WSL as well, with the exceptions of some of the optional steps. Here’s what I did:

Of course, there’s also the option of downloading and extracting the binaries we’ll need, and put them somewhere in your PATH. There are instructions here for how to get the latest version.

Where did that get us?

We now actually have the Docker engine installed on both Windows and the WSL, but it isn’t started on either. The Windows installer helpfully created a Docker shortcut on the desktop and/or in the Start menu – use that to start the Docker engine. Then, you can try running e.g. docker images from PowerShell and from Bash:

PowerShell:

We haven’t created any images yet, so that’s fine.

Bash:

Clearly unsatisfying. But with one or two extra steps, we’ll get it all working.

2. Connect Docker on WSL to Docker on Windows

Running docker against an engine on a different machine is actually quite easy, as Docker can expose a TCP endpoint which the CLI can attach to.

Note: In an update to Docker that was pushed shortly after first publishing this post, this TCP endpoint is turned off by default (thanks Mark for the heads-up!); to activate it, right-click the Docker icon in your taskbar and choose Settings, and tick the box next to “Expose daemon on tcp://localhost:2375 without TLS” (make sure you understand the risks).

With that done, all we need to do is instruct the CLI under Bash to connect to the engine running under Windows instead of to the non-existing engine running under Bash, like this:

Much better!

There are two ways to make this permanent – either add an alias for the above command, or better yet (thanks Dave!), export an environment variable which instructs Docker where to find the host engine:

Now, running docker commands from Bash works just like they’re supposed to.
Mission accomplished!


The versions used in this post

Of course, things like these might always have dependencies of the versions of the tools you’re using. In this blog post, I’ve been using the following versions:

Windows
Windows 10 Pro Anniversary Edition (Version 1607, OS Build 14393.1066)
Windows 10 Pro Creators Update (Version 1703, OS Build 15063.138)
WSL Ubuntu
14.04.5 LTS
Docker on Windows
17.03.1-ce, build c6d412e
Docker on Ubuntu
17.03.1-ce, build c6d412e

47 Comments

  1. Hi !

    Thank you for your update with windows Creator’s update, but I would just add something, WSL can launch windows executables but you have to provide the extension, so to launch docker you would have to type docker.exe which is not very practical.

    Just add aliases for docker.exe and docker-compose.exe and you’re done :)

  2. Steinar Flatland

    Worked perfectly. Great stuff. Thank you!

  3. Dave C.

    Instead of
    echo “alias docker=’docker -H tcp://0.0.0.0:2375′” >> ~/.bashrc

    Do
    echo “export DOCKER_HOST=”tcp://0.0.0.0:2375” >> ~/.bashrc

    And regarding Romain Prevost’s alias suggestion: I’ve run into some issues using docker-compose with this setup. I found it easier to install docker-ce and docker-compose under WSL then just exporting the variable above.

    • strarsis

      I got docker running in Windows 10 Home Premium and WSL up to the point where I can list containers / pull images,
      see these notes for reproducing the setup: https://gist.github.com/strarsis/44ded0d254066d9cb125ebbb04650d6c

      However, starting containers with docker-compose doesn’t work yet, I get an error message like:

      ERROR: for Cannot start service php: oci runtime error: container_linux.go:262: starting container process caused “chdir to cwd (\”\”) set in config.json failed: no such file or directory”

      I haven’t solved this issue yet, I tried several things, like wrapping the windows-native docker.exe in shell.
      How can I fix this with my current setup?

  4. Great blog, I could successfully install Docker on windows.

  5. Mark C

    Trying this on May 15, 2017, using docker version 17.03.1-ce-win12 (12058), between parts 2 and 3 it was also necessary to select the following option from Docker > Settings > General:
    “Expose daemon on tcp://localhost:2375 without TLS”.
    Note the warning underneath that option, in case it is important to you:
    “Exposing daemon to TCP without TLS helps legacy clients connect to teh daemon. It also makes yourself vulnerable to remote code execution attacks. Use with caution.”

    Without doing that, you continue to get the connection error, “Cannot connect to the Docker daemon…”

    • Tomas Lycken

      I realized this would probably be necessary when I read the release note about this update, but I hadn’t had time to experiment with it and come up with a solution. Thanks for posting back here!

      • Leo

        Hey Guys,
        I wrote a small script, that gets loaded in bashrc, it mounts /mnt/c to /c so you can do host mounts, and it also creates an alias for docker-machine to generate the right docker-machine env exports, including the secured port and certs path. Can you guys try it out see if it’s useful ?
        https://github.com/leohinojosa/wsldocker
        thanks!

  6. Ronny Carlansson

    A very good thread! Been searching for this a while :)
    Had the same problem as Mark C – “Expose daemon on tcp://localhost:2375 without TLS”.
    Took a while before I read his comment…

    Best,

    • Tomas Lycken

      Glad you found it helpful!

      I’ve updated the post to make this point clearer; hopefully future readers won’t have to stumble on this.

  7. Peter

    Thanks for the really great article by the way.
    Looked like a very simple few steps to install docker, however I ran into a few issues.
    I have windows 10 creator update installed on my machine. However I only have the Home edition and therefore cannot install the windows version docker. I instead followed your instructions to install docker on WSL.
    I have set the DOCKER_HOST variable.
    However when I try and run docker ps I get the following error:
    Cannot connect to the Docker daemon at tcp://0.0.0.0:2375. Is the docker daemon running?
    If this is an issue with the TCP endpoint being turned off then how would I solve this without having docker installed on Windows?

  8. Leo

    The Creators Update approach works great! But as Romain Prevost mentioned I had to locate the program with docker.exe or docker-compose.exe instead.

    Also I was cloning my repositories into the Linux filesystem, which causes a warning about “Unable to translate current working directory. Using C:\WINDOWS\system32” because your windows programs cannot open Linux files. It’s a WSL limitation, but I really think it should just error out instead of giving a warning.

  9. Adrian

    I installed docker under WSL as you had it, and this worked fine, running the Docker engine installed under Windows. Wanting to remove the WSL installation and just run the Windows Docker from Bash with just the path additions you mentioned (when running the CU), I tried to “sudo apt remove docker-ce”, but this results in an error, and the package is not removed. Can you confirm that you also can’t remove the docker-ce package and that something gets borked when installing Docker under WSL?

  10. Karl

    Working with Windows 10 Home, Creator’s Update, WSL, Docker Toolkit.

    • strarsis

      I also got the same setup (Windows 10 Home, Creator’s Update, WSL, Docker Toolkit).

      Running docker results in error:

      error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/containers/json?all=1: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.

      Also as root (sudo) I get the same error that the client should be elevated.

      In .bashrc:

      # Docker
      export PATH=”$HOME/bin:$HOME/.local/bin:$PATH”
      export PATH=”$PATH:/mnt/c/Program\ Files/Docker\ Toolbox”
      alias docker=docker.exe
      alias docker-machine=docker-machine.exe

      I also have same issues in normal windows cmd, also using docker.exe.
      Created a new docker-machine using docker-machine create default.
      Copied + evaluated the commands from docker-machine env.

      Docker error in cmd with docker ps:
      could not read CA certificate “C:\\Users\\\\.docker\\ca.pem”: open C:\Users\\.docker\ca.pem: The system cannot find the file specified.

      Regenerated the certs using docker-machine regenerate-certs [y].
      Still same error, cannot find the ca.pem.

      Thanks in advance and with best regards!

  11. Robert Jackson

    Thanks a lot. Running docker on bash shell under windows is fantastic. I also found it easier to just use the second method.

  12. Patrick Roza

    I prefer using the docker-ce package on bash for windows even in the creators update because running windows executables from bash do not support signals like SIGINT, e.g ctrl+c (BREAK), which I find important in my docker/docker-compos endevours ;-)

  13. Brian Lloyd-Newberry

    Just to save some people time, in the newer version of Docker for Windows it doesn’t listen on tcp://0.0.0.0:2375 or tcp://localhost:2375 by default. You need to go into the settings and enable it for the windows docker daemon by checking the “Expose daemon on tcp://localhost:2375 without TLS’ option. Note. this does open things up for a remote execution attack. The other option is setting up SSL keys and TLS, but that is for another day.

    You’ll get: the following:
    > docker run hello-world
    docker: Cannot connect to the Docker daemon at tcp://0.0.0.0:2375. Is the docker daemon running?.
    See ‘docker run –help’.

  14. Jeremiah

    not sure what I am doing wrong here, used the ‘shortcut’ as I am on creators update, but when I run docker.exe from bash, i get an error: Unable to translate current working directory. Using C:\windows\system32

  15. Patrick

    If you want to actually run the docker instances on WSL (you’ll get better performance) you should modify this process so that after installing docker on WSL you change the docker socket to use a loopback TCP socket instead of a *nix socket file as WSL currently doesn’t support *nix socket files. then you can either connect using the windows docker or you can just use it from command line WSL.

  16. Anna Bell

    This does not work for me, I have the creator update with the latest bash environment (16.04). When I add those 2 lines to .bashrc and after restart of bash I try to run docker –version, it tells me it can’t find docker. How to solve it?

    • Tomas Lycken

      What happens if you run docker.exe --version?

      Although Creators Update lets us run windows executables from the WSL, it doesn’t automatically resolve file extensions the way Windows does. You can add alias docker=docker.exe to simulate the behavior for this use case, though :)

    • Brad C

      I have the same problem. It doesn’t work for me whether I run “docker –version” or “docker.exe –version”.

  17. Dan

    Why do volumes not seem to work at all with docker-compose?

    Soemthing to do with the C:/ vs /mnt/c/ paths?

    • Dan

      ERROR: for client Cannot start service client: oci runtime error: container_linux.go:262: starting container process caused “process_linux.go:339: container init caused \”ro
      otfs_linux.go:57: mounting \\\”/mnt/c/var/www/react-app.local/client/package.json\\\” to rootfs \\\”/var/lib/docker/overlay2/47d9e084699b9b8dd832916012c288853d46558cd4a4521c1
      80a7b4caf32bbf4/merged\\\” at \\\”/var/lib/docker/overlay2/47d9e084699b9b8dd832916012c288853d46558cd4a4521c180a7b4caf32bbf4/merged/var/www/client/package.json\\\” caused \\\”
      not a directory\\\”\””
      : Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type

      • Tomas Lycken

        Yes, probably a path/file system issue. I haven’t explored docker-compose under WSL deeply; I tend to want to use bash when I script one-offs and try stuff out, but once I have it in my compose file I see no reason not to start the stack directly from Windows.

        • strarsis

          docker and docker-compose run well on WSL for me. But docker-compose volumes don’t work correctly.
          On the other hand, docker.exe and docker-compose.exe don’t work correctly in WSL,
          but docker-compose.exe volumes probably would.

  18. Andrew Lozoya

    I dont understand the implications of doing this, just so you can use bash… isnt the daemon still running on a hyperv VM which in itself is a linux kernel? Or am I way off base.

    • Tomas Lycken

      The reason for doing this is not to make Docker work any differently – you’re entirely correct, it’s just the same daemon still running on HyperV as if you use Docker from powershell or cmd. Rather, it’s to enable *using* Docker in bash scripts, e.g. for build or test orchestration. It’s of course possible to do those things in a Docker container as well, but it’s a little tricky to set it up so that you can work with Docker from within Docker, so I find this way much easier.

  19. Andrew Lozoya

    Where is the .bashrc actually located? I am assuming you meant %localappdata%\Lxss\rootfs
    but when i add the lines
    PATH=”$HOME/bin:$HOME/.local/bin:$PATH”
    PATH=”$PATH:/mnt/c/Program\ Files/Docker/Docker/resources/bin”

    i get thisbash: /root/.bashrc: Input/output error
    root@XXXX:/mnt/c/Users/lozoyaa/Desktop# docker –version
    The program ‘docker’ is currently not installed. You can install it by typing:
    apt install docker.io
    root@XXX:/mnt/c/Users/lozoyaa/Desktop#

  20. Andriy Minchenko

    Worked perfectly. (Windows 10 Pro, version 1607, build 14393.1593 +Docker version 17.06.2-ce, build cec0b72 )
    Thanks a lot!

  21. Hello, I had a great help from your blog!
    I tested Windows 10 1703 June 2017 update & WSL Ubuntu 16.04.2 LTS, and it worked very well!

    Would you mind if I introduce this method to Ubuntu Korea Seminar? I will definitely write down this URL page.
    Many people have asked about how to run docker Bash on Ubuntu on Windows. I think more people will use Bash on Windows by using this method.
    I appreciate again your solution and effort!!

  22. Enes

    Hi guys,

    I’ve did everything as described in the article and docker is working just fine. But docker-compose is not recognized as the command…is there anything special that I need to do for docker-compose. When using powershell this command is recognized and it is working.

    Thanks,

  23. Matt

    GOD DAMN I LOVE YOU LOL, this was exactly what I needed to know to solve my issue.

  24. Rashid Mahmood

    Hi,

    Thank you it is really helpful.

  25. Marian

    When I searched for “install docker on Ubuntu on Windows” I was actually hoping to find info that “it just works!” (no docker for windows install, just Ubuntu).
    That doesn’t seem to be the case :)
    My problem is I also have VirtualBox for different projects at work. And VB stops working after enabling Hyper-V.
    So… I was hoping that I could simply install docker in my WSL – like I would simply be booted under another OS (Ubuntu) – and that it wouldn’t have anything to do with Windows and it would simply work, without enabling Hyper-V in Windows – which causes a blue screen once I try to start up a VM from VirtualBox.
    When is VirtualBox going to fix this? :(

    • Tomas Lycken

      Well, WSL is a real Ubuntu system, but it doesn’t have all the features of Ubuntu installed on bare metal – virtualization is one of the things it doesn’t have. So, running docker natively under WSL is probably not going to happen anytime soon. And although Ubuntu doesn’t have anything named Hyper-V, you still need to enable hardware virtualization to enable Docker on a native Ubuntu host – so if VirtualBox doesn’t support “nested” virtualization, running Docker inside VirtualBox is going to be difficult.

      Are you successfully running Docker in Windows inside your virtual machines in VirtualBox? If not, maybe it’s possible to run Docker on the host system, and then mount the Docker socket onto the VirtualBox guest (just as you’d do to enable running Docker inside Docker containers)?

  26. Brian Bulkowski

    Hi – I didn’t read through all the responses, but since you’re so highly ranked on google for this problem you might want to update the instructions.

    First, you don’t need to change the extra environment variables, you do need to link or rename ( I prefer linking ) docker to docker. After you install docker, the environment variables are set also in WSL. One can easily test this by, with a fresh install, attempting to run docker.exe without the environment variables. Worked! Now I only need to figure out the best way to soft-link – I’m assuming I put files in /usr/local/bin like I usually would.

    Second, there’s a lot of comments about changing the default listener, so I suppose those are correct too.

    Thanks for the blog post – this tech is moving fast. I hope you find time to update.

    • Tomas Lycken

      Thanks for these comments! Yes, keeping this post up-to-date with instructions that actually work is important to me, so I do want to take the time to get this right if there’s an error somewhere.

      Just to be sure: you did notice that most of the instructions here are not for the current version of Windows, right? With Creators Update, you really don’t need to do anything – you can just run docker.exe and it works, since they have greatly improved WSL/Windows interop over Anniversary Edition. (You might have to add the Docker installation directory to your PATH in WSL, but I’m not sure – it might be there automatically. You might also, as I suggest in the post, want to alias docker=docker.exe just to make it “feel” more like on Linux – but it’s by no means necessary to be able to run Docker commands.)

      Unfortunately, I don’t have access to a system with Anniversary Edition now, so I can’t test anything there, but are you saying that you don’t need echo "export DOCKER_HOST='tcp://0.0.0.0:2375'" >> ~/.bashrc anymore? Or what exactly is it that I should change? :)

  27. Alexander Wagner

    First of all, I found that using Windows installer from docker page already adds “C:\Program Files\Docker\Docker\Resources\bin” to PATH although it should actually be “C:\Program Files\Docker\Docker\resources\bin”, i.e. lower case “r” in “resources”. Since WSL inherits PATH form Windows, it worked for me without adding extra pointers in .bashrc.

    Second, I am surprised that it seems to have worked for some people here using Windows 10 Home. I tried it and it didn’t work as Docker requires Hyper-V enabled which apparently is only possible under Windows Pro and Enterprise.

    Third, I found that you get the error “Unable to translate current working directory. Using C:\WINDOWS\system32” when running Docker in WSL in some directories, e.g. home (~), but not when running it in others, e.g. in “/mnt/c/Program\ Files/Docker/Docker/resources/bin”. This is apparently expected as some directories have no Windows equivalent, see this Github issue https://github.com/Microsoft/BashOnWindows/issues/2011.

    Thanks for the post! Happy dockering.

  28. Sherryl Manalo

    Thanks for your blog post! Exactly what I needed and worked like a charm.

Leave a Reply