Skip to content

Instantly share code, notes, and snippets.

@dehsilvadeveloper
Last active April 23, 2025 15:14
Show Gist options
  • Save dehsilvadeveloper/c3bdf0f4cdcc5c177e2fe9be671820c7 to your computer and use it in GitHub Desktop.
Save dehsilvadeveloper/c3bdf0f4cdcc5c177e2fe9be671820c7 to your computer and use it in GitHub Desktop.
Installing Docker on WSL 2 with Ubuntu 22.04

Installing Docker on WSL 2 with Ubuntu 22.04

Instalando Docker em um WSL 2 com Ubuntu 22.04

Prerequisites

Before start the installation process, make sure you meet the following prerequisites:

  • A Windows 10 operating system with WSL 2 support.
  • WSL 2 enabled.
  • Ubuntu 22.04 installed on WSL 2.

Step 1: Update the system

Before installing Docker, it is a good practice to ensure that all system packages are up to date. Open the Ubuntu terminal in WSL 2 and run the following command:

sudo apt update && sudo apt upgrade -y

Step 2: Install dependencies

Install the required dependencies for Docker:

sudo apt install -y apt-transport-https ca-certificates curl software-properties-common

Step 3: Add Docker GPG Key

Add the official Docker GPG key:

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

Step 4: Add the Docker Repository

Add the Docker repository to the system:

echo "deb [signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

Step 5: Install Docker Engine

Update repositories and install Docker Engine:

sudo apt update
sudo apt install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

Step 6: Add you user to Docker Group

Add your user to the Docker group to avoid the need to use sudo on every Docker command:

sudo usermod -aG docker $USER

Step 7: Check the installation

Close the open terminal on Ubuntu.

Restart WSL via the Windows command line (Powershell).

wsl --shutdown

Access Ubuntu again. Check if Docker was installed correctly on the Ubuntu terminal:

docker --version

You should get a response similar to this:

Docker version 26.1.4, build 5650f9b

Extra tips

Docker start up and shut down

To start the Docker service run the following on the Ubuntu terminal:

sudo service docker start

To stop the Docker service run the following on the Ubuntu terminal:

sudo service docker stop

Projects location

The performance of WSL 2 lies on running everything within Linux, so avoid running your projects with Docker from the /mnt/c path, as you will lose performance.

Opening folders via terminal

You can open a folder from Ubuntu with Windows Explorer by typing the command on your Ubuntu terminal:

explorer.exe .

The folder will be open using the Windows Explorer.

Remember to navigate to the desired folder on Ubuntu terminal beforehand.

Copy folder from Windows to Ubuntu

On the Ubuntu terminal confirm that you can access the mounted drive and all its directories using the command below.

