Tuesday, October 17, 2023

How to run a docker image without docker desktop installation in Windows 10 VM with virtulization and hyper-v enabled?

Installing WSL 2

  1. Open PowerShell as an administrator: Right-click the Windows Start button and select "Windows Terminal (Admin)" or "Command Prompt (Admin)."

  2. Enable the WSL feature:

    powershell
    dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  3. Enable the Virtual Machine Platform feature:

    powershell
    dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  4. Restart your computer to complete the installation of the Virtual Machine Platform feature.

  5. After restarting, open PowerShell as an administrator again.

  6. Set WSL 2 as the default version:

    powershell
    wsl --set-default-version 2
  7. Install a Linux distribution of your choice from the Microsoft Store or by using the command-line. For example, to install Ubuntu:

    powershell
    wsl --install -d Ubuntu
  8. Initialize your newly installed Linux distribution and follow the prompts to set up a user and password.

WSL 2 is now installed and ready for use.

Installing Docker in WSL 2

To install Docker in WSL 2, follow these steps:

  1. Open a WSL 2 terminal.

  2. Run the following command:

    bash
    sudo apt update && sudo apt install docker.io

Running the Docker Image

To run a Docker image in WSL 2, follow these steps:

  1. Open a WSL 2 terminal.

  2. Run the following command to start a Docker container based on an image:

    bash
    docker run -it <image_name>

    For example, to run the hello-world image:

    bash
    docker run -it hello-world

    This will start a new container and display the message "Hello from Docker!" on the console.

  3. If you need to map specific ports, use the -p option. For instance, to run the hello-world image on port 80:

    bash
    docker run -it -p 80:80 hello-world

You can then access the container on port 80 of your machine.

Example

To run the nginx image in a Windows 10 VM with virtualization and Hyper-V enabled:

  1. Install WSL 2.

  2. Install Docker in WSL 2.

  3. Run the nginx image with the following command:

    bash
    docker run -it -p 80:80 nginx

You can access the Nginx web server on port 80 of your host machine.

Note: If you are running Docker in a VM, you may need to configure port forwarding to access the container from the host machine.

No comments:

Post a Comment