Chapter 2. Get Started with Containers 2.1. Install and Deploy an Apache Web Server Container 2.1.1. Overview A Web server is one of the most basic examples used to illustrate how containers work. The procedure in this topic does the following: Builds an Apache (httpd) Web server inside a container Exposes the service on port 80 of the host Serves a simple index.html file Displays data from a backend server (needs additional MariaDB container described later) 2.1.2. Creating and running the Apache Web Server Container Install system : Install a RHEL 7 or RHEL Atomic system that includes the docker package and start the docker service. Pull image : Pull the rhel7 image by typing the following: Get tarball with supporting files : Download the tarball file attached to this article (get it here: web_cont_3.tgz ), download it to a new mywebcontainer directory, and untar it as follows: Modify action CGI script : Edit the action file as needed, which will be used to get data from the backend database server container. This script assumes that the docker0 interface on the host system is at IP address 172.17.42.1 , you can login to the database with the dbuser1 user account and redhat as the password, and use the database named gss . If that is the IP address and you use the database container described later, you don't need to modify this script. (You can also just ignore this script and just use the Web server to get HTML content.) Check the Dockerfile : Modify the Dockerfile file in the ~/mywebcontainer directory as needed (perhaps only modify Maintainer_Name to add your name). Here are the contents of that file: Build Web server container: From the directory containing the Dockerfile file and other content, type the following: Start the Web server container: To start the container image, run the following command: Test the Web server container: To check that the Web server is operational, run the first curl command below. If you have the backend database container running, try the second command: If you have a Web browser installed on the localhost, you can open a Web browser to see as better representation of the few lines of output. Just open the browser to this URL: http://localhost/cgi-bin/action 2.1.3. Tips for this container Here are some tips to help you use the Web Server container: Modify for MariaDB: To use this container with the MariaDB container (described later), you may need to edit the action script and change the IP address from 172.17.42.1 to the host IP on the docker0 interface. To find what that address is on your host, type the following: Adding content: You can include your own content, mounted from the local host, by using the -v option on the docker run command line. For example: 2.1.4. Attachments Apache Web container tar file: action CGI script and Dockerfile 2.2. Install and Deploy a MariaDB Container 2.2.1. Overview Using MariaDB, you can set up a basic database in a container that can be accessed by other applications. The procedure in this topic does the following: Builds a MariaDB database server inside a docker formatted container Exposes the service on port 3306 of the host Starts up the database service to share a few pieces of information Allows a script from Web server to query the database (needs additional Web server container described later) Offers tips on how to use and extend this container 2.2.2. Creating and running the MariaDB Database Server Container Install system: Install a Red Hat Enterprise Linux 7 or Red Hat Enterprise Linux Atomic Host system that includes the docker package and start the docker service. Pull image: Pull the rhel7 image by typing the following: Get tarball with supporting files: Download the tarball file attached to this article ( mariadb_cont_2.tgz ), download it to a new mydbcontainer directory, and untar it as follows: Check the Dockerfile: Modify the Dockerfile file in the ~/mydbcontainer directory as needed (perhaps only modify Maintainer_Name to add your name). Here are the contents of that file: Build database server container: From the directory containing the Dockerfile file and other content, type the following: Start the database server container: To start the container image, run the following command: Test the database server container: Assuming the docker0 interface on the host is 172.17.42.1 (yours may be different), check that the database container is operational by running the nc command (in RHEL 7, type yum install nc to get it) as shown here: 2.2.3. Tips for this container Here are some tips to help you use the Web Server container: Adding your own database: You can include your own MariaDB content by copying your database file to the build directory and changing the name of the database file from gss_db.sql to the name of your database (in several places in the Dockerfile file). Orchestrate containers: A better way to manage this container with other containers is to use Kubernetes to orchestrate them into pods. 2.2.4. Attachments Tar file containing gss_db.sql database and Dockerfile files for MariaDB container

