Search code examples
docker-for-windows

Use existing file sharing / volumes in Docker-compose with Docker for Windows WSL2


With Docker for Windows 10 pro Hyper-V I work normally with File sharing. So in the Docker Desktop section Resources I add a folder like K:\data on my Windows host. This works well for me for many years.

So my current configuration is:

  • Windows 10 pro
  • Data folder on Windows 10 HOST is e.g. K:\data
  • Docker for Windows with Hyper-V
  • Docker compose (stack) file with 2 Docker images: MySql and Jenkins.
  • Docker components both access the data that is (via a volume specification) residing on the Windows host.

I investigate if I can switch to Docker for Windows WSL2. Then I would like to continue using the docker compose file with the 2 Docker containers. I would like to continue using the data that is residing on the Windows host. More specifically, on the K:\data.

Is it possible to switch from Docker for Windows with Hyper-V to Docker for Windows WSL2 and still use the existing data residing in the Windows folder?

This is not a duplicate question: I work with Docker compose, so I don't want to use the "docker run -v 'host'" solution. In my docker compose file I use e.g. the following line:

volumes:
  - //k/data/spring-boot-app:/data/spring-boot-app

This question gets more important because since the new 2.5+ version, the Hyper-V version is hardly working on my standard Win10pro. The WSL2 worked immediately.

I pose this question as a simple user question, so others may benefit from it. I know that there is a world behind this topic.


Solution

  • Easy does it -- just switching to WSL2 works. I tried both the Docker for Windows 2.4 and 3.0.0 (21-12-2020) version.

    Let me proof and explain in 2 simple steps. Take care of the way the volumes are used in the docker-compose.yml file.

    First, just enabled the WSL2 in your Docker Desktop

    1 - Demo with a simple app:

    version: '3'
    services:
      chatbot:
        image: myusernameatdocker/chatbot:1.0-SNAPSHOT
        ports:
          - 8080:8080
        volumes:
          - /k/data/chatbot:/data/chatbot
    

    I created a simple Spring Boot application. It writes 1 line to my log file on Windows. That file I could perfectly read. Just on k:\data\chatbot\myfile.txt.

    2 - Demo with an existing MySql environment:

    version: '3'
    services:
      mymysqldb:
        image: mysql:5.7
        environment:
          - MYSQL_ROOT_PASSWORD=root123
          - MYSQL_DATABASE=mydatabase
          - MYSQL_USER=johan
          - MYSQL_PASSWORD=bladibladibla
        volumes:
          - /k/data/var/mysql-data:/var/lib/mysql
        ports:
          - "3306:3306"
    

    I just started it and it works. Normally I had first to add a k:\data folder on first installing Docker. Now I didn't do that yet.

    AND ... working with //k/data also worked. So, the MySql I use for many years still works.

    Did I get the warning that accessing the Windows file system is slow? Yes. OK, in my case for development work not a big deal. It's okay, it works!