Virtualization and Containerization
Contents
1 Building a custom Linux container 2
1.1 Cloning the simple-container repository . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.2 Building the binary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Creating Linux device and proc files . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4 Starting our custom container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Assigning an IP address to our container . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 Creating Docker containers 5
2.1 Building a simple web server . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.2 Starting the container . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
3 Running an Ubuntu Virtual machine 8
3.1 Creating the virtual machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3.2 Connecting to the virtual machine with VNC . . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Connecting to the virtual machine with SSH . . . . . . . . . . . . . . . . . . . . . . . . . 10
1
1 Building a custom Linux container
1.1 Cloning the simple-container repository
First clone the repository using:
git clone https://github.com/gnudeep/simple-container.git
1.2 Building the binary
We can use the make command to build the binary from our c source file.
For this make sure make and gcc are installed
sudo apt install make gcc
Then enter the cloned repository and run:
make
2
1.3 Creating Linux device and proc files
We can run the make-dev.sh file to create the dev and proc files necessary for our container to work
For this:
sudo ./make-dev.sh
1.4 Starting our custom container
We can start our container using:
sudo ./simple-container
3
1.5 Assigning an IP address to our container
In your host machine run:
ps aux
to get a list of running processes.
Then note down the process id of our simple-container process
Then run:
sudo ip link set veth1 netns 2193
to create a new network interface called veth1 where 2193 is the process id of the container
Then we can assign ip addresses to the new interface
In your host machine run:
sudo ifconfig veth0 192.168.1.1
then enter the namespace of your container with:
4
sudo nsenter -t 2193 --uts --pid --mount --net -- sh
And run:
ifconfig veth 192.168.1.2
to assign the ip 192.168.1.2 to the container
Now we can try pinging our host from the container and the container from our host
2 Creating Docker containers
2.1 Building a simple web server
Let’s clone a repository to create a sample app to run as a container.
git clone https://github.com/gnudeep/scratch-container.git
Then we can enter the cloned repository directory and build the app
First make sure golang is installed
5
sudo apt install golang
Then build the app with:
./build.sh
2.2 Starting the container
First make sure podman is installed
sudo apt install podman
Then build the container using:
podman build -t app-scratch-container .
Then we can start the container with:
6
podman run -d -p 6000:8080 app-scratch-container
We are forwarding the port 8080 of our container to host port 6000
Let’s check if our new webserver is running correctly inside the container For that use:
curl http://localhost:6000
7
3 Running an Ubuntu Virtual machine
3.1 Creating the virtual machine
First create a virtual harddisk for our virtual machine
qemu-img create -f qcow2 ubuntu-vm.qcow2 20G
Then start the virtual machine using the disk image for installation
qemu-system-x86 64 -m 4G
-cpu host
-enable-kvm
-drive file=ubuntu-vm.qcow2,format=qcow2
-cdrom /home/suhasdissa/Downloads/ubuntu-24.04.2-live-server-amd64.iso
-boot d
-vga virtio
-display vnc=:1
8
3.2 Connecting to the virtual machine with VNC
We can use a vnc client like TigerVnc to connect to our virtual machine
vncviewer localhost:5901
9
Then you can access the virtual machine through VNC and finish the installation process
3.3 Connecting to the virtual machine with SSH
After installation, start the virtual machine again with this command:
10
qemu-system-x86 64 -m 4G
-cpu host
-enable-kvm
-drive file=ubuntu-vm.qcow2,format=qcow2
-boot d
-netdev user,id=net0,hostfwd=tcp::2222-:22
-device e1000,netdev=net0
-vga virtio
-display vnc=:1
Here we are forwarding the port 22 of the virtual machine to port 2222 of our host
So we can connect to it using ssh using this command:
ssh suhasdissa@localhost -p 2222
11
12