Join our Discord Server
Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).

How to Link multiple Docker containers together?

12 min read

Today we are going to look at an interesting concept of linking containers. In our previous post, we pulled LAMP which is a complete stack of Linux, Apache, MySQL and PHP. What if I have Apache and MySQL as a seperate containers. How can I make it work together? This lead me to study https://docs.docker.com/userguide/dockerlinks/

The official link says “Network port mappings are not the only way Docker containers can connect to one another. Docker also has a linking system that allows you to link multiple containers together and send connection information from one to another. When containers are linked, information about a source container can be sent to a recipient container. This allows the recipient to see selected data describing aspects of the source container.”

Let’s try to implement this scenerio and see how it actually works:

This time I would like to try hands on Oracle Linux 7. Fortunately, there are lots of images in Docker registry and hence pulled one as shown:

[root@localhost ~]# docker pull oraclelinux:7
Pulling repository oraclelinux
919c8b6d612d: Download complete
8c3e49cb06dc: Download complete
2794e7de9041: Download complete
Status: Downloaded newer image for oraclelinux:7
[root@localhost ~]#

Let’s pick up MySQL server image from official Oracle repository:

[root@localhost ~]# docker pull mysql/mysql-server
Pulling repository mysql/mysql-server
296ef3e8959e: Download complete
511136ea3c5a: Download complete
ad98bd7101f2: Download complete
8f0a27825a9a: Download complete
c94023a2b919: Download complete
92af7065aa1f: Download complete
37a5e5ad845c: Download complete
4e1a8f6dcb3c: Download complete
0b5ee168e81e: Download complete
51526e13d844: Download complete
d8aaad0b8379: Download complete
Status: Downloaded newer image for mysql/mysql-server:latest
[root@localhost ~]#

Lets see what images has been pulled onto the Docker Engine:

[root@localhost ~]# docker images | grep oracle
oraclelinux 6 c500c430b7a6 4 weeks ago 161.8 MB
oraclelinux 6.6 c500c430b7a6 4 weeks ago 161.8 MB
oraclelinux 7 919c8b6d612d 4 weeks ago 199 MB
oraclelinux 7.1 919c8b6d612d 4 weeks ago 199 MB
oraclelinux latest 919c8b6d612d 4 weeks ago 199 MB
[root@localhost ~]# docker images | grep sql
mysql/mysql-server latest 296ef3e8959e 5 weeks ago 295.3 MB
[root@localhost ~]#

To build a customized container image, we must first build a guest container named webdeliverer, install the web server, and configure it to deliver web server content.

[root@localhost ~]# docker run -it –name webdeliverer oraclelinux:7 /bin/bash
[root@ff41a6f7922a /]#

Lets install apache and related services:

