Skip to content

Instantly share code, notes, and snippets.

@dehsilvadeveloper
Last active February 11, 2025 17:06
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

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.

@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.

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