Configuring Docker containers in the OwnCloud cloud
Forwarding Ports for Access via the Network
The command docker run
supplemented with the -p
switch, starts the image with a network connection in the container. The following example enables forwarding of the host port 13306 to the container port 3306 (i.e., the MySQL server):
docker run -d -p 13306:3306 mysql
Docker implements the routing by means of a simple iptables NAT rule on the host system. If you additionally want to bind the forwarding port to an IP address, you need to type the IP address, followed by a colon, and the port number. For the loopback address, the parameter value would be 127.0.0.1:13306:3306
. If you leave out the 13306
, as in 127.0.0.1::3306
, Docker randomly selects a forwarding port from the range 49000 through 49900.
In the configuration file, the EXPOSE
keyword defines which network ports to provide in the container. The parameter value for EXPOSE
is a space-delimited list of single ports. A configuration line might look like:
EXPOSE 3306
This line tells Docker to use the container port 3306. If you use the EXPOSE
setting in the Dockerfile to stipulate all open ports in the container, you can enable them all at the same time with the -P
switch when starting the container. Docker takes care of dynamic port assignment on the host, an assignment using the -p
switch is no longer possible in this case. An example on the host might look like:
docker run -d -P mysql
If you set both the -p
option and the -P
option; values with the lowercase p
take priority.
You can list the current redirects with two commands: The docker ps
command provides information about the network, but also run-time data for the image; the required data can be found in the PORTS column. If there an arrow (->), this is a redirect, but if you only see a port number, the port is open, and other containers are allowed to use it:
docker ps ... PORTS ... 0.0.0.0:5556->3306/tcp
The docker port
command additionally requires the name or ID of the started container and replies with the addressable port numbers in the container. The command
docker port mysql 3306 0.0.0.0:13306
addresses a container named mysql
.
Connecting Containers
The linking mechanism connects containers securely on the network, without opening ports to the outside world. This connection occurs via a network bridge (Figure 2), which is managed by Docker and to which each container is connected. If you want to use the connection to the mysql
database container shown in the example, you additionally need to stipulate the --link mysql:db
option on starting the web container. Docker requires the name of the linked container, mysql
, and an arbitrary alias – db
in this case (Listing 6).
Listing 6
Linking to the mysql Container
You can use this alias within the current web container to keep the connection data for the MySQL database. Docker provides the data in the form of environment variables, indicated by env
, starting with the alias of the first process started.
In addition to the existing environment variables, the communication partner receives an entry in its /etc/hosts
file; in this case, the IP address associated with the selected alias, which was automatically assigned by Docker. Direct access to the database container is thus possible via the hostname db
. Since linked containers enter a kind of parent-child relationship, it is quite easy to start multiple child containers. Several web containers can thus communicate with a database container.
Persistent Volumes
An advantage of containers and images is that the admin can always start from a defined state. Any change that occurs in the current container is discarded as soon as the container is stopped and deleted. This lack of permanence is a disadvantage if the containers store useful data that you need to keep persistently. Volumes are the answer: A volume is a directory on the host system that you can mount at any point in the container's filesystem (Listing 7).
Listing 7
Creating a Volume
The -v /tmp/test:/foo
parameter ensures that the /tmp/test
directory is available as /foo
in the container. In the case of the database server for OwnCloud, the following call makes sense:
docker run -d -v /data/mysql:/var/lib/mysql
Docker automatically creates the specified directory structure on the host if it does not exist. You might need to modify the filesystem permissions, say, so that a service container that runs in an unprivileged user context can also access the directory.
You will also want to make sure only one database container accesses one volume. Unfortunately, associating a container with a volume compromises flexibility, since you no longer simply migrate an image for a service to another computer – when planning your configuration, take care also to install a separate mechanism for distributing the user data.
« 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
-
Gnome 48 Debuts New Audio Player
To date, the audio player found within the Gnome desktop has been meh at best, but with the upcoming release that all changes.
-
Plasma 6.3 Ready for Public Beta Testing
Plasma 6.3 will ship with KDE Gear 24.12.1 and KDE Frameworks 6.10, along with some new and exciting features.
-
Budgie 10.10 Scheduled for Q1 2025 with a Surprising Desktop Update
If Budgie is your desktop environment of choice, 2025 is going to be a great year for you.
-
Firefox 134 Offers Improvements for Linux Version
Fans of Linux and Firefox rejoice, as there's a new version available that includes some handy updates.
-
Serpent OS Arrives with a New Alpha Release
After months of silence, Ikey Doherty has released a new alpha for his Serpent OS.
-
HashiCorp Cofounder Unveils Ghostty, a Linux Terminal App
Ghostty is a new Linux terminal app that's fast, feature-rich, and offers a platform-native GUI while remaining cross-platform.
-
Fedora Asahi Remix 41 Available for Apple Silicon
If you have an Apple Silicon Mac and you're hoping to install Fedora, you're in luck because the latest release supports the M1 and M2 chips.
-
Systemd Fixes Bug While Facing New Challenger in GNU Shepherd
The systemd developers have fixed a really nasty bug amid the release of the new GNU Shepherd init system.
-
AlmaLinux 10.0 Beta Released
The AlmaLinux OS Foundation has announced the availability of AlmaLinux 10.0 Beta ("Purple Lion") for all supported devices with significant changes.
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.