[root@ff41a6f7922a /]# yum install -y httpd perl && yum clean all
ol7_UEKR3 | 1.2 kB 00:00
ol7_latest | 1.4 kB 00:00
(1/5): ol7_UEKR3/x86_64/updateinfo | 32 kB 00:01
(2/5): ol7_latest/x86_64/group | 652 kB 00:01
(3/5): ol7_latest/x86_64/updateinfo | 375 kB 00:02
(4/5): ol7_UEKR3/x86_64/primary | 7.7 MB 00:05
(5/5): ol7_latest/x86_64/primary | 9.4 MB 00:07
ol7_UEKR3 185/185
ol7_latest 9582/9582
Resolving Dependencies
–> Running transaction check
—> Package httpd.x86_64 0:2.4.6-31.0.1.el7 will be installed
–> Processing Dependency: httpd-tools = 2.4.6-31.0.1.el7 for package: httpd-2.4.6-31.0.1.el7.x86_64
–> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-31.0.1.el7.x86_64
–> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-31.0.1.el7.x86_64
–> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-31.0.1.el7.x86_64
—> Package perl.x86_64 4:5.16.3-285.el7 will be installed
–> Processing Dependency: perl-libs = 4:5.16.3-285.el7 for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Scalar::Util) >= 1.10 for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Socket) >= 1.3 for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl-libs for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Carp) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(threads) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Scalar::Util) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Cwd) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Storable) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Time::Local) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Filter::Util::Call) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(File::Spec::Unix) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(File::Path) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Pod::Simple::XHTML) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(File::Spec::Functions) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(threads::shared) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(File::Spec) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Pod::Simple::Search) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Getopt::Long) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Exporter) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(constant) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl-macros for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Time::HiRes) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(Socket) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: perl(File::Temp) for package: 4:perl-5.16.3-285.el7.x86_64
–> Processing Dependency: libperl.so()(64bit) for package: 4:perl-5.16.3-285.el7.x86_64
–> Running transaction check
—> Package apr.x86_64 0:1.4.8-3.el7 will be installed
—> Package apr-util.x86_64 0:1.5.2-6.0.1.el7 will be installed
—> Package httpd-tools.x86_64 0:2.4.6-31.0.1.el7 will be installed
—> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
—> Package perl-Carp.noarch 0:1.26-244.el7 will be installed
—> Package perl-Exporter.noarch 0:5.68-3.el7 will be installed
—> Package perl-File-Path.noarch 0:2.09-2.el7 will be installed
—> Package perl-File-Temp.noarch 0:0.23.01-3.el7 will be installed
—> Package perl-Filter.x86_64 0:1.49-3.el7 will be installed
—> Package perl-Getopt-Long.noarch 0:2.40-2.el7 will be installed
–> Processing Dependency: perl(Pod::Usage) >= 1.14 for package: perl-Getopt-Long-2.40-2.el7.noarch
–> Processing Dependency: perl(Text::ParseWords) for package: perl-Getopt-Long-2.40-2.el7.noarch
—> Package perl-PathTools.x86_64 0:3.40-5.el7 will be installed
—> Package perl-Pod-Simple.noarch 1:3.28-4.el7 will be installed
–> Processing Dependency: perl(Pod::Escapes) >= 1.04 for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
–> Processing Dependency: perl(Encode) for package: 1:perl-Pod-Simple-3.28-4.el7.noarch
—> Package perl-Scalar-List-Utils.x86_64 0:1.27-248.el7 will be installed
—> Package perl-Socket.x86_64 0:2.010-3.el7 will be installed
—> Package perl-Storable.x86_64 0:2.45-3.el7 will be installed
—> Package perl-Time-HiRes.x86_64 4:1.9725-3.el7 will be installed
—> Package perl-Time-Local.noarch 0:1.2300-2.el7 will be installed
—> Package perl-constant.noarch 0:1.27-2.el7 will be installed
—> Package perl-libs.x86_64 4:5.16.3-285.el7 will be installed
—> Package perl-macros.x86_64 4:5.16.3-285.el7 will be installed
—> Package perl-threads.x86_64 0:1.87-4.el7 will be installed
—> Package perl-threads-shared.x86_64 0:1.43-6.el7 will be installed
–> Running transaction check
—> Package perl-Encode.x86_64 0:2.51-7.el7 will be installed
—> Package perl-Pod-Escapes.noarch 1:1.04-285.el7 will be installed
—> Package perl-Pod-Usage.noarch 0:1.63-3.el7 will be installed
–> Processing Dependency: perl(Pod::Text) >= 3.15 for package: perl-Pod-Usage-1.63-3.el7.noarch
–> Processing Dependency: perl-Pod-Perldoc for package: perl-Pod-Usage-1.63-3.el7.noarch
—> Package perl-Text-ParseWords.noarch 0:3.29-4.el7 will be installed
–> Running transaction check
—> Package perl-Pod-Perldoc.noarch 0:3.20-4.el7 will be installed
–> Processing Dependency: perl(parent) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
–> Processing Dependency: perl(HTTP::Tiny) for package: perl-Pod-Perldoc-3.20-4.el7.noarch
–> Processing Dependency: groff-base for package: perl-Pod-Perldoc-3.20-4.el7.noarch
—> Package perl-podlators.noarch 0:2.5.1-3.el7 will be installed
–> Running transaction check
—> Package groff-base.x86_64 0:1.22.2-8.el7 will be installed
—> Package perl-HTTP-Tiny.noarch 0:0.033-3.el7 will be installed
—> Package perl-parent.noarch 1:0.225-244.el7 will be installed
–> Finished Dependency Resolution

