September 29, 2024

How to Run Linux Ubuntu on Windows By Using Docker

In this tutorial, we explain how to run different versions of Linux Ubuntu on Windows by using docker. The YouTube tutorial accompanying this post is given below.

Motivation: If you are a developer or a software engineer testing programs in Linux and Ubuntu, you would often need to switch between these two operating systems in order to test different aspects and behavior of your program. This is especially the case for Robot Operating System (ROS) developers. Consequently, it is a good idea to have Linux Ubuntu installation inside of Windows environment, so you can quickly test different options.

There are several approaches for running Linux Ubuntu on Windows machines:

1) We can use virtual machines
2) We can use Windows Subsystem for Linux. We covered this in our previous tutorial.

3) We can use Docker and pull a Linux Ubuntu distribution from the official repository given here.

It is good to know and to test all of these three options. In this tutorial, we will cover the option number 3. This might not be the most optimal or the fastest option, however, it is good for testing and learning different aspects of Linux Ubuntu.

In this tutorial, we explain how to

  1. Download (pull) Linux Ubuntu image from the official Docker repository.
  2. Start a Linux Ubuntu container from the Linux Ubuntu image.
  3. Test the container by executing some Bash commands and by installing nano and C++ compiler.
  4. Compile and run a simple program inside of Docker Linux Container.

– The first step is to download and install Docker Desktop for Windows. We created a separate video tutorial on how to do that. The tutorial is given here.

To verify the Docker installation, open a Windows Command prompt and type:

docker version

The output should look like this:

To see the Docker Linux Ubuntu images, you need to go to this website:

To download or better to say to pull a specific version of Linux Ubuntu on your local computer, you need to type the following command in the Windows command prompt:

docker pull ubuntu:<version> 

The currently supported Linux Ubuntu versions are 20.04 (“focal”), 22.04 (“jammy”), and 24.04 (“noble”- also known as “latest”)

For example, to pull (download) Ubuntu 22.04 (code name jammy), we need to type:

docker pull ubuntu:jammy

This will download and create a local image of Ubuntu 22.04 on our computer. To run the Docker container from the image, we need to run

docker run --name os1 -it ubuntu:jammy bash

After you execute this command, you will see that we are in the Bach shell (Linux shell). Now, in this shell, we can write the standard Linux Ubuntu command. For example, you can type the following commands to list all the files:

ls -la

you will see the output shown below

Let us try to install a package or a program in the container. For example, let us try to install the Nano editor such that we can edit different files:

apt-get update
apt-get install nano

Nano editor is a light weight editor that is very easy to be used. Next, let us install GNU compilers, such that we can C++ files. First, let us install all the build tools that include the C++ compiler:

apt-get install build-essential

The command for starting the C++ compiler is g++ . Let us make sure that we can execute this command.

g++ --version 

The output should look like this:

This means that C++ compiler is properly installed and we can use it in our container.

Let us now write our first C++ program. For that purpose create a new folder and inside of that folder create the C++ file called “program1.cpp”:

mkdir codes
cd codes
nano program1.cpp

Then, edit the source file “program1.cpp”. The content of the “program1.cpp” file is given below

#include<iostream>
using namespace std;
int main()
{
cout<<"This is our first program inside of the Linux Ubuntu container!"<<endl;
return 0;
}

Next, we need to compile this file. To compile the file, save the file, and go back to the Bash shell. Then type

g++ -Wall -W -Werror program1.cpp -o program1exec

The name of the executable program is “program1exec”.

To run the program, we first need to make sure that the compiled file can be executed. To do that, we need to set the permissions:

chmod +x  program1exec

Finally, we run the program like this

./program1exec

To exit from our container, we need to type

exit

We will be back in Windows command prompt. To list all the containers, including the ones that are stopped, we need to type

docker ps -a

To erase our container, we need to type

docker rm os1

To erase our image, we need to type

docker image rm -f ubuntu:jammy