docker之创建MariaDB10.4.6镜像

基于Dockerfile方式创建

docker的安装

yum install -y yum-utils device-mapper-persistent-data lvm2 \
&& yum install docker -y \
&& systemctl enable docker \
&& systemctl restart docker

或安装docker-ce,装ce前已经安装的所有docker包要卸载。

yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
rpm --import https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
yum -y install docker-ce
systemctl enable docker
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
systemctl restart docker

方法一 直接拉取我的git

yum install git -y \
&& git clone https://github.com/106140873/dockermariadb10.git \
&& cd dockermariadb10 \
&& docker build -t huangshumao/mariadb10.4 ./

方法二 

设置创建目录和文件

mkdir maridb10
cd maridb10/
vi Dockerfile
vi entrypoint.sh

Dockerfile内容

[root@localhost mariadb10]# cat Dockerfile
FROM centos:latest
LABEL maintainer "huangshumao"
WORKDIR /opt
RUN set -ex \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && yum -y install kde-l10n-Chinese \
    && yum -y reinstall glibc-common \
    && yum update -y \
    && localedef -c -f UTF-8 -i zh_CN zh_CN.UTF-8 \
    && export LC_ALL=zh_CN.UTF-8 \
    && echo 'LANG="zh_CN.UTF-8"' > /etc/locale.conf \
    && yum clean all \
    && rm -rf /var/cache/yum/*
RUN set -ex \
    && echo -e "[mariadb]\nname = MariaDB\nbaseurl = http://mirrors.ustc.edu.cn/mariadb/yum/10.4/centos7-amd64\ngpgkey=http://mirrors.ustc.edu.cn/mariadb/yum/RPM-GPG-KEY-MariaDB\ngpgcheck=1" >/etc/yum.repos.d/mariadb.repo \
    && yum install MariaDB-server MariaDB-client -y \
    && yum clean all \
    && rm -rf /var/cache/yum/*

COPY readme.txt readme.txt
COPY entrypoint.sh /bin/entrypoint.sh
RUN chmod +x /bin/entrypoint.sh

VOLUME /var/lib/mysql
EXPOSE 3306
ENTRYPOINT ["entrypoint.sh"]

entrypoint.sh内容,tail -f 的作用是防止容器自动退出

[root@localhost mariadb10]# cat entrypoint.sh
#!/bin/bash
#
    if [ ! -d "/var/lib/mysql/mysql" ]; then
        mysql_install_db --user=mysql --datadir=/var/lib/mysql
        mysqld_safe &
        sleep 5s
    else
        mysqld_safe &
    fi
tail -f /opt/readme.txt

readme.txt

[root@localhost mariadb10]# cat readme.txt
进入容器命令 docker exec -it mariadb /bin/bash

创建镜像

docker build -t huangshumao/mariadb10.4 ./

创建容器,

[root@localhost mariadb10]# docker run -d huangshumao/mariadb10.4
797c2d68eeac5a648b41960d693730af7da2de78a5aeec64f6f785cbfce1c577
[root@localhost mariadb10]# docker ps
CONTAINER ID        IMAGE                            COMMAND             CREATED             STATUS              PORTS                                            NAMES
797c2d68eeac        huangshumao/mariadb10.4          "entrypoint.sh"     4 seconds ago       Up 3 seconds        3306/tcp                                         hungry_shtern

自定义容器名字和端口

docker run -d --name=mariadb10 -p 3306:3306 huangshumao/mariadb10.4

 

进入容器并验证

[root@localhost mariadb10]# docker exec -it hungry_shtern /bin/bash
[root@797c2d68eeac opt]# mysql
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 10.4.6-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

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

MariaDB [(none)]>

 

你可能感兴趣的:(DOCKER,DB-MYSQL,ORACLE,SQLSERVER)