Dependencies Resolved

================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
httpd x86_64 2.4.6-31.0.1.el7 ol7_latest 1.2 M
perl x86_64 4:5.16.3-285.el7 ol7_latest 8.0 M
Installing for dependencies:
apr x86_64 1.4.8-3.el7 ol7_latest 99 k
apr-util x86_64 1.5.2-6.0.1.el7 ol7_latest 91 k
groff-base x86_64 1.22.2-8.el7 ol7_latest 933 k
httpd-tools x86_64 2.4.6-31.0.1.el7 ol7_latest 78 k
mailcap noarch 2.1.41-2.el7 ol7_latest 30 k
perl-Carp noarch 1.26-244.el7 ol7_latest 19 k
perl-Encode x86_64 2.51-7.el7 ol7_latest 1.5 M
perl-Exporter noarch 5.68-3.el7 ol7_latest 28 k
perl-File-Path noarch 2.09-2.el7 ol7_latest 26 k
perl-File-Temp noarch 0.23.01-3.el7 ol7_latest 56 k
perl-Filter x86_64 1.49-3.el7 ol7_latest 76 k
perl-Getopt-Long noarch 2.40-2.el7 ol7_latest 55 k
perl-HTTP-Tiny noarch 0.033-3.el7 ol7_latest 38 k
perl-PathTools x86_64 3.40-5.el7 ol7_latest 82 k
perl-Pod-Escapes noarch 1:1.04-285.el7 ol7_latest 49 k
perl-Pod-Perldoc noarch 3.20-4.el7 ol7_latest 86 k
perl-Pod-Simple noarch 1:3.28-4.el7 ol7_latest 216 k
perl-Pod-Usage noarch 1.63-3.el7 ol7_latest 26 k
perl-Scalar-List-Utils x86_64 1.27-248.el7 ol7_latest 35 k
perl-Socket x86_64 2.010-3.el7 ol7_latest 48 k
perl-Storable x86_64 2.45-3.el7 ol7_latest 76 k
perl-Text-ParseWords noarch 3.29-4.el7 ol7_latest 13 k
perl-Time-HiRes x86_64 4:1.9725-3.el7 ol7_latest 44 k
perl-Time-Local noarch 1.2300-2.el7 ol7_latest 24 k
perl-constant noarch 1.27-2.el7 ol7_latest 18 k
perl-libs x86_64 4:5.16.3-285.el7 ol7_latest 687 k
perl-macros x86_64 4:5.16.3-285.el7 ol7_latest 42 k
perl-parent noarch 1:0.225-244.el7 ol7_latest 12 k
perl-podlators noarch 2.5.1-3.el7 ol7_latest 111 k
perl-threads x86_64 1.87-4.el7 ol7_latest 48 k
perl-threads-shared x86_64 1.43-6.el7 ol7_latest 38 k

Transaction Summary
================================================================================
Install 2 Packages (+31 Dependent packages)

