一、tomcat(更换jdk)
mkdir tomcat
cd tomcat/
tar xf jdk-8u191-linux-x64.tar.gz
tar xf apache-tomcat-8.5.40.tar.gz
vim Dockerfile
FROM centos:7
MAINTAINER Crushlinux
ADD jdk1.8.0_191 /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-8.5.40 /usr/local/tomcat
RUN chmod 755 /usr/local/tomcat/bin/startup.sh
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 8080
CMD ["/usr/local/tomcat/bin/catalina.sh", "run"]
docker build -t tomcat:new .
docker images tomcat:new
docker run -d -p 8080:8080 --name tomcat-test --restart=always tomcat:new
二、mysql
mkdir mysql
cd mysql/
vim Dockerfile
FROM centos:7
MAINTAINER Crushlinux
#安装mariadb数据库
RUN yum install -y mariadb mariadb-server mariadb-devel
#设置环境变量,便于管理
ENV MARIADB_USER root
ENV MARIADB_PASS 123456
#让容器支持中文
ENV LC_ALL en_US.UTF-8
#初始化数据库
ADD db_init.sh /root/db_init.sh
RUN chmod 775 /root/db_init.sh && /root/db_init.sh
#导出端口
EXPOSE 3306
#设置默认启动命令
CMD ["mysqld_safe"]
vim db_init.sh 主要用于授权
#!/bin/bash
mysql_install_db --user=mysql
sleep 3
mysqld_safe &
sleep 3
mysql -e "use mysql; grant all privileges on *.* to '$MARIADB_USER'@'%' identified by '$MARIADB_PASS' with grant option;"
h=$(hostname)
mysql -e "use mysql; update user set password=password('$MARIADB_PASS') where user='$MARIADB_USER' and host='$h';"
mysql -e "flush privileges;"
docker build -t mysql:new .
docker run -d -p 3306:3306 --name mysql-test --restart=always mysql:new
yum -y install mariadb mariadb-devel
mysql -h 192.168.77.123 -u root -P 3306 -p123456
三、lnmp架构
mkdir lnmp
cd lnmp/
vim Dockerfile
#基础镜像
FROM centos:7
#维护该镜像的用户信息
MAINTAINER Crushlinux
#配置Nginx 的YUM 源
RUN rpm -ivh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm
#安装nginx
RUN yum -y install nginx
#安装mysql 和php
RUN rpm --rebuilddb && yum -y install mariadb-devel mariadb-server mariadb php php-fpm
#修改nginx 配置文件,使之支持PHP
ADD default.conf /etc/nginx/conf.d/default.conf
#修改php-fpm 配置文件允许nginx 访问
RUN sed -i '/^user/s/apache/nginx/g' /etc/php-fpm.d/www.conf
RUN sed -i '/^group/s/apache/nginx/g' /etc/php-fpm.d/www.conf
#设置环境变量,便于管理
ENV MARIADB_USER root
ENV MARIADB_PASS 123456
#mysql 数据库授权
ADD db_init.sh /root/db_init.sh
RUN chmod 775 /root/db_init.sh
RUN /root/db_init.sh
#添加测试页面
ADD index.php /usr/share/nginx/html/index.php
#定义时区
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
#分别开启80 端口,443 端口,9000 端口,3360 端口
EXPOSE 80
EXPOSE 9000
EXPOSE 3306
#复制脚本,设置权限,启动容器时启动该脚本
ADD run.sh /run.sh
RUN chmod 775 /run.sh
CMD ["/run.sh"]
vim db_init.sh 主要用于授权
#!/bin/bash
/usr/bin/mysql_install_db --user=mysql
sleep 3
/usr/bin/mysqld_safe &
sleep 3
mysql -e "use mysql; grant all privileges on *.* to '$MARIADB_USER'@'%' identified by '$MARIADB_PASS' with grant option;"
h=$(hostname)
mysql -e "use mysql; update user set password=password('$MARIADB_PASS') where user='$MARIADB_USER' and host='$h';"
mysql -e "flush privileges;"
vim index.php
vim default.conf 修改nginx配置文件
server {
listen 80;
server_name localhost;
location / {
root /usr/share/nginx/html;
index index.html index.php index.htm;
}
location ~ \.php$ {
root /usr/share/nginx/html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/share/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
}
vim run.sh 启动nginx脚本
#!/bin/bash
/usr/sbin/nginx && /usr/sbin/php-fpm && /usr/bin/mysqld_safe
docker build -t lnmp:new .
docker images lnmp
docker run -d -p 80:80 -p 3306:3306 -p 9000:9000 --name lnmp-test --restart=always lnmp:new
四、redis
mkdir redis
cd redis/
vim Dockerfile
FROM centos:7
MAINTAINER Crushlinux
RUN yum -y update && yum -y install epel-release && yum -y install redis
RUN sed -i -e 's@bind 127.0.0.1@bind 0.0.0.0@g' /etc/redis.conf
RUN sed -i -e 's@protected-mode yes@protected-mode no@g' /etc/redis.conf
RUN echo "requirepass 123456" >> /etc/redis.conf
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
EXPOSE 6379
CMD [ "/usr/bin/redis-server","/etc/redis.conf"]
docker build -t redis:new .
docker images redis:new
docker run -d -p 6379:6379 --name redis-test --restart=always redis:new
rpm -ivh /root/epel-release-latest-7.noarch.rpm
yum -y install redis
redis-cli -h localhost -a 123456
以上就是dockerfile构建tomcat、mysql、lnmp、redis镜像的操作
如有错误欢迎各位大佬批评指正,我们共同进步