Skip to content

Instantly share code, notes, and snippets.

@ermogenes
Last active March 6, 2024 16:34
Show Gist options
  • Save ermogenes/c712c964ed6de4e06444bceb78c4d30b to your computer and use it in GitHub Desktop.
Save ermogenes/c712c964ed6de4e06444bceb78c4d30b to your computer and use it in GitHub Desktop.
Ambiente Docker CE no Windows com WSL

Ambiente Docker CE no Windows com WSL

Instruções para rodar o Docker (versão comunitária) no WSL sem utilizar o Docker Desktop (que exige licenciamento).

Estarão disponíveis no seu terminal Powershell os comandos:

Pré-requisitos:

  • Windows com WSL versão 2
  • Uma instância de Linux no WSL (eu usei Ubuntu-20.04 da Microsoft Store)

Docker CE

sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
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 focal stable"
sudo apt update
apt-cache policy docker-ce
sudo apt install docker-ce
docker --version

Testar:

wsl docker run hello-world

A cada reinicialização do WSL2 é necessário iniciar o serviço:

wsl sudo service docker start

Ref: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-docker-on-ubuntu-20-04-pt

Executar dockerd automaticamente ao logar no WSL

sudo visudo

Adicionar no final:

# Docker daemon specification
sungkim ALL=(ALL) NOPASSWD: /usr/bin/dockerd

Editar o .bashrc:

nano ~/.bashrc

Adicionar no final:

# Start Docker daemon automatically when logging in if not running.
RUNNING=`ps aux | grep dockerd | grep -v grep`
if [ -z "$RUNNING" ]; then
    sudo dockerd > /dev/null 2>&1 &
    disown
fi
sudo usermod -aG docker $USER

Ref.: https://medium.com/geekculture/run-docker-in-windows-10-11-wsl-without-docker-desktop-a2a7eb90556d

Docker Compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
docker-compose --version

Ref: https://docs.docker.com/compose/install/

Portainer

docker volume create portainer_data
docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:2.9.3

https://localhost:9443

Ref.: https://docs.portainer.io/v/ce-2.9/start/install/server/docker/linux

lazydocker

docker run --rm -it -v /var/run/docker.sock:/var/run/docker.sock -v /yourpath:/.config/jesseduffield/lazydocker lazyteam/lazydocker

$PROFILE

No Powershell:

Set-ExecutionPolicy RemoteSigned
notepad $PROFILE
Function Start-WslDockerStart {
	wsl sudo service docker start
}

Function Start-WslDocker {
	wsl docker $args
}

Function Start-WslDockerCompose {
	wsl docker-compose $args
}

Function Start-WslPortainer {
	wsl docker stop portainer
	wsl docker rm portainer
	wsl docker run -d -p 8000:8000 -p 9443:9443 --name portainer --restart=always -v /var/run/docker.sock:/var/run/docker.sock -v portainer_data:/data portainer/portainer-ce:latest
	Start-Process "https://localhost:9443/"
}

Function Start-WslPortOpen {
	$listenport = $args[0]
	$connectport = $args[1]
	$ip = (wsl sh -c "hostname -I").Split(" ")[0]
	netsh interface portproxy add v4tov4 listenport=$listenport listenaddress=0.0.0.0 connectport=$connectport connectaddress=$ip
	netsh interface portproxy show v4tov4 | Select-String -Pattern $listenport
}

Function Start-WslPortClose {
	$listenport = $args[0]
	netsh interface portproxy delete v4tov4 listenport=$listenport 0.0.0.0
}

Function Start-WslPortShow {
	netsh interface portproxy show v4tov4
}

Function Start-WslPortReset {
	netsh interface portproxy reset
}

Set-Alias -Name docker-start -Value Start-WslDockerStart
Set-Alias -Name docker -Value Start-WslDocker
Set-Alias -Name docker-compose -Value Start-WslDockerCompose
Set-Alias -Name portainer -Value Start-WslPortainer
Set-Alias -Name proxy-add -Value Start-WslPortOpen
Set-Alias -Name proxy-delete -Value Start-WslPortClose
Set-Alias -Name proxy-show -Value Start-WslPortShow
Set-Alias -Name proxy-reset -Value Start-WslPortReset

Ref.: https://www.objectivity.co.uk/blog/how-to-live-without-docker-desktop-developers-perspective/

Dicas adicionais

@ermogenes
Copy link
Author

Rodar o Powershell como admin:

start-process PowerShell -verb runas

@ermogenes
Copy link
Author

As partições só crescem automaticamente. Para reduzir, tem que ser manual.
Ref.: microsoft/WSL#4699
Ferramenta simples para reduzir o espaço: https://github.com/okibcn/wslcompact

@ermogenes
Copy link
Author

Para o Ubuntu 22, use as instruções de https://github.com/codeedu/wsl2-docker-quickstart.

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