Total download size: 14 M
Installed size: 44 M
Downloading packages:
(1/33): apr-util-1.5.2-6.0.1.el7.x86_64.rpm | 91 kB 00:01
(2/33): apr-1.4.8-3.el7.x86_64.rpm | 99 kB 00:01
(3/33): groff-base-1.22.2-8.el7.x86_64.rpm | 933 kB 00:01
(4/33): httpd-2.4.6-31.0.1.el7.x86_64.rpm | 1.2 MB 00:01
(5/33): mailcap-2.1.41-2.el7.noarch.rpm | 30 kB 00:01
(6/33): httpd-tools-2.4.6-31.0.1.el7.x86_64.rpm | 78 kB 00:01
(7/33): perl-Carp-1.26-244.el7.noarch.rpm | 19 kB 00:00
(8/33): perl-Encode-2.51-7.el7.x86_64.rpm | 1.5 MB 00:01
(9/33): perl-5.16.3-285.el7.x86_64.rpm | 8.0 MB 00:04
(10/33): perl-Exporter-5.68-3.el7.noarch.rpm | 28 kB 00:01
(11/33): perl-File-Temp-0.23.01-3.el7.noarch.rpm | 56 kB 00:02
(12/33): perl-File-Path-2.09-2.el7.noarch.rpm | 26 kB 00:03
(13/33): perl-Filter-1.49-3.el7.x86_64.rpm | 76 kB 00:02
(14/33): perl-Getopt-Long-2.40-2.el7.noarch.rpm | 55 kB 00:02
(15/33): perl-HTTP-Tiny-0.033-3.el7.noarch.rpm | 38 kB 00:01
(16/33): perl-PathTools-3.40-5.el7.x86_64.rpm | 82 kB 00:01
(17/33): perl-Pod-Escapes-1.04-285.el7.noarch.rpm | 49 kB 00:00
(18/33): perl-Pod-Perldoc-3.20-4.el7.noarch.rpm | 86 kB 00:00
(19/33): perl-Pod-Simple-3.28-4.el7.noarch.rpm | 216 kB 00:00
(20/33): perl-Pod-Usage-1.63-3.el7.noarch.rpm | 26 kB 00:00
(21/33): perl-Scalar-List-Utils-1.27-248.el7.x86_64.rpm | 35 kB 00:00
(22/33): perl-Socket-2.010-3.el7.x86_64.rpm | 48 kB 00:00
(23/33): perl-Storable-2.45-3.el7.x86_64.rpm | 76 kB 00:00
(24/33): perl-Text-ParseWords-3.29-4.el7.noarch.rpm | 13 kB 00:00
(25/33): perl-Time-HiRes-1.9725-3.el7.x86_64.rpm | 44 kB 00:00
(26/33): perl-Time-Local-1.2300-2.el7.noarch.rpm | 24 kB 00:00
(27/33): perl-constant-1.27-2.el7.noarch.rpm | 18 kB 00:00
(28/33): perl-libs-5.16.3-285.el7.x86_64.rpm | 687 kB 00:00
(29/33): perl-macros-5.16.3-285.el7.x86_64.rpm | 42 kB 00:01
(30/33): perl-parent-0.225-244.el7.noarch.rpm | 12 kB 00:00
(31/33): perl-podlators-2.5.1-3.el7.noarch.rpm | 111 kB 00:00
(32/33): perl-threads-1.87-4.el7.x86_64.rpm | 48 kB 00:00
(33/33): perl-threads-shared-1.43-6.el7.x86_64.rpm | 38 kB 00:01
——————————————————————————–
Total 579 kB/s | 14 MB 00:24
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : apr-1.4.8-3.el7.x86_64 1/33
Installing : apr-util-1.5.2-6.0.1.el7.x86_64 2/33
Installing : httpd-tools-2.4.6-31.0.1.el7.x86_64 3/33
Installing : groff-base-1.22.2-8.el7.x86_64 4/33
Installing : 1:perl-parent-0.225-244.el7.noarch 5/33
Installing : perl-HTTP-Tiny-0.033-3.el7.noarch 6/33
Installing : perl-podlators-2.5.1-3.el7.noarch 7/33
Installing : perl-Pod-Perldoc-3.20-4.el7.noarch 8/33
Installing : 1:perl-Pod-Escapes-1.04-285.el7.noarch 9/33
Installing : perl-Encode-2.51-7.el7.x86_64 10/33
Installing : perl-Text-ParseWords-3.29-4.el7.noarch 11/33
Installing : perl-Pod-Usage-1.63-3.el7.noarch 12/33
Installing : 4:perl-macros-5.16.3-285.el7.x86_64 13/33
Installing : perl-Storable-2.45-3.el7.x86_64 14/33
Installing : perl-Exporter-5.68-3.el7.noarch 15/33
Installing : perl-constant-1.27-2.el7.noarch 16/33
Installing : perl-Time-Local-1.2300-2.el7.noarch 17/33
Installing : perl-Socket-2.010-3.el7.x86_64 18/33
Installing : perl-Carp-1.26-244.el7.noarch 19/33
Installing : 4:perl-Time-HiRes-1.9725-3.el7.x86_64 20/33
Installing : perl-PathTools-3.40-5.el7.x86_64 21/33
Installing : perl-Scalar-List-Utils-1.27-248.el7.x86_64 22/33
Installing : 1:perl-Pod-Simple-3.28-4.el7.noarch 23/33
Installing : perl-File-Temp-0.23.01-3.el7.noarch 24/33
Installing : perl-File-Path-2.09-2.el7.noarch 25/33
Installing : perl-threads-shared-1.43-6.el7.x86_64 26/33
Installing : perl-threads-1.87-4.el7.x86_64 27/33
Installing : perl-Filter-1.49-3.el7.x86_64 28/33
Installing : 4:perl-libs-5.16.3-285.el7.x86_64 29/33
Installing : perl-Getopt-Long-2.40-2.el7.noarch 30/33
Installing : 4:perl-5.16.3-285.el7.x86_64 31/33
Installing : mailcap-2.1.41-2.el7.noarch 32/33
Installing : httpd-2.4.6-31.0.1.el7.x86_64 33/33
Verifying : perl-HTTP-Tiny-0.033-3.el7.noarch 1/33
Verifying : 4:perl-macros-5.16.3-285.el7.x86_64 2/33
Verifying : mailcap-2.1.41-2.el7.noarch 3/33
Verifying : perl-threads-shared-1.43-6.el7.x86_64 4/33
Verifying : perl-Storable-2.45-3.el7.x86_64 5/33
Verifying : perl-Exporter-5.68-3.el7.noarch 6/33
Verifying : perl-constant-1.27-2.el7.noarch 7/33
Verifying : perl-PathTools-3.40-5.el7.x86_64 8/33
Verifying : 1:perl-Pod-Escapes-1.04-285.el7.noarch 9/33
Verifying : 1:perl-parent-0.225-244.el7.noarch 10/33
Verifying : apr-1.4.8-3.el7.x86_64 11/33
Verifying : groff-base-1.22.2-8.el7.x86_64 12/33
Verifying : perl-File-Temp-0.23.01-3.el7.noarch 13/33
Verifying : 1:perl-Pod-Simple-3.28-4.el7.noarch 14/33
Verifying : httpd-2.4.6-31.0.1.el7.x86_64 15/33
Verifying : apr-util-1.5.2-6.0.1.el7.x86_64 16/33
Verifying : perl-Time-Local-1.2300-2.el7.noarch 17/33
Verifying : perl-Socket-2.010-3.el7.x86_64 18/33
Verifying : perl-Carp-1.26-244.el7.noarch 19/33
Verifying : perl-podlators-2.5.1-3.el7.noarch 20/33
Verifying : 4:perl-Time-HiRes-1.9725-3.el7.x86_64 21/33
Verifying : perl-Scalar-List-Utils-1.27-248.el7.x86_64 22/33
Verifying : httpd-tools-2.4.6-31.0.1.el7.x86_64 23/33
Verifying : perl-Pod-Usage-1.63-3.el7.noarch 24/33
Verifying : perl-Encode-2.51-7.el7.x86_64 25/33
Verifying : 4:perl-5.16.3-285.el7.x86_64 26/33
Verifying : perl-Getopt-Long-2.40-2.el7.noarch 27/33
Verifying : perl-File-Path-2.09-2.el7.noarch 28/33
Verifying : perl-threads-1.87-4.el7.x86_64 29/33
Verifying : perl-Filter-1.49-3.el7.x86_64 30/33
Verifying : perl-Pod-Perldoc-3.20-4.el7.noarch 31/33
Verifying : perl-Text-ParseWords-3.29-4.el7.noarch 32/33
Verifying : 4:perl-libs-5.16.3-285.el7.x86_64 33/33

