Building a Simple Database Server in a Container

Updated -

Using MariaDB, you can set up a basic database in a container that can be accessed by other applications. The procedure in this document 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 from here)
  • Offers tips on how to use and extend this container

Creating and running the MariaDB Database Server Container

  1. 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. See Get Started with Docker Containers for details.

  2. Pull image: Pull the rhel7 image by typing the following:

    # docker pull rhel7:latest
    
  3. Get tarball with supporting files: Download the tarball file attached to this article (mariadb_cont*.tgz), download it to a new mydbcontainer directory, and untar it as follows:

    # mkdir ~/mydbcontainer
    # cp mariadb_cont*.tgz ~/mydbcontainer
    # cd ~/mydbcontainer
    # tar xvf mariadb_cont*.tgz
    gss_db.sql
    Dockerfile
    
  4. 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:

    # 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
    
    # Add Mariahdb software
    RUN yum -y install mariadb-server
    
    # Set up Mariahdb database
    ADD gss_db.sql /tmp/gss_db.sql
    RUN /usr/libexec/mariadb-prepare-db-dir
    RUN /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 ["--basedir=/usr"]
    ENTRYPOINT ["/usr/bin/mysqld_safe"]
    
  5. Build database server container: From the directory containing the Dockerfile file and other content, type the following:

    # 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
    ...
    
  6. Start the database server container: To start the container image, run the following command:

    # docker run -d -p 3306:3306 --name=mydbforweb dbforweb
    
  7. 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:

    # 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.40-MariaDB?acL3YF31?X?FWbiiTIO2Kd6mysql_native_password Ctrl-C
    

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. See Get Started Orchestrating Docker Containers with Kubernetes for a description of how to get started using Kubernetes.

Attachments

7 Comments

FYI, I had to manually install netcat, yum install nc, on my rhel7.1-4 host.

Good point. The nc command is already in RHEL Atomic. I added a few words to note how to install nc.

mariadb_cont.tgz isn't a . t(tarred)gz(gzipped) file - it's just tarred.
The instructions for extracting it are correct but the extension implies otherwise

This may be a little off-topic, but I can't build this container because the container doesn't see the registration or repos assigned through the satellite server. Anyone know why this might happen? I can't find anything anywhere. Thanks.

Are you on RHEL7 or RHEL7 Atomic with a registered (subscription-manager register --auto-attach) to Satellite 6.X? RHEL6/Satellite 5 will not automatically pass subscription info.

If not, it will not pass the subscription information through to the container and you will have to do some funky stuff like:

subscription-manager/rhn_register inside the container/Docker file, then run commands similar to : https://github.com/fatherlinux/docker-bench-security/blob/master/distros/Dockerfile.rhel

Just to add a little on the Sat 6/container discussion. The following links may be useful for those looking to use RHEL Atomic and containers with Satellite 6.
http://videos.cdn.redhat.com/summit2015/presentations/13831_operating-and-managing-an-atomic-container-based-infrastructure.pdf
https://access.redhat.com/blogs/1169563/posts/1318283

Expect many of the manual steps to become more seamless in the future. :)

With recent builds of Atomic I've needed to apply the same changes to the Dockerfile as the Apache Demo
* https://access.redhat.com/articles/1328953

RUN yum -y install deltarpm yum-utils --disablerepo=*-eus-* --disablerepo=*-htb-* \
    --disablerepo=*-ha-* --disablerepo=*-rt-* --disablerepo=*-lb-* --disablerepo=*-rs-* --disablerepo=*-sap-*

RUN yum-config-manager --disable *-eus-* *-htb-* *-ha-* *-rt-* *-lb-* *-rs-* *-sap-* > /dev/null