基于commit命令方式创建
docker的安装
[root@test01 ~]# yum install docker
[root@test01 ~]# systemctl enable docker
[root@test01 ~]# systemctl start docker
更新加速器
sudo mkdir -p /etc/docker sudo tee /etc/docker/daemon.json <<-'EOF' { "registry-mirrors": ["https://z0ubh7pu.mirror.aliyuncs.com"] } EOF sudo systemctl daemon-reload sudo systemctl restart docker
下载本地镜像
[root@test01 ~]# docker pull centos:7.4.1708
[root@test01 ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
docker.io/centos 7.4.1708 3afd47092a0e 3 months ago 196.6 MB
创建交互型容器
[root@test01 ~]# docker run -it --name="mysql_server" centos /bin/bash
安装mariadb服务
[root@e8126d0481d2 /]# yum -y install mariadb-server net-tools
初始化mariadb
[root@e8126d0481d2 /]# mysql_install_db --user=mysql
后台启动mariadb服务
[root@e8126d0481d2 /]# mysqld_safe &
[1] 114
[root@e8126d0481d2 /]#
180210 13:45:27 mysqld_safe Logging to '/var/log/mariadb/mariadb.log'.
180210 13:45:27 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql
[root@e8126d0481d2 /]# netstat -tunpl
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:3306 0.0.0.0:* LISTEN -
创建mariadb登录密码,并可以指定ip登录
[root@e8126d0481d2 /]# mysqladmin -u root password '123456'
[root@e8126d0481d2 /]# mysql -u root -p
Enter password:
MariaDB [(none)]> show databases;
MariaDB [(none)]> use mysql;
MariaDB [mysql]> select Host from user where user='root';
MariaDB [mysql]> grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
MariaDB [mysql]> update user set password=password('123456') where user='root' and host='e8126d0481d2';
MariaDB [mysql]> flush privileges;
MariaDB [mysql]> exit
容器登录验证
[root@e8126d0481d2 /]# mysql -u root -h 172.17.0.2 -p
Enter password:
MariaDB [(none)]> exit
创建容器启动脚本
[root@e8126d0481d2 ~]# cat run.sh
#!/bin/sh
mysqld_safe
创建镜像
[root@test01 ~]# docker ps -a
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
e8126d0481d2 centos "/bin/bash" 11 minutes ago Exited (0) 8 seconds ago mysql_server
[root@test01 ~]# docker commit mysql_server mariadb:1.0
创建容器
[root@test01 ~]# docker run -d -p 13306:3306 mariadb:1.0 /root/run.sh
[root@test01 ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
eed3e88a1261 mariadb:1.0 "mysqld_safe" 4 seconds ago Up 3 seconds 0.0.0.0:13306->3306/tcp romantic_hamilton
主机登录验证
[root@test01 ~]# yum -y install mariadb
[root@test01 ~]# mysql -u root --port=13306 -p
MariaDB [(none)]>