您好,登錄后才能下訂單哦!
1、制做JAVA運(yùn)行環(huán)境鏡像
我的基礎(chǔ)鏡像是基于官網(wǎng)centos 7.2版本制作,沒(méi)有可以提前下載好
#docker pull docker.io/centos
[root@R210 ~]# mkdir -p /opt/Dockerfile/JRE1.7
[root@R210 ~]# cd /opt/Dockerfile/JRE1.7
[root@R210 jre1.7]# ls
Dockerfile server-jre-7u80-linux-x64.tar.gz
[root@R210 jre1.7]# cat Dockerfile
FROM centos:latest
MAINTAINER yy ENV JAVA_VERSION "7u80" ENV JDK_VERSION "1.7.0_80" RUN rpm --rebuilddb \ && yum --setopt=tsflags=nodocs -y install \ net-tools \ && rm -rf /var/cache/yum/* && yum clean all #RUN rpm --import http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-7 \ # && yum update && yum install net-tools -y \ RUN mkdir -p /usr/lib/jvm COPY server-jre-${JAVA_VERSION}-linux-x64.tar.gz /usr/lib/jvm/server-jre-${JAVA_VERSION}-linux-x64.tar.gz RUN cd /usr/lib/jvm \ && tar -zxvf server-jre-${JAVA_VERSION}-linux-x64.tar.gz \ && rm -rf /usr/lib/jvm/server-jre-${JAVA_VERSION}-linux-x64.tar.gz \ && ln -s /usr/lib/jvm/jdk${JDK_VERSION}/bin/java /bin/ \ && cd /usr/lib/jvm/jdk${JDK_VERSION} \ && find . -type f -name '*.html' -delete \ && find . -type f -name 'COPYRIGHT' -delete \ && find . -type f -name 'LICENSE' -delete \ && find . -type f -name 'NOTICE' -delete \ && find . -type f -name '3RDPARTY' -delete \ && find . -type f -name '*.txt' -delete \ && find . -type f -name '*.bat' -delete \ #jre包可以再做精簡(jiǎn)
2.制作生產(chǎn)環(huán)境Tomcat鏡像
[root@R210 ~]# mkdir -p /opt/Dockerfile/tomcat7
[root@R210 ~]# cd /opt/Dockerfile/tomcat7
[root@R210 tomcat7]# ls
apache-tomcat-7.0.70.tar.gz Dockerfile
[root@R210 tomcat7]# cat Dockerfile
FROM java7:7u80 MAINTAINER yy ENV TOMCAT_VERSION 7.0.70 ENV CATALINA_HOME /tomcat ENV APP_DIR ${CATALINA_HOME}/webapps/ RUN rpm --rebuilddb \ && yum --setopt=tsflags=nodocs -y install \ apr tomcat-native \ && rm -rf /var/cache/yum/* && yum clean all #RUN wget -q https://archive.apache.org/dist/tomcat/tomcat-${TOMCAT_MAJOR_VERSION}/v${TOMCAT_MINOR_VERSION}/bin/apache-tomcat-${TOMCAT_MINOR_VERSION}.tar.gz #https://archive.apache.org/dist/tomcat/tomcat-7/v7.0.70/bin/apache-tomcat-7.0.70.tar.gz COPY apache-tomcat-${TOMCAT_VERSION}.tar.gz apache-tomcat-{TOMCAT_VERSION}.tar.gz RUN tar zxf apache-tomcat-*.tar.gz && \ rm apache-tomcat-*.tar.gz && \ mv apache-tomcat* tomcat && \ rm -rf tomcat/webapps/* && \ sed -i '/protocol="AJP\/1.3"/d' /tomcat/conf/server.xml EXPOSE 8080 ENTRYPOINT ["/tomcat/bin/catalina.sh","run"] ONBUILD ADD . ${APP_DIR}
制作鏡像
[root@R210 tomcat7]#docker build -t tomcat7:7.0.70 .
從鏡像啟動(dòng)一個(gè)tomcat容器,把測(cè)試程序部署到/www/app1/webapps下從瀏覽器訪問(wèn)
[root@R210 ]# docker run -d -p 8002:8080 --name mytomcat7 -v /www/app1/webapps/:/tomcat/webapps tomcat7:7.0.70
3.制作數(shù)據(jù)庫(kù)mariadb鏡像
[root@XSMM mk_mariadb]# ls
Dockerfile mariadb-func.sh mariadb.repo run.sh
[root@XSMM-WEB mk_mariadb]# cat Dockerfile
FROM centos:latest MAINTAINER yy ENV TZ "Asia/Shanghai" ENV TERM xterm COPY mariadb.repo /etc/yum.repos.d/ RUN \ yum update -y && \ yum install -y epel-release && \ yum install -y MariaDB-server hostname net-tools pwgen && \ yum clean all && \ rm -rf /var/lib/mysql/* && \ rm -rf /var/cache/yum/* COPY mariadb-func.sh / COPY run.sh / RUN chmod 755 *.sh VOLUME ["/var/lib/mysql"] EXPOSE 3306 CMD ["/run.sh"]
[root@XSMM-WEB mk_mariadb]# cat mariadb-func.sh
#!/bin/sh ######################################################### # 循環(huán)檢測(cè)數(shù)據(jù)庫(kù)進(jìn)程是否正常啟動(dòng)、服務(wù)是否可用 # ######################################################### function wait_for_db() { set +e echo "等待數(shù)據(jù)庫(kù)啟動(dòng)..." while true; do if grep "ready for connections" $ERROR_LOG; then break; else echo "繼續(xù)等待數(shù)據(jù)庫(kù)啟動(dòng)完成..." && sleep 1 fi done set -e } ######################################################### # 關(guān)閉數(shù)據(jù)庫(kù)進(jìn)程 ######################################################### function terminate_db() { local pid=$(cat $VOLUME_HOME/mysql.pid) echo "捕獲進(jìn)程信號(hào)&關(guān)閉數(shù)據(jù)庫(kù)進(jìn)程..." kill -TERM $pid while true; do if tail $ERROR_LOG | grep -s -E "mysqld .+? ended" $ERROR_LOG; then break; else sleep 0.5; fi done }
######################################################### #如果數(shù)據(jù)目錄為空,調(diào)用 mysql_install_db 腳本生成默認(rèn)庫(kù) # 全局變量: # $VOLUME_HOME # $ERROR_LOG ######################################################### function install_db() { if [ ! -d $VOLUME_HOME/mysql ]; then echo "=> 數(shù)據(jù)目錄 $VOLUME_HOME為空,數(shù)據(jù)庫(kù)未初始化..." echo "=> 正在安裝 MariaDB..." mysql_install_db --user=mysql > /dev/null 2>&1 echo "=> MariaDB 安裝完成!" else echo "=> 使用已經(jīng)存在數(shù)據(jù)目錄文件." fi ######################################################### # 移除之前容器運(yùn)行留下的日志文件,保留當(dāng)前容器生成的日志 ######################################################### if [ -f $ERROR_LOG ]; then echo "----------------- Previous error log -----------------" tail -n 20 $ERROR_LOG echo "----------------- Previous error log ends -----------------" && echo mv -f $ERROR_LOG "${ERROR_LOG}.old"; fi touch $ERROR_LOG && chown mysql $ERROR_LOG }
######################################################### # 創(chuàng)建用戶(hù) # 變量: # $MARIADB_USER # $MARIADB_PASS ######################################################### function create_mysql_user() { echo "Creating DB admin user..." && echo local users=$(mysql -s -e "SELECT count(User) FROM mysql.user WHERE User='$MARIADB_USER'") if [[ $users == 0 ]]; then echo "=> Creating MariaDB user '$MARIADB_USER' with '$MARIADB_PASS' password." mysql -uroot -e "CREATE USER '$MARIADB_USER'@'%' IDENTIFIED BY '$MARIADB_PASS'" else echo "=> User '$MARIADB_USER' exists, updating its password to '$MARIADB_PASS'" mysql -uroot -e "SET PASSWORD FOR '$MARIADB_USER'@'%' = PASSWORD('$MARIADB_PASS')" fi; mysql -uroot -e "GRANT ALL PRIVILEGES ON *.* TO '$MARIADB_USER'@'%' WITH GRANT OPTION" echo "========================================================================" echo " 可以使用以下命令連接 MariaDB Server: " echo " mysql -u$MARIADB_USER -p$MARIADB_PASS -h<host> " echo " " echo " 出于安全考慮,應(yīng)該修改上述密碼,root可以在本地?zé)o密碼登陸建議設(shè)置密碼 " echo "========================================================================" }
######################################################### # 顯示數(shù)據(jù)庫(kù)狀態(tài) ######################################################### function show_db_status() { echo "Showing DB status..." && echo mysql -uroot -e "status" } ######################################################### # 清理數(shù)據(jù)庫(kù),刪除test庫(kù)和無(wú)密碼用戶(hù) ######################################################### function secure_and_tidy_db() { echo "刪除test庫(kù)和無(wú)密碼用戶(hù)..." mysql -uroot -e "DROP DATABASE IF EXISTS test" mysql -uroot -e "DELETE FROM mysql.user where User = ''" ######################################################### # 設(shè)置root用戶(hù)只能從本地登陸 ######################################################### mysql -uroot -e "DELETE FROM mysql.user where User = 'root' AND Host NOT IN ('127.0.0.1','::1')" mysql -uroot -e "DELETE FROM mysql.proxies_priv where User = 'root' AND Host NOT IN ('127.0.0.1','::1')" echo "設(shè)置root用戶(hù)只能從本地登陸完成!" }
[root@XSMM-WEB mk_mariadb]# cat run.sh
#!/bin/bash set -e set -u source ./mariadb-func.sh # 用戶(hù)指定數(shù)據(jù)庫(kù)用戶(hù)名,不提供默認(rèn)為mysql,并隨機(jī)生成密碼 MARIADB_USER=${MARIADB_USER:="mysql"} MARIADB_PASS=${MARIADB_PASS:-$(pwgen -s 12 1)} # 指定其它必要環(huán)境變量 VOLUME_HOME="/var/lib/mysql" ERROR_LOG="$VOLUME_HOME/$HOSTNAME.err" MYSQLD_PID_FILE="$VOLUME_HOME/$HOSTNAME.pid" # Trap INT and TERM signals to do clean DB shutdown trap terminate_db SIGINT SIGTERM #調(diào)用install_db函數(shù) install_db # 輸出所有信息到終端 tail -F $ERROR_LOG & #將數(shù)據(jù)庫(kù)啟動(dòng)到后臺(tái)運(yùn)行并記錄PID /usr/bin/mysqld_safe & MYSQLD_SAFE_PID=$! wait_for_db secure_and_tidy_db show_db_status create_mysql_user #不在終止這個(gè)腳本直到數(shù)據(jù)庫(kù)進(jìn)程mysqld_safe正常退出 wait $MYSQLD_SAFE_PID
[root@XSMM-WEB mk_mariadb]# cat mariadb.repo
# MariaDB 10.1 CentOS repository list - created 2016-01-06 02:19 UTC # http://mariadb.org/mariadb/repositories/ [mariadb] name = MariaDB baseurl = http://yum.mariadb.org/10.1/centos7-amd64 gpgkey=https://yum.mariadb.org/RPM-GPG-KEY-MariaDB gpgcheck=1
#創(chuàng)建鏡像
[root@XSMM mk_mariadb]# docker build -t mariadb10 .
#docker run --name mydb -d -p 3306:3306 -v /mydata/mysql/:/var/lib/mysql mariadb10
... ---> Running in 35b0398fbeb8 ---> d353c6e1e33b Removing intermediate container 35b0398fbeb8 Step 12 : CMD /run.sh ---> Running in 7248c5e14559 ---> 538b64e6987d Removing intermediate container 7248c5e14559 Successfully built 538b64e6987d
[root@XSMM]# docker p_w_picpaths
REPOSITORY TAG IMAGE ID CREATED SIZE mariadb10 latest 538b64e6987d 4 minutes ago 1.111 GB centos7-ssh latest 104c3f5a710c 7 days ago 504.1 MB centos6 latest ba7961b610e2 7 days ago 194.6 MB docker.io/tomcat latest 3c90608ca58a 8 days ago 357.4 MB docker.io/centos/redis latest a4b79297fc55 2 weeks ago 405.6 MB docker.io/centos latest 970633036444 5 weeks ago 196.7 MB
#從鏡像生成一個(gè)mariadb容器
[root@XSMM]# docker run --name mydb -d -p 3306:3306 -v /mydata/mysql/:/var/lib/mysql mariadb10
7eb4b284406bfb497ee0addf8cda153ab91013f3f9fc3357d856c68ea8a6e658
#查看容器日志
[root@XSMM]# docker logs mydb
=> 使用已經(jīng)存在數(shù)據(jù)目錄文件. 等待數(shù)據(jù)庫(kù)啟動(dòng)... 繼續(xù)等待數(shù)據(jù)庫(kù)啟動(dòng)完成... 160910 01:07:05 mysqld_safe Logging to '/var/lib/mysql/7eb4b284406b.err'. 160910 01:07:05 mysqld_safe Starting mysqld daemon with databases from /var/lib/mysql ... 2016-09-10 1:07:06 139756268353664 [Note] /usr/sbin/mysqld: ready for connections. Version: '10.1.17-MariaDB' socket: '/var/lib/mysql/mysql.sock' port: 3306 MariaDB Server 2016-09-10 1:07:06 139756268353664 [Note] /usr/sbin/mysqld: ready for connections. 刪除test庫(kù)和無(wú)密碼用戶(hù)... 設(shè)置root用戶(hù)只能從本地登陸完成! Showing DB status... Creating DB admin user... => Creating MariaDB user 'mysql' with 'tMEpSbAy0lIK' password. ======================================================================== 可以使用以下命令連接 MariaDB Server: mysql -umysql -ptMEpSbAy0lIK -h<host> 出于安全考慮,應(yīng)該修改上述密碼,root可以在本地?zé)o密碼登陸建議設(shè)置密碼 ========================================================================
[root@XSMM]# mysql -umysql -ptMEpSbAy0lIK
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)
#連接數(shù)據(jù)測(cè)試
[root@XSMM]# mysql --protocol=tcp -umysql -ptMEpSbAy0lIK
Welcome to the MariaDB monitor. Commands end with ; or \g. Your MariaDB connection id is 10 Server version: 10.1.17-MariaDB MariaDB Server Copyright (c) 2000, 2016, Oracle, MariaDB Corporation Ab and others. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. MariaDB [(none)]> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | performance_schema | +--------------------+ 3 rows in set (0.01 sec) MariaDB [(none)]>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。