Use Ubuntu and other distributions as Docker containers
Ubuntu Versions
You can use the commands in Listing 3 to create containers with different versions of Ubuntu. Each of these commands creates a container from an Ubuntu image and starts a Bash shell in it. The options -i
(interactive) and -t
(terminal) tell the container to run in interactive mode with a virtual terminal, so you can control it from the current terminal window. Without these options the container would start in the background.
Listing 3
Starting a Container
### Current version $ docker run -it ubuntu ### Ubuntu 20.04 $ docker run -it ubuntu:20.04 ### Ubuntu 16.04 $ docker run -it ubuntu:16.04
From the root prompt, you can see that you have administrator privileges in the container (Listing 4, lines 2 to 5). In the shell you can now enter commands in the usual way (lines 2 and 6). If you try to use external programs, you will start the versions from the container. To find out which distribution version is running in the container, you can look for the /etc/os-release
, /etc/debian_version
, or /etc/redhat-release
files (line 6).
Listing 4
In the Container
01 esser@pencent:~$ docker run -it ubuntu 02 root@4eaac4755505:/# ps 03 PID TTY TIME CMD 04 1 pts/0 00:00:00 bash 05 9 pts/0 00:00:00 ps 06 root@4eaac4755505:/# grep PRETTY /etc/os-release 07 PRETTY_NAME="Ubuntu 22.04.1 LTS"
If you already know when starting the container that you do not need a shell, but simply want to execute a specific command, you append it after the image name in the call (Listing 5). Simply omit the -i
and -t
options if interactive use is not required. Figure 2 shows how to launch containers with different versions of Ubuntu. To do this, simply append a colon and the desired version to the image name (such as ubuntu:16.04
).
Listing 5
Command Call
$ docker run --rm fedora grep PRETTY /etc/os-release PRETTY_NAME="Fedora Linux 36 (Container Image)"
You cannot use the Docker tool to find out which versions are available, but you can get this information from the Docker Hub page [6]. Use the search function there, select the appropriate image from the matches, and then switch to the Tags tab. On a longish page, you will then find hints relating to all available tags. These can be version numbers, but also code names or identifiers such as latest. If you change the sort order to A-Z, you will see the oldest version numbers first (Figure 3). If you know the name of the image, you can also directly construct a URL, such as https://hub.docker.com/_/<image>/Tags.
The Bash function in Listing 6 is an alternative to this. Include this code snippet in the ~/.bashrc
file and post-install the skopeo
and jq
tools (in the packages of the same name on Ubuntu). You can use the dfind
command in all new shells to search for versions on Docker Hub (Listing 7).
Listing 6
dfind
dfind () { skopeo inspect docker://$1 | \ jq '.RepoTags' | tr -d "\n"; echo; }
Listing 7
Running dfind
$ dfind fedora ["20", "21", "22", "23", "24", "25", "26", "26-modular", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "branched", "heisenbug", "latest", "modular", "rawhide"]
File Access
Running programs in the container usually only makes sense if you can access files on the host computer. You can set up a share far more easily with Docker than with a VM; simply specify an additional option of the form
-v <FolderHost>:<FolderContainer>
when running Docker. For example, to make your home directory on the host available to an Ubuntu container in /var/homedir/
, start Docker as shown in the first line of Listing 8. You will then find the whole directory tree including your home directory and all subfolders in /var/homedir/
.
Listing 8
File Access
01 $ docker run -it --rm \ 02 -v $HOME:/var/homedir ubuntu 03 $ docker run -it --rm \ 04 -v /home:/home \ 05 -v /etc/passwd:/etc/passwd \ 06 -v /etc/shadow:/etc/shadow \ 07 -v /etc/group:/etc/group ubuntu 08 $ docker cp /etc/passwd \ 09 22340547e6a3:/etc/passwd
You can set up several such mappings, and can even include individual files in the container, using -v
. If you use this method to map the /etc/passwd
, /etc/shadow
, and /etc/group
files, which are important for user management into the container, you can switch to your own account in the container with su
and work in your home directory in the usual way (Listing 8, starting with line 3).
Target directories in the container do not need to exist; Docker creates them on the fly. Figure 4 shows the same home directory from the host system and from within the container. Be careful: Changes you make to the files in the container also take effect on the host system. To be safe, it is a good idea to copy the files to the container instead.
File Transfer
Docker provides a separate command for transferring files into and out of the container. docker cp
lets you copy files in a similar way to the SSH scp tool. You cannot use it to copy files to an image; you first need to start a container.
Once the container is running, you will find its computer name at the prompt, which is an ID consisting of hexadecimal numbers. You can also search for the container with docker ps
. In the overview consisting of very long lines, in addition to the numeric ID, there is also a pretty name that Docker randomly assigns unless you specify one for the new container at startup time.
Both the numeric ID and the name can be used for the copy command (e.g., to copy the file /etc/passwd
to the container). Line 9 of Listing 8 copies the /etc/passwd
file from the host system to a container with an ID of 22340547e6a3
and to the same folder there, leaving the names unchanged.
« Previous 1 2 3 Next »
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
So Long Neofetch and Thanks for the Info
Today is a day that every Linux user who enjoys bragging about their system(s) will mourn, as Neofetch has come to an end.
-
Ubuntu 24.04 Comes with a “Flaw"
If you're thinking you might want to upgrade from your current Ubuntu release to the latest, there's something you might want to consider before doing so.
-
Canonical Releases Ubuntu 24.04
After a brief pause because of the XZ vulnerability, Ubuntu 24.04 is now available for install.
-
Linux Servers Targeted by Akira Ransomware
A group of bad actors who have already extorted $42 million have their sights set on the Linux platform.
-
TUXEDO Computers Unveils Linux Laptop Featuring AMD Ryzen CPU
This latest release is the first laptop to include the new CPU from Ryzen and Linux preinstalled.
-
XZ Gets the All-Clear
The back door xz vulnerability has been officially reverted for Fedora 40 and versions 38 and 39 were never affected.
-
Canonical Collaborates with Qualcomm on New Venture
This new joint effort is geared toward bringing Ubuntu and Ubuntu Core to Qualcomm-powered devices.
-
Kodi 21.0 Open-Source Entertainment Hub Released
After a year of development, the award-winning Kodi cross-platform, media center software is now available with many new additions and improvements.
-
Linux Usage Increases in Two Key Areas
If market share is your thing, you'll be happy to know that Linux is on the rise in two areas that, if they keep climbing, could have serious meaning for Linux's future.
-
Vulnerability Discovered in xz Libraries
An urgent alert for Fedora 40 has been posted and users should pay attention.