Installed:
httpd.x86_64 0:2.4.6-31.0.1.el7 perl.x86_64 4:5.16.3-285.el7

Dependency Installed:
apr.x86_64 0:1.4.8-3.el7
apr-util.x86_64 0:1.5.2-6.0.1.el7
groff-base.x86_64 0:1.22.2-8.el7
httpd-tools.x86_64 0:2.4.6-31.0.1.el7
mailcap.noarch 0:2.1.41-2.el7
perl-Carp.noarch 0:1.26-244.el7
perl-Encode.x86_64 0:2.51-7.el7
perl-Exporter.noarch 0:5.68-3.el7
perl-File-Path.noarch 0:2.09-2.el7
perl-File-Temp.noarch 0:0.23.01-3.el7
perl-Filter.x86_64 0:1.49-3.el7
perl-Getopt-Long.noarch 0:2.40-2.el7
perl-HTTP-Tiny.noarch 0:0.033-3.el7
perl-PathTools.x86_64 0:3.40-5.el7
perl-Pod-Escapes.noarch 1:1.04-285.el7
perl-Pod-Perldoc.noarch 0:3.20-4.el7
perl-Pod-Simple.noarch 1:3.28-4.el7
perl-Pod-Usage.noarch 0:1.63-3.el7
perl-Scalar-List-Utils.x86_64 0:1.27-248.el7
perl-Socket.x86_64 0:2.010-3.el7
perl-Storable.x86_64 0:2.45-3.el7
perl-Text-ParseWords.noarch 0:3.29-4.el7
perl-Time-HiRes.x86_64 4:1.9725-3.el7
perl-Time-Local.noarch 0:1.2300-2.el7
perl-constant.noarch 0:1.27-2.el7
perl-libs.x86_64 4:5.16.3-285.el7
perl-macros.x86_64 4:5.16.3-285.el7
perl-parent.noarch 1:0.225-244.el7
perl-podlators.noarch 0:2.5.1-3.el7
perl-threads.x86_64 0:1.87-4.el7
perl-threads-shared.x86_64 0:1.43-6.el7

