转载

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

前言

​上篇文章讲述了有关Dockerfile的理论,以及演示了通过其构建的三个镜像服务httpd、sshd以及systemd。本文将结合先前的shell编程与当下的Dockerfile来给出基于Dockerfile构建Nginx、Tomcat以及MySQL模板。 基于Dockerfile构建Nginx、Tomcat、MySQL镜像(含包源)

  • 揭开Docker的面纱 - 基础理论梳理和安装流程演示  https://www.linuxidc.com/Linux/2020-04/163003.htm
  • Docker基础命令详解 - 镜像及容器操作  https://www.linuxidc.com/Linux/2020-04/163005.htm
  • 深入理解Docker的硬件资源控制与验证  https://www.linuxidc.com/Linux/2020-04/163006.htm
  • Docker网络模式与配置Docker自定义网络(bridge模式)  https://www.linuxidc.com/Linux/2020-04/163007.htm
  • Docker构建镜像的三种方式(Dockerfile初步)  https://www.linuxidc.com/Linux/2020-04/163008.htm
  • 教你如何搭建Docker私有仓库  https://www.linuxidc.com/Linux/2020-04/163009.htm
  • 深入理解Docker数据管理与端口映射  https://www.linuxidc.com/Linux/2020-04/163010.htm
  • Dockerfile与Dockerfile实战  https://www.linuxidc.com/Linux/2020-04/163011.htm

​ 由于时间和篇幅问题,本文将省略build的过程。其中Nginx的构建将基于wget命令从网上下载,后两者将使用本地已有的软件包。

​ 这样的目的一方面是演示多种方式,以及对两种方式的区别,另一方面则是效率问题,从网上获取一般收到影响较大(这个体会将在您build的时候感受出来)。

​ 好了,下面直接给出对应的Dockerfile和相关文件吧。

​先给出软件包链接:

链接: https://pan.baidu.com/s/1teUc4kqzLH1kZS7Vu3znaQ

关注Linux公社微信公众号( linuxidc_com ),(见 https://www.linuxidc.com/Linux/2013-12/93755.htm ),在Linux公社微信公众号后台发送发送“ 163012 ”即可获得。

基于Dockerfile的Nginx镜像构建

目录结构

[root@localhost nginx]# tree ./
./
├── Dockerfile
└── nginx.sh

0 directories, 2 files

Dockerfile以及其他文件

#基于dockerfile构建nginx镜像
#设置基础镜像
FROM CentOS:7
#维护该镜像的用户信息
MAINTAINER lokott@123.com
#指令集
#更新及安装相关工具
RUN yum update -y
RUN yum install -y wget lsof telnet net-tools gcc gcc-c++ make pcre pcre-devel zlib zlib-devel
#从官网上下载nginx软件包源并解压
RUN wget http://nginx.org/download/nginx-1.16.1.tar.gz
RUN tar zxf nginx-1.16.1.tar.gz
#创建nginx用户
RUN useradd -M -s /sbin/nologin nginx
#指定后续RUN指令的工作目录
WORKDIR nginx-1.16.1
#配置参数以及编译nginx
RUN ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module
RUN make && make install
#ENV PATH /usr/local/nginx/sbin:$PATH
#端口设置
EXPOSE 80
EXPOSE 443
#以非daemon方式运行
RUN echo "daemon off;" >> /usr/local/nginx/conf/nginx.conf
#切换工作目录
WORKDIR /root/nginx
ADD nginx.sh /nginx.sh
RUN chmod 755 /nginx.sh
#启动容器执行指令
CMD ["/nginx.sh"]

shell脚本文件

[root@localhost nginx]# cat nginx.sh 
#!/bin/bash
/usr/local/nginx/sbin/nginx

对比手工编译的shell脚本是不是非常类似呢?其中需要注意的有两点:

1、从网上下载需要安装wget工具进行软件包下载并且需要执行tar命令解压,而本地有软件包则会自动解压(看下面两个服务构建镜像过程就可以理解了)

2、nginx通过服务是需要关闭后台运行,否则一直会无法正常运行容器

构建及运行

[root@localhost nginx]# docker build -t nginx:centos .
[root@localhost nginx]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
[root@localhost nginx]# docker run -d -P nginx:centos 
3d4c431bf95feded1928268a4237768ca7ed2b362ef3cf582cab7e9d49cc4669
[root@localhost nginx]# docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS                                           NAMES
3d4c431bf95f        nginx:centos        "/nginx.sh"         4 seconds ago       Up 3 seconds        0.0.0.0:32772->80/tcp, 0.0.0.0:32771->443/tcp   distracted_mclean

测试:浏览器访问ip:32772 此时不能访问32771,因为涉及到ssl证书及其他服务

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

基于Dockerfile的Tomcat镜像构建

目录结构

[root@localhost tomcat]# tree .
.
├── apache-tomcat-9.0.16.tar.gz
├── Dockerfile
└── jdk-8u91-linux-x64.tar.gz

0 directories, 3 files

Dockerfile文件

FROM centos:7
MAINTAINER this is tomcat
ADD jdk-8u91-linux-x64.tar.gz /usr/local
WORKDIR /usr/local
RUN mv jdk1.8.0_91 /usr/local/<a href="../../Java" target="_blank" title="https://www.linuxidc.com/topicnews.aspx?tid=19">Java</a>
ENV JAVA_HOME /usr/local/java
ENV JAVA_BIN /usr/local/java/bin
ENV JRE_HOME /usr/local/java/jre
ENV PATH $PATH:/usr/local/java/bin:/usr/local/java/jre/bin
ENV CLASSPATH /usr/local/java/jre/bin:/usr/local/java/lib:/usr/local/java/jre/lib/charsets.jar
ADD apache-tomcat-9.0.16.tar.gz /usr/local
WORKDIR /usr/local
RUN mv apache-tomcat-9.0.16 /usr/local/tomcat8
EXPOSE 8080
ENTRYPOINT ["/usr/local/tomcat8/bin/catalina.sh","run"]

构建及运行

[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

宿主机系统安装mariadb服务来测试

[root@localhost mysql5.7]# yum install mariadb -y
[root@localhost mysql5.7]# mysql -h 20.0.0.149 -P 32774 -uroot -p123456
Welcome to the MariaDB monitor.  Commands end with ; or /g.
Your MySQL connection id is 4
Server version: 5.7.20 Source distribution

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

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

MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.01 sec)
#创建一个数据库,退出后再次然后进入容器查看
MySQL [(none)]> create database mydb;
Query OK, 1 row affected (0.00 sec)

MySQL [(none)]> exit
Bye

[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 5
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> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.00 sec)

mysql> exit
Bye
[root@e9c9f93766d1 mysql]# exit
exit
[root@localhost mysql5.7]#

小结

基于Dockerfile构建这三个服务的镜像案例就说到这里,当然在工程中一般不会将MySQL服务放在容器中运行,而是会单独使用服务器部署提供服务(搭建高可用集群架构),本文主要是提供基于Dockerfile构建编写这些服务的案例。

需要注意两点:在Dockerfile中什么时候使用tar命令;权限问题

谢谢阅读!

Linux公社的RSS地址 : https://www.linuxidc.com/rssFeed.aspx

本文永久更新链接地址: https://www.linuxidc.com/Linux/2020-04/163012.htm

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

原文  https://www.linuxidc.com/Linux/2020-04/163012.htm
正文到此结束
Loading...