sudo ls /mnt/*

You have to see the list of folders and files currently present on your Windows C:/ folder.

If you can see the items just fine, then navigate until the destination folder on the Ubuntu terminal and use the following command.

cp -r /mnt/c/my-folder .

Where "my-folder" is the name of the folder on Windows that you want to copy and "." indicates the destination. Since that we are already on the destination folder we can use just "." , but you can inform the desetination path too.

cp -r /mnt/c/my-folder /home/my-user/my-folder

Opening project on Visual Studio Code

You can open a project with the IDE Visual Studio Code by typing the command on your Ubuntu terminal:

code .

Remember to navigate to the desired folder on Ubuntu terminal beforehand.

Free cached memory on Ubuntu

To free cache memory on Linux Ubuntu running on WSL you can use the following command on the Ubuntu terminal:

echo 1 | sudo tee /proc/sys/vm/drop_caches

Windows Terminal

To access your environment through Windows, I recommend using the Windows Terminal by Microsoft, also available on the Windows Store. This tool includes CLI tabs, a high degree of customization, and even native WSL support to open Linux-based windows.

Want to learn more?

If you want to see the official documentation, visit https://docs.docker.com/desktop/wsl/

@adainrivers
Copy link

Thank you!

@dehsilvadeveloper
Copy link
Author

Thank you!

You are welcome.

@neok
Copy link

neok commented Dec 9, 2024

Very nice, thanks

@dehsilvadeveloper
Copy link
Author

Very nice, thanks

You are welcome.

@cjz9032
Copy link

cjz9032 commented Dec 13, 2024

Thank you!

@dehsilvadeveloper
Copy link
Author

Thank you!

You are welcome.

@sudheer-akki
Copy link

Thanks a lot!

@koushik-ta
Copy link

Thanks for sharing this. It's really helpful.

@dehsilvadeveloper
Copy link
Author

Thanks a lot!

You are welcome.

@dehsilvadeveloper
Copy link
Author

Thanks for sharing this. It's really helpful.

You are welcome.

@nigiwen
Copy link

nigiwen commented Dec 29, 2024

I want to know how to move Ubuntu and Docker to another drive, because I don't have space on my C drive.

@dehsilvadeveloper
Copy link
Author

I want to know how to move Ubuntu and Docker to another drive, because I don't have space on my C drive.

Hello. I never done this kind of drive migration before. However I found these two articles about the matter:

Maybe the information there can help you ?

It appears that WSL has some commands to help you on the migration process, but, again, I never done this before and I don`t know if it will work.

Good luck.

@nigiwen
Copy link

nigiwen commented Dec 31, 2024

I want to know how to move Ubuntu and Docker to another drive, because I don't have space on my C drive.

Hello. I never done this kind of drive migration before. However I found these two articles about the matter:

Maybe the information there can help you ?

It appears that WSL has some commands to help you on the migration process, but, again, I never done this before and I don`t know if it will work.

Good luck.

This is cool. Thanks.

@Aboubakr06
Copy link

Thank you

@Adhjie
Copy link

Adhjie commented Jan 9, 2025

I actually need that since Vanilla OS requirement is pretty high on disk space. I learnt of export import from TroubleChute video. I will have to check the actual .vhdx on the environment path tho, by using diskpart.

Oh yeah, I first found this from the sparse feature issue in wsl, that lead me to diskpart feature, and eventually export import feature.

I was researching backup option in linux, but since wsl is a vm, turns out export import is more than enough instead of foxclone or timeshift.

For reducing disk space, backup, and moving the wsl .vhdx VM file, links:
https://superuser.com/questions/1606213/how-do-i-get-back-unused-disk-space-from-ubuntu-on-wsl2

microsoft/WSL#4699 (comment)

Thanks for the notif tho, now I will check my .vhdx to use other distro and move it to other disk. I do hope the imported .vhdx doesn't resides in windowsapp/Ubuntu directory anymore which is on C:

@dehsilvadeveloper
Copy link
Author

Thank you

You are welcome.

@dehsilvadeveloper
Copy link
Author

I actually need that since Vanilla OS requirement is pretty high on disk space. I learnt of export import from TroubleChute video. I will have to check the actual .vhdx on the environment path tho, by using diskpart.

Oh yeah, I first found this from the sparse feature issue in wsl, that lead me to diskpart feature, and eventually export import feature.

I was researching backup option in linux, but since wsl is a vm, turns out export import is more than enough instead of foxclone or timeshift.

For reducing disk space, backup, and moving the wsl .vhdx VM file, links: https://superuser.com/questions/1606213/how-do-i-get-back-unused-disk-space-from-ubuntu-on-wsl2

microsoft/WSL#4699 (comment)

Thanks for the notif tho, now I will check my .vhdx to use other distro and move it to other disk. I do hope the imported .vhdx doesn't resides in windowsapp/Ubuntu directory anymore which is on C:

Thanks for sharing.

@Adhjie
Copy link

Adhjie commented Jan 9, 2025

@nigiwen

Export, import is still my way to go.
There's another option by the same YTber, using junction, though I would say this is for advance user only since hardlink symbolic link and junction have their differences in use case.

There's a good chart of this in ss64 website.

So I still prefer --export --vhdx and other options
Then --import.
The diskpart program made by one of the user in that github issue is nice too, since it pings the .vhdx file location

This is the link to the backup solution (if some users can't find it because of YT search being dependent on keyword), I just found it today there's a junction option if you search 'backup' or 'move data':
https://youtu.be/-R4VcMCkc6k
(It's fast in my m2 SSD, it would probably be slower in HDD)

Edit:
Spellings, wordings.

Edit 2:
Forgot to tag the person asking about moving the wsl virtual disk from one partition to another.

@jorgeromero17
Copy link

Thank you! Great content!

@dehsilvadeveloper
Copy link
Author

Thank you! Great content!

You are welcome.

@barusko
Copy link

barusko commented Feb 9, 2025

You rock !! thank you ;) ...

@jfgudk
Copy link

jfgudk commented Feb 10, 2025

Thank You!

@LegendWorrier
Copy link

thanks

@dehsilvadeveloper
Copy link
Author

You rock !! thank you ;) ...

You are welcome.

@dehsilvadeveloper
Copy link
Author

Thank You!

You are welcome.

@dehsilvadeveloper
Copy link
Author

thanks

You are welcome.

@ameunoia
Copy link

nice one sir!

@airtonix
Copy link

airtonix commented Apr 2, 2025

Because you shouldn't seriously entertain using any mounted windows drive:

I'd set the /etc/wsl.conf to

[automount]
enabled = false

[interop]
enabled = false
appendWindowsPath = false

[boot]
command = service docker start

@Adhjie
Copy link

Adhjie commented Apr 2, 2025

Working solely in wsl is the ideal workflow.
But to move or copy paste files, usually autompint automount* is easier than manual setup.

Move or copy are fine.
Just don't do something like wet wget* across system.

If you got unlimited internet data with FUP. There are syncthing and its forks for copy paste or mirrored files/notes.

I'll share my personal .conf and wsl.config later when I get to open my pc at home.

It depends on the user needs.

One could also min max the values in umask etc. For beginner I mainly just use default.

I forgot to synced my wsl configs using syncthing so it's only in my pc, rn.

Also, since I haven't got the time to fully deploy docker, my configs are mainly for wsl and distro.

There's something like loop address for docker but I only find it in issue not yet in practice. ( I was researching each confs that have to do with docker.)

I was doing wsl DE GUI to set up waydroid, but the kernel also stop me from doing it.

There's also a drop cache problem in wsl if it's set to auto.

Links:
https://gist.github.com/gorshkov-leonid/b55072d6876acecf43dabaf7f1e72cf0

https://gist.github.com/martinsam16/4492957e3bbea34046f2c8b49c3e5ac0

(Anyone are free to chime in for docker wsl configs optimization, I only know some optimization for wsl case. Docker wsl configs are in my waitlist but not priority;
since I only intend to taste Linux dev env in wsl, but was intrigued by andro emu in linux as an OS alt to proprietary windows andro emu)

Edit misspellings

edit 2:

wsl.conf inside wsl (I can't check my latest one, I forgot I backed up my wsl instance [wsl --export] and set up andro emu on windows again, so all virt are disabled, to make an emu that use intel bridge and virtual box to use the virt feature from bios, virtualization enabled in taskmgr, directly)

# Automatically mount Windows drive when the distribution is launched
[automount]

# Set to true will automount fixed drives (C:/ or D:/) with DrvFs under the root directory set above. Set to false means drives won't be mounted automatically, but need to be mounted manually or with fstab.
enabled = true

# Sets the directory where fixed drives will be automatically mounted. This example changes the mount location, so your C-drive would be /c, rather than the default /mnt/c. 
# root = /

# DrvFs-specific options can be specified.
# pinned gist. To avoid this error https://askubuntu.com/questions/1115564/wsl-ubuntu-distro-how-to-solve-operation-not-permitted-on-cloning-repository
options = "metadata"
# ,uid=1003,gid=1003,umask=077,fmask=11,case=off"

# Sets the `/etc/fstab` file to be processed when a WSL distribution is launched.
# mountFsTab = true

# Network host settings that enable the DNS server used by WSL 2. This example changes the hostname, sets generateHosts to false, preventing WSL from the default behavior of auto-generating /etc/hosts, and sets generateResolvConf to false, preventing WSL from auto-generating /etc/resolv.conf, so that you can create your own (ie. nameserver 1.1.1.1).
# [network]
# hostname = DemoHost
# generateHosts = false
# generateResolvConf = false

# Set whether WSL supports interop processes like launching Windows apps and adding path variables. Setting these to false will block the launch of Windows processes and block adding $PATH environment variables.
# gist pinned. To avoid collisions. Any necessary paths can be set manually
[interop]
enabled = true
appendWindowsPath = false

# Set the user when launching a distribution with WSL.
# [user]
# default = DemoUser

# Set a command to run when a new WSL instance launches. This example starts the Docker container service.
[boot]
systemd = true
# command = service docker start

wsl.config

# Settings apply across all Linux distros running on WSL 2
[wsl2]

# Limits VM memory to use no more than 4 GB, this can be set as whole numbers using GB or MB
memory=3GB

# Sets the VM to use two virtual processors (logical processor, core x threads iirc)
processors=5

# Specify a custom Linux kernel to use with your installed distros. The default kernel used can be found at https://github.com/microsoft/WSL2-Linux-Kernel
# kernel=C:\\temp\\myCustomKernel

# Sets additional kernel parameters, in this case enabling older Linux base images such as Centos 6
# kernelCommandLine = vsyscall=emulate

# Sets amount of swap storage space to 8GB, default is 25% of available RAM
swap=8GB

# Sets swapfile path location, default is %USERPROFILE%\AppData\Local\Temp\swap.vhdx
# swapfile=C:\\temp\\wsl-swap.vhdx

# Disable page reporting so WSL retains all allocated memory claimed from Windows and releases none back when free
pageReporting=true
idleThreshold=1

guiApplications=true

# Turn on default connection to bind WSL 2 localhost to Windows localhost. Setting is ignored when networkingMode=mirrored
localhostforwarding=true

# Disables nested virtualization
# nestedVirtualization=false

# Turns on output console showing contents of dmesg when opening a WSL 2 distro for debugging
# debugConsole=true

# networkingMode = mirrored # https://github.com/microsoft/WSL/issues/10494

# Enable experimental features
[experimental]
sparseVhd=false
autoMemoryReclaim=dropcache
# hostAddressLoopback = true # https://github.com/microsoft/WSL/issues/10494
nestedVirtualization = true

# 2nd order after normal documentation. the above are from microsoft wsl example of wsl dot config

# pinned
# idleThreshold=1

# pinned no documented conclusive solution yet
# for docker instance, eg
# vmIdleTimeout=-1 (error, not working for now. this is supposed to disable timeout or make it infinite)
vmIdleTimeout = 604800000

Yeah, old wsl.conf
Sadly my wsl.conf is inside a wsl.vhdx backup.
Interop better be off indeed. Not sure about vscode integration but, I haven't reached that part yet.

I think having a text editor or DE inside wsl is better, I don't know yet how to access wsl things from Windows vscode integration.

There's a devops oriented distro, but I'm not sure if Linux ISOs need just to be tar'd so that it could be imported into wsl. Or it's more complex than that.

Edit:
I got the name now: bluefin
https://news.ycombinator.com/item?id=38992292

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