Create customised docker containers on Ubuntu Pro

Docker containers are extremely useful for running applications reliably on different computing environments. This is because they package the application along with all its dependencies into a single image that can be easily deployed.

Docker is the underlying technology used to run these containers / images. Docker also allows you to modify the container and create new customised versions easily. As an example, on your Ubuntu Pro VM, we’ll run a container based on the latest Ubuntu image and then customise it by including Python.

Note

If you don’t have an Ubuntu Pro VM already, you can create one based on Create an Ubuntu Pro instance

Install Docker

On your Ubuntu Pro VM, the easiest way to install Docker is to use snap. Update your package manager data and then install docker using:

sudo apt update
sudo snap install docker

Download a Docker image

Search for available Ubuntu images:

sudo docker search ubuntu

You’ll find many ubuntu related images, some of which have an [OK] under the ‘OFFICIAL’ column indicating that they are images built and supported by a company.

NAME                             DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
ubuntu                           Ubuntu is a Debian-based Linux operating sys…   16442     [OK]
websphere-liberty                WebSphere Liberty multi-architecture images …   297       [OK]
ubuntu-upstart                   DEPRECATED, as is Upstart (find other proces…   115       [OK]
neurodebian                      NeuroDebian provides neuroscience research s…   104       [OK]
ubuntu/nginx                     Nginx, a high-performance reverse proxy & we…   100
ubuntu/squid                     Squid is a caching proxy for the Web. Long-t…   67
[...]

Pull the latest official Ubuntu image:

sudo docker pull ubuntu

It’ll give an output similar to:

Using default tag: latest
latest: Pulling from library/ubuntu
445a6a12be2b: Pull complete
Digest: sha256:aabed3296a3d45cede1dc866a24476c4d7e093aa806263c27ddaadbdce3c1054
Status: Downloaded newer image for ubuntu:latest
docker.io/library/ubuntu:latest

You can check the downloaded images using:

sudo docker images

The image that you just pulled will show up in the output:

REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
ubuntu       latest    c6b84b685f35   6 weeks ago   77.8MB

Run the container

Run a container based on this downloaded image and it’ll take you to the new container’s command prompt:

sudo docker run -it ubuntu
root@0587b9a5915d:/#

In this container, you can check if it is the latest version of Ubuntu:

cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=22.04
DISTRIB_CODENAME=jammy
DISTRIB_DESCRIPTION="Ubuntu 22.04.3 LTS"

Customise the image

To customise the image, you can for instance install Python within the container:

apt update
apt install python3

Check the installed version:

/usr/bin/python3 -V
Python 3.10.12

Now that you have modified the original Ubuntu image, you can save the changes to create a new image. Use Ctrl + P and Ctrl + Q to exit the container interface and get back into the VM.

To save the changes you’ll need the container ID (of the container where you made the changes). You can get this by checking the containers running on your VM:

sudo docker ps
CONTAINER ID   IMAGE     COMMAND       CREATED         STATUS         PORTS     NAMES
0587b9a5915d   ubuntu    "/bin/bash"   3 minutes ago   Up 3 minutes             quirky_lamarr

Now commit the changes to create a new Docker image:

sudo docker commit -m "installed python3" -a "myname" 0587b9a5915d

where the parameter -m (message) is used to indicate the changes made and -a (author) is used to indicate the author of the changes.

If you look at the list of images on your VM, you’ll see the newly added one:

sudo docker images
REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
<none>       <none>    4fad28bffebd   53 seconds ago   152MB
ubuntu       latest    c6b84b685f35   6 weeks ago      77.8MB