制做docker镜像-创新互联
1、制做JAVA运行环境镜像
发展壮大离不开广大客户长期以来的信赖与支持,我们将始终秉承“诚信为本、服务至上”的服务理念,坚持“二合一”的优良服务模式,真诚服务每家企业,认真做好每个细节,不断完善自我,成就企业,实现共赢。行业涉及宣传片制作等,在成都网站建设、全网营销推广、WAP手机网站、VI设计、软件开发等项目上具有丰富的设计经验。我的基础镜像是基于官网centos 7.2版本制作,没有可以提前下载好
#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包可以再做精简2.制作生产环境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 .
从镜像启动一个tomcat容器,把测试程序部署到/www/app1/webapps下从浏览器访问
[root@R210 ]# docker run -d -p 8002:8080 --name mytomcat7 -v /www/app1/webapps/:/tomcat/webapps tomcat7:7.0.70
3.制作数据库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 ######################################################### # 循环检测数据库进程是否正常启动、服务是否可用 # ######################################################### function wait_for_db() { set +e echo "等待数据库启动..." while true; do if grep "ready for connections" $ERROR_LOG; then break; else echo "继续等待数据库启动完成..." && sleep 1 fi done set -e } ######################################################### # 关闭数据库进程 ######################################################### function terminate_db() { local pid=$(cat $VOLUME_HOME/mysql.pid) echo "捕获进程信号&关闭数据库进程..." 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 }######################################################### #如果数据目录为空,调用 mysql_install_db 脚本生成默认库 # 全局变量: # $VOLUME_HOME # $ERROR_LOG ######################################################### function install_db() { if [ ! -d $VOLUME_HOME/mysql ]; then echo "=> 数据目录 $VOLUME_HOME为空,数据库未初始化..." echo "=> 正在安装 MariaDB..." mysql_install_db --user=mysql > /dev/null 2>&1 echo "=> MariaDB 安装完成!" else echo "=> 使用已经存在数据目录文件." fi ######################################################### # 移除之前容器运行留下的日志文件,保留当前容器生成的日志 ######################################################### 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 }######################################################### # 创建用户 # 变量: # $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[root@XSMM-WEB mk_mariadb]# cat run.sh
#!/bin/bash set -e set -u source ./mariadb-func.sh # 用户指定数据库用户名,不提供默认为mysql,并随机生成密码 MARIADB_USER=${MARIADB_USER:="mysql"} MARIADB_PASS=${MARIADB_PASS:-$(pwgen -s 12 1)} # 指定其它必要环境变量 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 #调用install_db函数 install_db # 输出所有信息到终端 tail -F $ERROR_LOG & #将数据库启动到后台运行并记录PID /usr/bin/mysqld_safe & MYSQLD_SAFE_PID=$! wait_for_db secure_and_tidy_db show_db_status create_mysql_user #不在终止这个脚本直到数据库进程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#创建镜像
[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#从镜像生成一个mariadb容器
[root@XSMM]# docker run --name mydb -d -p 3306:3306 -v /mydata/mysql/:/var/lib/mysql mariadb10
7eb4b284406bfb497ee0addf8cda153ab91013f3f9fc3357d856c68ea8a6e658
#查看容器日志
[root@XSMM]# docker logs mydb
=> 使用已经存在数据目录文件. 等待数据库启动... 继续等待数据库启动完成... 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库和无密码用户... 设置root用户只能从本地登陆完成! Showing DB status... Creating DB admin user... => Creating MariaDB user 'mysql' with 'tMEpSbAy0lIK' password. ======================================================================== 可以使用以下命令连接 MariaDB Server: mysql -umysql -ptMEpSbAy0lIK -h[root@XSMM]# mysql -umysql -ptMEpSbAy0lIK
ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2)#连接数据测试
[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)]>另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享名称:制做docker镜像-创新互联
网页地址:http://azwzsj.com/article/ejopi.html