Docker Image | Dockerfile | 基于Dockerfile方式创建MariaDB镜像

1 设置创建目录和文件

[root@test01 ~]# mkdir mariadb_dockerfile && cd mariadb_dockerfile
[root@test01 mariadb_dockerfile]# touch db_init.sh 
[root@test01 mariadb_dockerfile]# touch run.sh

2 编辑Dockerfile等文件

Dockerfile
[root@test01 mariadb_dockerfile]# cat Dockerfile 
#使用的基础镜像
FROM centos:7.4.1708

#添加作者信息
MAINTAINER liuxin 842887233@qq.com

#安装mariadb数据库
RUN yum -y install mariadb-server

#设置环境变量,便于管理
ENV MARIADB_USER root
ENV MARIADB_PASS 123456
#让容器支持中文
ENV LC_ALL en_US.UTF-8

#初始化数据库
ADD db_init.sh /root/db_init.sh
RUN chmod 775 /root/db_init.sh
RUN /root/db_init.sh

#导出端口
EXPOSE 3306

#添加启动文件
ADD run.sh /root/run.sh
RUN chmod 775 /root/run.sh

#设置默认启动命令
CMD ["/root/run.sh"]
db_init.sh
[root@test01 mariadb_dockerfile]# cat db_init.sh 
#!/bin/bash

mysql_install_db --user=mysql
sleep 3
mysqld_safe &
sleep 3
#mysqladmin -u "$MARIADB_USER" password "$MARIADB_PASS"
mysql -e "use mysql; grant all privileges on *.* to '$MARIADB_USER'@'%' identified by '$MARIADB_PASS' with grant option;"
h=$(hostname)
mysql -e "use mysql; update user set password=password('$MARIADB_PASS') where user='$MARIADB_USER' and host='$h';"
mysql -e "flush privileges;"
run.sh
[root@test01 mariadb_dockerfile]# cat run.sh 
#!/bin/bash
mysqld_safe

3  创建镜像
[root@test01 mariadb_dockerfile]# docker build -t liuxin/centos-mariadb:v1 ./
4 创建容器
[root@test01 mariadb_dockerfile]# docker run -d -p 13306:3306 liuxin/centos-mariadb:v1 /root/run.sh
[root@test01 mariadb_dockerfile]# docker ps
CONTAINER ID        IMAGE                      COMMAND             CREATED             STATUS              PORTS                     NAMES
7743527ac603        liuxin/centos-mariadb:v1   "/root/run.sh"      5 seconds ago       Up 3 seconds        0.0.0.0:13306->3306/tcp   nostalgic_mirzakhani
5 验证登录
[root@test01 mariadb_dockerfile]# yum -y install mariadb

[root@master mariadb_dockerfile]# docker ps |grep liuxin/centos-mariadb:v1
efbd88a6d952        liuxin/centos-mariadb:v1               "/root/run.sh"           3 minutes ago       Up 3 minutes           0.0.0.0:13306->3306/tcp     optimistic_shaw

[root@test01 mariadb_dockerfile]#  docker inspect efbd88a6d952
**"IPAddress": "172.17.0.2",**
[root@test01 mariadb_dockerfile]# mysql -uroot -h 127.0.0.2 --port=13306 -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 1
Server version: 5.5.56-MariaDB MariaDB Server

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

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

MariaDB [(none)]> exit
  • 4
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Docker Swarm是Docker的原生集群管理工具,可以用于在多个主机上运行和管理容器。Dockerfile是一种用于定义Docker镜像构建过程的文本文件。 使用Docker Swarm时,可以使用Dockerfile创建镜像,但其实创建镜像的过程与在单个主机上创建镜像的过程非常相似。主要的区别是,在Swarm中创建镜像将会在整个集群中被使用,而非仅限于单个主机。 首先,在Swarm集群的主节点上创建一个Dockerfile,该文件包含了构建镜像的指令和配置。以一个简单的Node.js应用为例,可以定义如下的Dockerfile: ``` FROM node:14-alpine WORKDIR /app COPY package*.json ./ RUN npm install COPY . . EXPOSE 3000 CMD [ "npm", "start" ] ``` 然后,在主节点的终端中,使用`docker build`命令来构建镜像。例如,使用以下命令创建一个名为`my-app`的镜像: ``` docker build -t my-app . ``` 接下来,使用`docker push`命令将构建好的镜像推送到Docker镜像仓库。例如,可以推送到Docker Hub: ``` docker push username/my-app ``` 最后,在Swarm集群的其他节点上使用`docker service`命令来创建服务,并使用先前构建镜像作为服务的副本。例如,使用以下命令创建一个名为`my-app`的服务: ``` docker service create --name my-app --replicas 3 username/my-app ``` 这将在集群的多个节点上运行三个容器的副本,以提供高可用性和负载均衡的服务。 通过以上步骤,我们可以使用DockerfileDocker Swarm集群中创建镜像,并在集群中运行多个容器的副本来提供服务。这种方式使得应用程序可以更加灵活地在集群中运行,并可以根据需要进行扩展和管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值