首頁 > 軟體

基於Dockerfile構建Nginx、Tomcat、MySQL映象(含包源)

2020-06-16 16:12:15

? 由於時間和篇幅問題,本文將省略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的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/Java
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的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命令;許可權問題

謝謝閱讀!


IT145.com E-mail:sddin#qq.com