Complete!
Cleaning repos: ol7_UEKR3 ol7_latest
Cleaning up everything
[root@ff41a6f7922a /]#
Now let’s add content for the web server to display in the deliverer container:

[root@ff41a6f7922a /]# echo “Welcome to Collabnix Web Server Deliverer” > /var/www/html/index.html

Use exit command to stop a running container:

[root@ff41a6f7922a /]# exit
exit
[root@localhost ~]#

Let’s create a new Docker image that reflects the contents of the webdeliverer container that I just configured.
The following docker commit command captures the modified container into a new image named collabnix/ol7-webdeliverer:v1
[root@localhost ~]# docker commit -m “OracleLinux-WebDeliverer” `docker ps -l -q` collabnix/ol7-webdeliverer:v1
70deec1a2aacc1f24e1338f8f11186c3a431b0a789b0f75a22101ab7e6019a68

Let’s verify it:

[root@localhost ~]# docker images
REPOSITORY TAG IMAGE ID CREATED VIRTUAL SIZE
collabnix/ol7-webdeliverer v1 70deec1a2aac About a minute ago 257.9 MB

We don’t need webdeliverer container anymore now since we have created a image out of it:

[root@localhost ~]# docker rm webdeliverer
webdeliverer
[root@localhost ~]#
Deploying the configured Docker Image:

