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)"
Figure 2: If you are testing different Ubuntu versions, you will find the version information in /etc/os-release.

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.

Figure 3: The Tags subpage for an image on Docker Hub reveals which versions you can use.

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.

Figure 4: You can map individual files or entire directories from the host to the container.

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.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Linux Magazine

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Docker Open Source Developer Tools

    Docker provides the open source tools and resources for compiling, building, and testing containerized applications.

  • Tutorials – Docker

    You might think Docker is a tool reserved for gnarly sys admins, useful only to service companies that run complicated SaaS applications, but that is not true: Docker is useful for everybody.

  • Docker with OwnCloud

    Run your application smoothly and portably in the cloud with the Docker container system. This workshop takes a practical look deploying Docker with the OwnCloud cloud environment.

  • Perl: Testing Modules with Docker

    If you want to distribute your programs across multiple platforms, you need to prepare them to run in foreign environments from the start. Linux container technology and the resource-conserving Docker project let you test your own Perl modules on several Linux distributions in one fell swoop.

  • Docker

    Docker is an economical alternative to conventional virtualization. Because each Docker container shares the underlying operating system, it enjoys the resource isolation and allocation benefits of VMs but is much more portable and efficient.

comments powered by Disqus
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.

Learn More

News