基于Dockerfile构建Nginx、Tomcat、MySQL镜像(含包源)(2)

构建及运行

[root@localhost tomcat]# docker build -t tomcat:centos . [root@localhost tomcat]# docker run -d -P tomcat:centos c8a2e5524af1bf74dd1677d85f45db8e7d4715f97acac1298227cf0fc1092f46 [root@localhost tomcat]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES c8a2e5524af1 tomcat:centos "/usr/local/tomcat8/…" 5 seconds ago Up 4 seconds 0.0.0.0:32773->8080/tcp xenodochial_yalow

测试

基于Dockerfile构建Nginx、Tomcat、MySQL镜像(含包源)

基于Dockerfile的MySQL镜像构建

目录结构

[root@localhost mysql5.7]# tree . . ├── Dockerfile ├── my.cnf └── mysql-boost-5.7.20.tar.gz 0 directories, 3 files

Dockerfile及其他文件

[root@localhost mysql5.7]# cat Dockerfile

#基于基础镜像 FROM centos:7 #维护该镜像的用户信息 MAINTAINER lokott@123.com #指令集 #下载相关工具 RUN yum -y install \ ncurses \ ncurses-devel \ bison \ cmake \ make \ gcc \ gcc-c++ #创建mysql用户 RUN useradd -s /sbin/nologin mysql #复制软件包到指定目录(将会自动解压) ADD mysql-boost-5.7.20.tar.gz /usr/local/src #指定工作目录 WORKDIR /usr/local/src/mysql-5.7.20/ #cmake配置及编译安装 RUN cmake \ -DCMAKE_INSTALL_PREFIX=/usr/local/mysql \ -DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \ -DSYSCONFDIR=/etc \ -DSYSTEMD_PID_DIR=/usr/local/mysql \ -DDEFAULT_CHARSET=utf8 \ -DDEFAULT_COLLATION=utf8_general_ci \ -DWITH_INNOBASE_STORAGE_ENGINE=1 \ -DWITH_ARCHIVE_STORAGE_ENGINE=1 \ -DWITH_BLACKHOLE_STORAGE_ENGINE=1 \ -DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \ -DMYSQL_DATADIR=/usr/local/mysql/data \ -DWITH_BOOST=boost \ -DWITH_SYSTEMD=1 && make && make install #更改mysql目录属主属组 RUN chown -R mysql:mysql /usr/local/mysql/ #删除默认安装的my.cnf文件 RUN rm -rf /etc/my.cnf #复制一份my.cnf到etc目录下 ADD my.cnf /etc #更改该文件权限 RUN chown mysql:mysql /etc/my.cnf #设置环境变量,命令目录及库文件目录 ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH #指定工作目录 WORKDIR /usr/local/mysql/ #初始化设置 RUN bin/mysqld \ --initialize-insecure \ --user=mysql \ --basedir=/usr/local/mysql \ --datadir=/usr/local/mysql/data #优化启动方式 RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/ EXPOSE 3306 #直接设置运行启动脚本 RUN echo -e "#!/bin/sh \nsystemctl enable mysqld" > /run.sh RUN chmod 755 /run.sh RUN sh /run.sh #启动容器时执行 CMD ["init"]

my.cnf文件

[client] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysql] port = 3306 default-character-set=utf8 socket = /usr/local/mysql/mysql.sock [mysqld] user = mysql basedir = /usr/local/mysql datadir = /usr/local/mysql/data port = 3306 character_set_server=utf8 pid-file = /usr/local/mysql/mysqld.pid socket = /usr/local/mysql/mysql.sock server-id = 1 sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES

构建及运行

[root@localhost mysql5.7]# docker build -t mysql:latest . ...//友情提示MySQL5.7时间比较长 [root@localhost mysql5.7]# docker run --name mysql_new -d -P --privileged mysql:latest e9c9f93766d149a3387aed4cb5e04425269a884fccf06256b087d00e4c262222 [root@localhost mysql5.7]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e9c9f93766d1 mysql:latest "init" 6 seconds ago Up 5 seconds 0.0.0.0:32774->3306/tcp

进入MySQL服务的容器中进行提权操作

[root@localhost mysql5.7]# docker exec -it mysql_new /bin/bash [root@e9c9f93766d1 mysql]# mysql Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.7.20 Source distribution Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> grant all privileges on *.* to 'root'@'%' identified by '123456'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql> flush privileges; Query OK, 0 rows affected (0.01 sec) mysql> exit Bye [root@e9c9f93766d1 mysql]# exit exit

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/cb21bace7d29368909aced018dc61c44.html