docker in wsl code example

Example 1: windows wsl2 linux install docker

//INSTALL DOCKER ON LINUX SUBSYSTEM (ON WIN 10) AND ALL REQUIRED SOFTWARE
//----------------------------------------------------------------------------------------------------
#ENABLE LINUX SUBSYTEM IN CONTROL PANEL
#INSTALL WSL2
#INSTALL PREFFERED LINUX DISTRO

#INSTALLING DOCKER ON SUBSYSTEM
#https://dzone.com/articles/wsl2-for-dockerized-net-core-application-subhankar

#Setup WSL2 (Enable Hyper-V)
#Fire up your PowerShell(Administrative mode) and run the below commands.  
#This will enable the virtualization feature which will be used by WSL2

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

#Next you have to find out your Installed Linux Distribution, so that you can set that distribution to work as WSL version 2.
wsl -l -v

#convert your current Linux Distribution to WSL version 2
wsl --set-version Ubuntu 2

#Finally, if you want to use your WSL2 as the default version then execute the command below.
wsl --set-default-version 2

# Update the apt package list.
sudo apt-get update -y

# Install Docker's package dependencies.
sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

# Download and add Docker's official public PGP key.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Verify the fingerprint.
sudo apt-key fingerprint 0EBFCD88

# Add the `stable` channel's Docker upstream repository.
#
# If you want to live on the edge, you can change "stable" below to "test" or
# "nightly". I highly recommend sticking with stable!
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

# Update the apt package list (for the new apt repo).
sudo apt-get update -y

# Install the latest version of Docker CE.
sudo apt-get install -y docker-ce

# Allow your user to access the Docker CLI without needing root access.
sudo usermod -aG docker $USER

#START DOCKER DAEMON
sudo service docker start
sudo service docker status

#VERIFY
sudo docker info (or docker ps)

Example 2: running docker in wsl

# Update the apt package list.
sudo apt-get update -y

# Install Docker's package dependencies.
sudo apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common

# Download and add Docker's official public PGP key.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

# Verify the fingerprint.
sudo apt-key fingerprint 0EBFCD88

# Add the `stable` channel's Docker upstream repository.
#
# If you want to live on the edge, you can change "stable" below to "test" or
# "nightly". I highly recommend sticking with stable!
sudo add-apt-repository \
   "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
   $(lsb_release -cs) \
   stable"

# Update the apt package list (for the new apt repo).
sudo apt-get update -y

# Install the latest version of Docker CE.
sudo apt-get install -y docker-ce

# Allow your user to access the Docker CLI without needing root access.
sudo usermod -aG docker $USER

Example 3: docker

docker ps # current containers
docker run # create and start the container
docker create # create container
dokcer exec # to run commnads in container for once
docker volume # create a docker volume
docker network # create a docker network
docker rm # remove container 
docker images # list the images
docker rmi # remove image
docker build # build a new image from dockerfile
docker push # push your image to docker repo
docker pull # download an image from docker repo
docker commit # create an image from container

Tags:

Misc Example