Now we can create multiple web servers using new Docker image as template as shown:
[root@localhost ~]# docker run -d –name web2 -p 8081:80 collabnix/ol7-webdeliverer:v1 /usr/sbin/httpd -D
FOREGROUND
70dc9263262d59c9aaddc1c6aaa98744a4e2965b435af8f6a967dcd91dbe27e7

[root@localhost ~]# docker run -d –name web3 -p 8082:80 collabnix/ol7-webdeliverer:v1 /usr/sbin/httpd -D
FOREGROUND
24aff6d521e75de967ad41681be9b6e89e32e920bd55eccbae1dcfd3ecb9413e
[root@localhost ~]#

Let’s verify:

[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
24aff6d521e7 collabnix/ol7-webdeliverer:v1 “/usr/sbin/httpd -D 33 seconds ago Up 31 seconds
0.0.0.0:8082->80/tcp web3
70dc9263262d collabnix/ol7-webdeliverer:v1 “/usr/sbin/httpd -D 45 seconds ago Up 43 seconds
0.0.0.0:8081->80/tcp web2
Let’s open up text web browser to see if Web page comes up:

[root@localhost ~]# curl http://localhost:8081
Welcome to Collabnix Web Server Deliverer
[root@localhost ~]# curl http://localhost:8082
Welcome to Collabnix Web Server Deliverer

You can check at the web browser too:

oracle7-docker

Let’s publish to Docker registry:

[root@localhost ~]# docker push collabnix/ol7-webdeliverer
The push refers to a repository [collabnix/ol7-webdeliverer] (len: 1)
Sending image list
Pushing repository collabnix/ol7-webdeliverer (1 tags)
8c3e49cb06dc: Image already pushed, skipping
2794e7de9041: Image already pushed, skipping
919c8b6d612d: Image already pushed, skipping
70deec1a2aac: Image successfully pushed
Pushing tag for rev [70deec1a2aac] on {https://cdn-registry-1.docker.io/v1/repositories/collabnix/ol7-
webdeliverer/tags/v1}
[root@localhost ~]#

oracle-linux
How to save the image as tar?

[root@localhost ~]# docker save -o webdeliverer.tar collabnix/ol7-webdeliverer:v1
How to limit CPU and memory on the container?

[root@localhost ~]# docker run -it collabnix/ol7-webdeliverer:v1 /bin/bash
[root@d6091eeb3298 /]#
[root@d6091eeb3298 /]#
[root@d6091eeb3298 /]#
[root@d6091eeb3298 /]# free -m
total used free shared buff/cache available
Mem: 1876 159 330 0 1386 1541
Swap: 4031 0 4031
[root@d6091eeb3298 /]#

Say, I want to limit the memory to 512MB rather than 1876MB. Here is the way to do it:

docker run -d –memory=512m -p 8089:80 collabnix/ol7-webdeliverer:v1

Connecting a Web Server Container to a MySQL Container:

Suppose I want to connect my web server container to a container running an instance of MySQL. Docker makes it
possible to link containers together, creating a secure channel for one container to access certain information
from another.

Previously I pulled the Oracle-verified MySQL image from the Docker Hub using the command docker pull
mysql/mysql-server. To run the MySQL image as a container named db, I enter the following command:

[root@localhost ~]# docker run –name db -d -e MYSQL_ROOT_PASSWORD=mysql123 -v /home/data:/var/lib/mysql
mysql/mysql-server
1c35297d6999d00c2f49ca2267e3d21c4e8fe7f23437f0c3b9f27c7376a04ef6

[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS
PORTS NAMES
1c35297d6999 mysql/mysql-server:latest “/entrypoint.sh mysq 5 seconds ago Up 4 seconds

The Docker Hub mysql/mysql-server image includes an entrypoint script (entrypoint.sh) that sets up the database
server automatically, initializing mysqld. I can view log messages generated from a container using the docker
logs command:

[root@localhost ~]# docker logs db
Running mysql_install_db
2015-05-21 07:05:40 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use —
explicit_defaults_for_timestamp server option (see documentation for more details).
2015-05-21 07:05:40 0 [Note] /usr/sbin/mysqld (mysqld 5.6.24) starting as process 22 …
2015-05-21 07:05:44 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use —
explicit_defaults_for_timestamp server option (see documentation for more details).
2015-05-21 07:05:44 0 [Note] /usr/sbin/mysqld (mysqld 5.6.24) starting as process 44 …
PLEASE REMEMBER TO SET A PASSWORD FOR THE MySQL root USER !
To do so, start the server, then issue the following commands:

/usr/bin/mysqladmin -u root password ‘new-password’
/usr/bin/mysqladmin -u root -h 1c35297d6999 password ‘new-password’

Alternatively you can run:

/usr/bin/mysql_secure_installation

which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.

See the manual for more instructions.

Please report any problems at http://bugs.mysql.com/

The latest information about MySQL is available on the web at

http://www.mysql.com

Support MySQL by buying support/licenses at http://shop.mysql.com

Note: new default config file not created.
Please make sure your config file is current

WARNING: Default config file /etc/my.cnf exists on the system
This file will be read by default by the MySQL server
If you do not want to use this, either remove it, or use the
–defaults-file argument to mysqld_safe when starting the server

Finished mysql_install_db
MySQL init process in progress…
2015-05-21 07:05:46 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use —
explicit_defaults_for_timestamp server option (see documentation for more details).
2015-05-21 07:05:46 0 [Note] mysqld (mysqld 5.6.24) starting as process 67 …
MySQL init process in progress…
MySQL init process in progress…
MySQL init process done. Ready for start up.
2015-05-21 07:05:49 0 [Warning] TIMESTAMP with implicit DEFAULT value is deprecated. Please use —
explicit_defaults_for_timestamp server option (see documentation for more details).
2015-05-21 07:05:49 0 [Note] mysqld (mysqld 5.6.24) starting as process 1 …
[root@localhost ~]#
[root@localhost ~]# docker run –name db -d -e MYSQL_ROOT_PASSWORD=mysql123 -v /home/data:/var/lib/mysql
mysql/mysql-server^C
[root@localhost ~]# docker exec -it db bash
[root@1c35297d6999 /]# ls /var/lib/mysql
auto.cnf ib_logfile1 mysql performance_schema
ib_logfile0 ibdata1 mysql.sock
[root@1c35297d6999 /]#
Lets try logging in MySQL:

[root@1c35297d6999 /]# mysql -h 127.0.0.1 -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.24 MySQL Community Server (GPL)

Copyright (c) 2000, 2015, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

mysql>

By linking containers using the –link flag, other application containers can access the MySQL database running in
the db container. This simplifies the separation of database, application, and web services, making it easier to
isolate services in different containers. I can run my web server image as a container and connect it to the
database container using the following command:

$docker run -d –name web1 -v /home/data -p 80:80 \
–link db:db collabnix/webdeliverer:v1

Assuming /etc/httpd/conf/httpd.conf is configured to allow CGI script execution on the web server and some perl script is configured to perform some kind of SQL query, I can run queries against the database in the db container (note
that I can use the docker inspect command to obtain the web server IP address I need for the query):

[root@ol7-host ~]# curl http://172.17.0.5/cgi-bin/doquery.pl?150
+—————————+
| customerName |
+—————————+
| Google Inc |
| JP Morgan |
| Cloudbytes India Pvt Ltd |
| …
+—————————+
12 rows in set (0.00 sec)

Have Queries? Join https://launchpass.com/collabnix

Ajeet Raina Ajeet Singh Raina is a former Docker Captain, Community Leader and Arm Ambassador. He is a founder of Collabnix blogging site and has authored more than 570+ blogs on Docker, Kubernetes and Cloud-Native Technology. He runs a community Slack of 8900+ members and discord server close to 2200+ members. You can follow him on Twitter(@ajeetsraina).
Join our Discord Server