此内容没有您所选择的语言版本。

Chapter 4. Install and Deploy a MariaDB Container


4.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
  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.
  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_2.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. Create the Dockerfile: Create the Dockerfile file shown below in the ~/mydbcontainer directory and modify it 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 --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
  5. Modify gss_db.sql: Look at the gss_db.sql file in the ~/mydbcontainer directory and modify it as needed:
  6. 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
    ...
  7. Start the database server container: To start the container image, run the following command:

    # docker run -d -p 3306:3306 --name=mydbforweb dbforweb
  8. 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.44-MariaDB?acL3YF31?X?FWbiiTIO2Kd6mysql_native_password Ctrl-C

4.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.

4.4. Attachments

Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2026 Red Hat
返回顶部