Skip to content

Instantly share code, notes, and snippets.

@martinsam16
Last active November 8, 2024 13:03
Show Gist options
  • Save martinsam16/4492957e3bbea34046f2c8b49c3e5ac0 to your computer and use it in GitHub Desktop.
Save martinsam16/4492957e3bbea34046f2c8b49c3e5ac0 to your computer and use it in GitHub Desktop.
How to install wsl2 ubuntu + docker + docker-compose

Activate wsl2

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --set-default-version 2

Install and Configure Ubuntu

# install ubuntu from the store
# open the ubuntu app
# set username and password
# close the ubuntu app
wsl --set-version Ubuntu 2

Install Docker & Docker-compose on Ubuntu

#Refresh and install packages
sudo apt-get update
sudo apt-get install -y apt-transport-https ca-certificates curl software-properties-common libssl-dev libffi-dev git wget nano

#Add user group
sudo groupadd docker
sudo usermod -aG docker ${USER}

#Add docker key and repo
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get update

#Install docker and docker-compose
sudo apt-get install -y docker-ce containerd.io docker-compose

#Install docker-compose (if the previous command failed to install)
sudo curl -sSL https://github.com/docker/compose/releases/download/v`curl -s https://github.com/docker/compose/tags | grep "compose/releases/tag" | sed -r 's|.*([0-9]+\.[0-9]+\.[0-9]+).*|\1|p' | head -n 1`/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose && sudo chmod +x /usr/local/bin/docker-compose

Run docker on startup

echo "sudo service docker start" >> ~/.profile
#echo "cd /mnt/" >> ~/.profile
source ~/.profile

Considerations

#To restart wsl (use in case it didn't work)
wsl --shutdown

# to communicate containers don't use localhost, point to: [ubuntu terminal] - the first ip that appears
ip addr | grep eth0 | grep inet

# for for ensuring docker service is running on startup to workaround having to deal with sudo
wsl.exe -u root service docker status || wsl.exe -u root service docker start

Limit resources

  1. Enter the user's folder. C:\Users\USER
  2. Create the file: .wslconfig
  3. Enter the following:
[wsl2]
memory = 4GB # Limits memory
processors = 2 # Limits virtual processors
  1. Save and restart the LxssManager service
@herculanocm
Copy link

tks

@gdanielson
Copy link

Thanks @martinsam16 for this, great info.
For for ensuring docker service is running on startup to workaround having to deal with sudo I have configured this..
wsl.exe -u root service docker status || wsl.exe -u root service docker start

@martinsam16
Copy link
Author

@gdanielson thanks, it's updated

@kentheam
Copy link

Thanks :)

@alvarosinmarca
Copy link

Thank you!

@gustavorteuber
Copy link

thx!!

@Windelimarcos
Copy link

This is great! Thks.

@jasperspahl
Copy link

why do you put a cd /mnt/ in the .profile?
seams unnessesary

Copy link

ghost commented Sep 15, 2023

Thanks!
Worked for me.

@miguelrosa81
Copy link

Nice. Thank You. Very help full.

@MarcoGhise
Copy link

Install docker-compose is missing a "v" before the release version. It should be

https://github.com/docker/compose/releases/download/v2.23.0/docker-compose-linux-x86_64

Instead I got

https://github.com/docker/compose/releases/download/2.23.0/docker-compose-linux-x86_64

I guess this fix is enough to work out the issue.

#Install docker-compose (if the previous command failed to install)
sudo curl -sSL https://github.com/docker/compose/releases/download/v`curl -s https://github.com/docker/compose/tags | grep "compose/releases/tag" | sed -r 's|.*([0-9]+\.[0-9]+\.[0-9]+).*|\1|p' | head -n 1`/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose && sudo chmod +x /usr/local/bin/docker-compose

@martinsam16
Copy link
Author

@ElvisDev187
Copy link

very helpfull thx!

@RogerioLS
Copy link

oh man thanks!!

@meyerovb
Copy link

meyerovb commented Feb 2, 2024

I didn't need to do anything above, docker desktop had this:
RVu24

@0tii
Copy link

0tii commented Feb 22, 2024

For your own sanity's sake do not add the plain service start command into your .profile, it will annoy you with every terminal you open. use this instead:

if ! service docker status > /dev/null; then
  echo "docker service not running, starting..."
  sudo service docker start
fi

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