Since I wrote Launch Ubuntu Desktop on Google Cloud last week, I kept thinking about putting Ubuntu Desktop into containers. A container is an independent unit of software packages and their dependencies so that the application on the container can run reliably in different computing environments. Docker, an open-source project launched in 2013, made Container technology popular all over the world in just a few years. Why? Let’s compare Containers and Virtual Machines.
The fundamental difference between Virtual Machine and Container is that Containers do not contain a guest Operating System. Containers virtualize the operating system instead of hardware, hence are more portable and efficient. Multiple containers can run on the same machine and share the same OS kernel with other containers. That being said, Containers are much smaller than VMs. You can run 4-6 times as many containers as VMs in the same physical machine.
However, Docker didn’t invent Container, instead, Docker built its technology base on LXC, a method of containerization developed by Canonical in 2008.
In this tutorial, we will create a Docker Container on the latest Ubuntu Pro 20.04 by following 3 steps:
- Launch a Ubuntu VM instance on Google Cloud to host Containers.
- Install Docker.
- Pull a Docker Image and run the container.
Launch a Ubuntu VM to host Containers
In this step, we will launch a VM instance in Google Cloud. The default e2-medium (2 vCPU, 4 GB memory) machine type works fine for the tutorial purpose. If you want a more performant machine, there are a variety of choices in Google Cloud.
1. In the Google Cloud Console, go to the VM Instances page:
2. Click CREATE INSTANCE.
3. Set the instance name to ubuntu-container-host .
4. Select a region and zone you want to run your instance.
5. Scroll down to the Boot disk options and click Change
6. In the Boot disk pop-up window, in Operating System, select Ubuntu Pro from the drop-down; in Version, select Ubuntu-Pro-20.04-LTS; keep the rest options as default value and click SELECT. Ubuntu Pro ensures the latest security update, which will be useful when we install the productivity applications.
7. Click CREATE to create the instance.
8. In less than one minute, you will be able to see your Ubuntu instance in RUNNING status. You can click the SSH button in the instance list to connect to your new instance.
If you prefer to start a VM through Google Cloud Shell, you can use this command to achieve the same result:
gcloud compute instances create ubuntu-container-host --zone=us-central1-a --machine-type=e2-medium --image=projects/ubuntu-os-pro-cloud/global/images/ubuntu-pro-2004-focal-v20210720
In Ubuntu, the easiest way to install Docker is to snap. A snap is a bundle of an app and its dependencies that works without modification across many different Linux distributions. Snaps are discoverable and installable from the Snap Store, an app store with an audience of millions.
1. In the SSH window connected to your VM instance, update the package manager data and install Docker
sudo apt update sudo snap install docker
That’s it! Snap makes installation so easy.
Pull a Docker Image and run the container
Let’s first search for ubuntu images.
sudo docker search ubuntu
We found a lot of ubuntu related images:
NAME DESCRIPTION STARS OFFICIAL AUTOMATED ubuntu Ubuntu is a Debian-based Linux operating sys… 13254 [OK] dorowu/ubuntu-desktop-lxde-vnc Docker image to provide HTML5 VNC interface … 590 [OK] websphere-liberty WebSphere Liberty multi-architecture images … 282 [OK]
[OK] means that the image was built and supported by a company. We will pull the latest official Ubuntu Image
sudo docker pull ubuntu
Using default tag: latest latest: Pulling from library/ubuntu 7b1a6ab2e44d: Pull complete Digest: sha256:626ffe58f6e7566e00254b638eb7e0f3b11d4da9675088f4781a50ae288f3322 Status: Downloaded newer image for ubuntu:latest docker.io/library/ubuntu:latest
Check the images we downloaded to this VM instance:
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE ubuntu latest ba6acccedd29 7 weeks ago 72.8MB
Let’s run a container based on this official Ubuntu image.
sudo docker run -it ubuntu root@81046dba57f4:/#
In this container, we can check if it is the latest version of ubuntu:
DISTRIB_ID=Ubuntu DISTRIB_RELEASE=20.04 DISTRIB_CODENAME=focal DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS"
Yes, it is the latest version. Let’s install python into this container.
apt update apt install python3
Check the version we installed:
Since we modify the original Ubuntu image, we want to save the changes to new images. We click “Ctrl + P” and “Ctrl + Q” to exit the container interface and back the VM.
sudo docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 81046dba57f4 ubuntu "bash" 21 minutes ago Up 21 minutes bold_sammet
We commit the changes to a new Docker image:
sudo docker commit -m "installed python3" -a "hugo" 81046dba57f4
-m with parameter “installed python3? indicates the changes made to this image.
-a with parameter “hugo” indicates that I am the author of these changes.
sudo docker images
REPOSITORY TAG IMAGE ID CREATED SIZE <none> <none> 2316816463ea 16 seconds ago 144MB ubuntu latest ba6acccedd29 7 weeks ago 72.8MB
That’s it. We made a Docker Container Image on Ubuntu Pro on Google Cloud.