How to run a Docker on Windows 10 with WSL?

1.Install Linux on Windows with WSL

Please refer to the URL: Install WSL | Microsoft Docs

WSL: windows subsystem for linux

You must be running Windows 10 version 2004 and higher (Build 19041 and higher) or Windows 11. If not, please upgrade your Windows 10.

Windows 10 upgrade https://www.microsoft.com/zh-cn/software-download/windows10

2. Start a Windows PowerShell and run as Administrator

 3. Set wsl2 to default

wsl --set-default-version 2

4. Linux-Ubuntu installation

wsl --install -d Ubuntu-18.04

5. Open the Linux-Ubuntu

 6. Docker-ce installation

sudo apt install docker-ce

Maybe there are some issues on this step, please don't give up, you will be successful.

Troubleshooting:

sudo apt install docker-ce
  E: Unable to locate package docker-ce
    sudo apt update
  E: Package 'docker-ce' has no installation candidate: 
    sudo apt-get update  
    sudo apt-get upgrade
    # Add docker sources
	sudo echo "deb https://download.docker.com/linux/ubuntu zesty edge" > /etc/apt/sources.list.d/docker.list
	# Support https
	apt-get install -y apt-transport-https ca-certificates curl software-properties-common
	# Add GPG key
	sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
	# Set the location of repository
	sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"
	# Install again
	sudo apt install docker-ce
	 [NOT USE]apt-get install -y docker-ce

If you have questions, please see it. windows10 + wsl2 installation docker_happytree001's blog-CSDN blog_win10 wsl2 installation docker

7. Add current user into docker group, it'll run docker without sudo command.

sudo usermod -aG docker $USER

8. Start docker daemon

sudo service docker start

9. Create a ASP.NET Core Web Api

 10. Add a Dockerfile under the root of your solution

 The contents of dockerfile as below:

#See https://aka.ms/containerfastmode to understand how Visual Studio uses this Dockerfile to build your images for faster debugging.

FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 80
EXPOSE 443

FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["WebApplication1/WebApplication1.csproj", "WebApplication1/"]
RUN dotnet restore "WebApplication1/WebApplication1.csproj"
COPY . .
WORKDIR "/src/WebApplication1"
RUN dotnet build "WebApplication1.csproj" -c Release -o /app/build

FROM build AS publish
RUN dotnet publish "WebApplication1.csproj" -c Release -o /app/publish

FROM base AS final
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "WebApplication1.dll"]

11. Back to Ubuntu, and elevate privilege for current user, (run as root)

sudo -i

12. Switch to the path of solution

df

cd /mnt/d/WebApplication1

13. Docker service start

sudo service docker start

 show docker service status

sudo service docker status

 14, Docker build

docker build --no-cache -t dockerdemo:0.1 . 

15. Docker run

# Save a container
docker run --name dockerdemo -p 801:80 dockerdemo:0.1
or

# Doesn't save a container
docker run -it --rm --name dockerdemo -p 801:80 dockerdemo:0.1

16. Get the IP of Ubuntu, it will be used to get a http request.

ifconfig

17. Testing

Or using http://localhost:801/WeatherForecast 

 18. Appendix

# Show docker container
docker ps -a

# Delete docker container
docker rm {container ID}

# Show docker image
docker images

# Delete docker image
docker rmi {image ID}

# Go to Ubuntu from Windows
\\wsl$\Ubuntu-18.04

Guess you like

Origin blog.csdn.net/wish366/article/details/125011890