Solve 'Drive has not been shared' error with Docker on Windows

; Date: May 5, 2020

Tags: Docker »»»» Docker for Windows

We often mount folders into a Docker container to ensure data is persistent while letting us freely destroy and recreate the container. But on Windows you might get a head-scratching error message saying "Unhandled exception: Drive has not been shared". The most common advice that might come up when searching the Internet is not about this situation, but about general file sharing in Windows. In this case the error refers to configuration settings in Docker.

In my case I am setting up a database instance. Because databases need to be eternal, and containers are ephemeral, it is extremely important to keep the database data directory outside the database container. That way we can destroy and recreate the container without harming the database.

The command I'm accustomed to running on a Unix/Linux host is:

$ docker run --name db-userauth \
    --env MYSQL_USER=userauth \
    --env MYSQL_PASSWORD=userauth \
    --env MYSQL_DATABASE=userauth \
    --mount type=bind,src=`pwd`/userauth-data,dst=/var/lib/mysql \
    --network authnet -p 3306:3306 \
    --env MYSQL_ROOT_PASSWORD=w0rdw0rd \
    mysql/mysql-server:8.0 \
    --bind_address=0.0.0.0

The --mount option is the important part, and tells Docker to mount the userauth-data directory in the current directory as /var/lib/mysql in the container. For Windows we obviously need to do this a little different, such as hardcode the full path, because Windows does not allow this syntax. But that's a little nitpick compared to this error I received on Windows:

C:\Program Files\Docker\Docker\resources\bin\docker.exe: Error response from daemon: status code not OK but 500: {"Message":"Unhandled exception: Drive has not been shared"}.
See 'C:\Program Files\Docker\Docker\resources\bin\docker.exe run --help'.

The help for the run command is of course useless in giving us advice. Also, much of the advice that comes up is about general file sharing on Windows. But that is about ensuring a drive (or folder) can be accessed over the network using SMB or CIFS services.

The real solution is to enable file sharing in Docker.

Following the advice enable general file sharing will cause you to do a dangerous and unnecessary thing like turning on advanced sharing options for your C drive. Which reminds me, I need to turn that off on my laptop. That's the problem with following random advice you find on the Internet. Sometimes the advice is for a different problem, and you end up doing the wrong thing. In this case if we enable file sharing for our C drive, allowing Everyone to have read/write access because we need Docker to have access, doesn't that mean anyone on our local network can access any file and... er... sounds like a major security problem.

Fortunately that advice did not help with the case above, so I kept searching for answers. What came up is incredibly simple.

The Docker for Windows application has a file sharing setting.

Simply open the Docker dashboard application -- In the Taskbar there is a Docker icon, and right-click that icon you'll see a choice labeled Dashboard. Click on that and you'll be in an application that shows the active containers, and has a settings area.

In the Docker Dashboard application navigate to Settings -> Resources -> File Sharing.

This area has checkboxes for every hard drive directly attached to your computer. Simply check the drives where you want Docker to have access. Then click the Apply and Restart button. This will restart the Docker daemon.

After this you'll be able to rerun the command, and see successful access to the file system. Windows Defender might pop up a dialog box asking permission for Docker to have access. Needless to say, if that happens then click the button to approve access.

About the Author(s)

David Herron : David Herron is a writer and software engineer focusing on the wise use of technology. He is especially interested in clean energy technologies like solar power, wind power, and electric cars. David worked for nearly 30 years in Silicon Valley on software ranging from electronic mail systems, to video streaming, to the Java programming language, and has published several books on Node.js programming and electric vehicles.