Commands:
docker pull rhel7:latest
mkdir ~/mywebcontainer cp web_cont*.tgz ~/mywebcontainer cd ~/mywebcontainer tar xvf web_cont*.tgz action Dockerfile
#!/usr/bin/python -*- coding: utf-8 -*- import MySQLdb as mdb import os con = mdb.connect(os.getenv('DB_SERVICE_SERVICE_HOST','172.17.42.1'), 'dbuser1', 'redhat', 'gss') with con: cur = con.cursor() cur.execute("SELECT MESSAGE FROM atomic_training") rows = cur.fetchall() print 'Content-type:text/html\r\n\r\n' print '<html>' print '<head>' print '<title>My Application</title>' print '</head>' print '<body>' for row in rows: print '<h2>' + row[0] + '</h2>' print '</body>' print '</html>' con.close()
Webserver container with CGI python script Using RHEL 7 base image and Apache Web server Version 1 Pull the rhel image from the local registry FROM rhel7:latest USER root MAINTAINER Maintainer_Name Fix per https://bugzilla.redhat.com/show_bug.cgi?id=1192200 RUN yum -y install deltarpm yum-utils --disablerepo=*-eus-* --disablerepo=*-htb-* *-sjis-* --disablerepo=*-ha-* --disablerepo=*-rt-* --disablerepo=*-lb-* --disablerepo=*-rs-* --disablerepo=*-sap-* RUN yum-config-manager --disable *-eus-* *-htb-* *-ha-* *-rt-* *-lb-* *-rs-* *-sap-* *-sjis* > /dev/null Update image RUN yum update -y RUN yum install httpd procps-ng MySQL-python -y Add configuration file ADD action /var/www/cgi-bin/action RUN echo "PassEnv DB_SERVICE_SERVICE_HOST" >> /etc/httpd/conf/httpd.conf RUN chown root:apache /var/www/cgi-bin/action RUN chmod 755 /var/www/cgi-bin/action RUN echo "The Web Server is Running" > /var/www/html/index.html EXPOSE 80 Start the service CMD mkdir /run/httpd ; /usr/sbin/httpd -D FOREGROUND
docker build -t webwithdb . Sending build context to Docker daemon 4.096 kB Sending build context to Docker daemon Step 0 : FROM rhel7:latest ---> bef54b8f8a2f Step 1 : USER root ---> Running in 00c28d347131 ---> cd7ef0fcaf55
docker run -d -p 80:80 --name=mywebwithdb webwithdb
curl http://localhost/index.html The Web Server is Running curl http://localhost/cgi-bin/action <html> <head> <title>My Application</title> </head> <body> <h2>RedHat rocks</h2> <h2>Success</h2> </body> </html> </tt></pre>
ip a | grep docker0 | grep inet inet 172.17.42.1/16 scope global docker0
docker run -d -p 80:80 -v /var/www/html:/var/www/html --name=mywebwithdb webwithdb
docker pull rhel7:latest
mkdir ~/mydbcontainer cp mariadb_cont*.tgz ~/mydbcontainer cd ~/mydbcontainer tar xvf mariadb_cont*.tgz gss_db.sql Dockerfile
Database container with simple data for a Web application Using RHEL 7 base image and MariahDB database Version 1 Pull the rhel image from the local repository FROM rhel7:latest USER root MAINTAINER Maintainer_Name Update image RUN yum update -y --disablerepo=*-eus-* --disablerepo=*-htb-* --disablerepo=*sjis* --disablerepo=*-ha-* --disablerepo=*-rt-* --disablerepo=*-lb-* --disablerepo=*-rs-* --disablerepo=*-sap-* RUN yum-config-manager --disable *-eus-* *-htb-* *-ha-* *-rt-* *-lb-* *-rs-* *-sap-* *-sjis-* > /dev/null Add Mariahdb software RUN yum -y install net-tools mariadb-server Set up Mariahdb database ADD gss_db.sql /tmp/gss_db.sql RUN /usr/libexec/mariadb-prepare-db-dir RUN test -d /var/run/mariadb || mkdir /var/run/mariadb; chmod 0777 /var/run/mariadb; /usr/bin/mysqld_safe --basedir=/usr & sleep 10s && /usr/bin/mysqladmin -u root password 'redhat' && mysql --user=root --password=redhat < /tmp/gss_db.sql && mysqladmin shutdown --password=redhat Expose Mysql port 3306 EXPOSE 3306 Start the service CMD test -d /var/run/mariadb || mkdir /var/run/mariadb; chmod 0777 /var/run/mariadb;/usr/bin/mysqld_safe --basedir=/usr
docker build -t dbforweb . Sending build context to Docker daemon 528.4 kB Sending build context to Docker daemon Step 0 : FROM rhel7:latest ---> bef54b8f8a2f Step 1 : USER root
docker run -d -p 3306:3306 --name=mydbforweb dbforweb
nc -v 172.17.42.1 3306 Ncat: Version 6.40 ( http://nmap.org/ncat ) Ncat: Connected to 172.17.42.1:3306. R 5.5.44-MariaDB?acL3YF31?X?FWbiiTIO2Kd6mysql_native_password Ctrl-C