Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrating existing containers from Hyper-V to WSL2 technology

After DockerCon 2020, I enthusiastically downloaded Windows 10 2004 and tried to upgrade Docker Desktop to WSL 2 containers and experiment.

I had a few containers, in particular a couple of databases along with their data stored within volumes. Postgres and MS SQL Server in the case.

I wouldn't like to lose the data, though it's not critical. I used Docker volumes rather than OS mounts because I have repeatedly seen that using Windows mounts for database data storage is not recommended.

When I enabled WSL-2 for the first time, all my containers and volumes disappeared.

I'd like to ask if there is any (recommended) procedure or tool to mgirate Hyper-V based containers to WSL-2 along with their data.

Images can be easily redownloaded. How about container setup and data migration to WSL-2?

Of course I can do it manually. I can dump the volumes to my local drive (as a tar) using busybox and restore using another busybox instance

like image 757
usr-local-ΕΨΗΕΛΩΝ Avatar asked Jun 01 '20 11:06

usr-local-ΕΨΗΕΛΩΝ


People also ask

Is WSL2 better than Hyper-V?

Depending on your system's hardware performance, you likely found that WSL2 is the faster option. To expedite the process of running Ubuntu Linux on Hyper-V, you could set up SSH access to the virtual machine. This speeds up command-line access.

Is WSL2 a VM or container?

WSL 2 uses the latest and greatest in virtualization technology to run a Linux kernel inside of a lightweight utility virtual machine (VM). However, WSL 2 is not a traditional VM experience. This guide will compare WSL 1 and WSL 2, including exceptions for using WSL 1 rather than WSL 2.

Does Docker require Hyper-V WSL2?

Docker Desktop on Windows 10 supports two backends: HyperV and WSL2. WSL2 in turn also uses Hyper-V — so without having Hyper-V enabled Docker Desktop fails to start and can't be used.

Is WSL2 a container?

With the WSL 2 backend supported in Docker Desktop for Windows, you can work in a Linux-based development environment and build Linux-based containers, while using Visual Studio Code for code editing and debugging, and running your container in the Microsoft Edge browser on Windows.


Video Answer


1 Answers

Of course, here is my sharing of experience.

Reconstruct the docker run syntax

First, you need to remember or reconstruct the syntax to start the container to re-run them later. The idea is to collect as much information as possible from existing containers to re-run them

Here is a good starting point

Migrating volumes

That's between ease of execution and long-running task. Easy because it took me simply one container, long and tedious because it requires multiple commands

docker run `
           --rm ` #Dispose after use
           -v G:\Docker:/volumes ` # Mount my Windows drive so that the file will appear in Explorer
           - v src_mount:/src ` # e.g. mssql2017:/mssql2017 mounts mssql2017 named volume to Busybox
           busybox `
           tar -zcvf /volumes/backup_name.tar.gz /src

Rinse and repeat for all named volumes of your interest. I had a bunch only

Dump images you won't be able/willing to reconstruct

In my case, Oracle 12c/19c were built but never pushed. Building Oracle is painful becuase you have to build the container after downloading their licensed ZIP file

Use docker save -o wisely. Example

docker save oracledb:12.0.0.0c -o oracledb.img

Restore images

After switching to WSL-2, use docker load wisely

Restore volumes

Manually recreate all volumes with docker volume create and unzip with busybox. This is kind of a reverse

docker run `
       --rm ` #Dispose after use
       -v G:\Docker:/volumes ` # Mount my Windows drive so that the file will appear in Explorer
       - v dest_mount:/dest ` # e.g. mssql2017:/mssql2017 mounts mssql2017 named volume to Busybox
       busybox `
       tar -zxvf /volumes/backup_name.tar.gz /dest

Restore containers

Now that you have your source Docker commandline-s, launch them to recreate containers.

Conclusion: I am thinking about making a reusable Powershell script

like image 93
usr-local-ΕΨΗΕΛΩΝ Avatar answered Oct 23 '22 02:10

usr-local-ΕΨΗΕΛΩΝ