Configuring Docker containers in the OwnCloud cloud
Docker Talk
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.
If you focus on virtualization today, you've surely encountered the new world of Linux container technology. Docker provides a powerful, enterprise-level tool that makes it easy for admins to roll out new and lightweight instances of their applications.
This workshop shows how to configure Docker [1] with the OwnCloud cloud service and demonstrates Docker at work with complex applications that require a web server, a database, and persistent storage.
Getting Started
It does not matter whether you use the Docker as your container manager: The basis for any container is always an image that consists of the distribution filesystem. You can either use pre-built images or build an image yourself. The do-it-yourself approach has advantages, and once you build an image, you can modify it later to adapt it to new situations.
Docker requires an instruction file, known as the Dockerfile, that describes the operation for creating the image. It is important to be familiar with some naming conventions: The filename of each Dockerfile must start with an uppercase letter. A Dockerfile consists of several consecutive instructions, where each line contains a command followed by a parameter. Commands, for example, RUN
, must be written out in full and entirely in uppercase letters. Parameters can consist of several words, as is typically the case with Linux command lines that use their own parameters, such as, chmod +x ./My_Script.sh
.
Listing 1 shows the Dockerfile for the image of a MySQL database. The base image is selected in the FROM
line. The name centos:latest
refers to the centos
image with the latest
tag as another, subordinate, specification: latest
always means the latest revision of the image. Releases are identified as in this example: ubuntu:12.04
.
Listing 1
Dockerfile for MySQL
To find the image, Docker connects with the central registry or the hub (formerly known as the index). Users exchange images via this publicly accessible platform, so admins usually do not have to build an image from scratch.
Pre-built images are available for many applications. You can find an image using the search function on the Docker website [2] or the docker search pattern_desc
command. If the specified image is not available locally, Docker will automatically download it. Alternatively, you can retrieve an image with the docker pull image_name
command.
If a line starts with RUN
, Docker executes the command that follows when it creates the image, but not when running the container. The specified command must not require interaction on the part of the administrator. Commands that require interactive input must be appropriately encapsulated – for example with expect
– or configured with a matching parameter, so that Docker can continue to run without any input (e.g., yum install -y
). RUN
commands can occur any number of times in Dockerfiles.
The lines in the Dockerfile are processed sequentially when creating an image with docker build
. Each command in the Dockerfile automatically creates a new image, which Docker only stores as a diff to the previously stored image. Rerunning docker build
does not lead to the commands being re-executed; instead, Docker reads the results from the cache to save time.
If one intermediate step defined in Dockerfile changes, Docker must also repeat all the subsequent steps. The relationship between different images is revealed in a chart created by running docker images -viz | dot -T png -o docker-images.png
(Figure 1).
The ADD
command adds files and directories to the image. In the MySQL Dockerfile, ADD
commands are included through the start.sh
script, which is stored as the /start
file in the image. If you want to add many files to an image, you can also bundle them in a tarball, which you pass in directly as an argument to ADD
. You can also stipulate URLs, such as http://server_name.fqdn/myfile.sh.
ENTRYPOINT vs. CMD
The CMD
or ENTRY POINT
keyword determines which command will run by default when executing a container. In most cases, this is either the binary of a service, a startup script, or an init replacement, such as supervisor
or runit
.
Note the subtle difference: CMD /start
tells Docker to use /bin/sh -c
, or to be more precise /bin/sh -c '/start'
, to run the command. If you want to run the command directly, you need to pass it in as a list; for example:
CMD [ 'Program', '--Argument1', '--Argument2' ]
The main difference between CMD
and ENTRYPOINT
becomes apparent at run time; Listing 2 shows an example in the interactive Docker shell. Both images run the /bin/echo
program. If you specify CMD
, you can override the command specified in the Dockerfile, but not with ENTRYPOINT
(see the example in Listing 2).
Listing 2
Difference between ENTRYPOINT and CMD
The instruction set for Dockerfiles is very extensive [2]. Michael Crosby's website [3] provides more useful information about the many options in Dockerfile. Listing 3 shows a complete Dockerfile for the OwnCloud scenario [4].
Listing 3
Dockerfile for OwnCloud
Building Images
After you have completed your Dockerfile, you need to build the actual image. The docker build
command needs the path to the directory containing the Dockerfile as a mandatory parameter. A path can also be a URL – or even the standard input. Additionally, it makes sense to name the resulting image directly and add a tag so that you can easily reference it later:
$ cd /path/to/Dockerfile $ docker build -t myimage:test .
After building an image by reference to a Dockerfile, you can create and run containers based on the image. Listing 4 shows the output of the docker images
command, which lists the locally available images.
Listing 4
Listing the Locally Available Images
To start a container, just type docker run owncloud_httpd:latest
, which executes the container in the foreground. The -d
parameter starts the container in the background. After running, Docker returns the checksum of the started container.
docker run -d centos:latest sleep 30 a6c462a7999ab01db374ffe502b02d5aac1cd6
This option proves especially useful when used in scripts. To start an interactive session in a container, you need the -i
and -t
parameters. -i
stands for interactive
and binds the container to the standard input; -t
allocates a pseudo-terminal, and --rm
lets you automatically delete the stopped container:
docker run -i -t --rm fedora:latest /bin/bash bash-4.2# cat /etc/fedora-release Fedora release 20 (Heisenbug)
The --rm
option is useful for tests with containers that you do not want to continue after stopping them.
This procedure also makes it possible to start an interactive session and transfer the results (e.g., package installations or configuration changes) to a new image (Listing 5). The --name
parameter in this example directly assigns a name, which the user can use later to access this container. If you do not specify --name
, Docker generates a random name.
Listing 5
Building an Image Interactively
After a successful docker commit
, you can now start a new container:
docker run -d myhttpd:latestapachectl -DFOREGROUND
The docker ps
command displays only containers that are currently running. If Docker has stopped a container because the executed command was completed, the administrator must use docker ps -a
to see all the containers.
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
-
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.
-
Canonical Bumps LTS Support to 12 years
If you're worried that your Ubuntu LTS release won't be supported long enough to last, Canonical has a surprise for you in the form of 12 years of security coverage.
-
Fedora 40 Beta Released Soon
With the official release of Fedora 40 coming in April, it's almost time to download the beta and see what's new.
-
New Pentesting Distribution to Compete with Kali Linux
SnoopGod is now available for your testing needs
-
Juno Computers Launches Another Linux Laptop
If you're looking for a powerhouse laptop that runs Ubuntu, the Juno Computers Neptune 17 v6 should be on your radar.