1、使用dockerfile制作nginx+php-fpm镜像,实现lnmp。
2、使用dockerfile制作tomcat镜像,并实现对jsp测试页访问
3、安装配置harbor服务,并将打包好的镜像提交到harbor仓库
---------------------------------------------------------------------------------------------------------
1 使用dockerfile制作nginx+php-fpm镜像,实现lnmp。
# mkdir -p /opt/docker/lnmp # cd /opt/docker/lnmp #Dockerfile # cat /opt/docker/lnmp/Dockerfile FROM centos:7.9.2009 LABEL maintainer "Marko.Ou <oxz@qq.com>" RUN \ #更换yum源; \ cd /etc/yum.repos.d; \ mkdir bakdir; \ mv *.repo bakdir #添加repo配置文件 ADD CentOS7.repo /etc/yum.repos.d RUN \ #yum安装nginx/php-fpm/mysql; \ yum -y install https://mirrors.tuna.tsinghua.edu.cn/remi/enterprise/remi-release-7.rpm; \ yum -y install nginx php82-php-fpm php82-php-mysqlnd acl; \ #修改php配置文件; \ sed -ri '/^user/s#(.*= ).*#\1nginx#' /etc/opt/remi/php82/php-fpm.d/www.conf; \ sed -ri '/^group/s#(.*= ).*#\1nginx#' /etc/opt/remi/php82/php-fpm.d/www.conf; \ #准备访问资源; \ mkdir -p /data/www/wordpress; \ echo web_app > /data/www/wordpress/index.php; \ setfacl -m u:nginx:rwx /data/www #添加nginx配置文件 ADD testou.com.conf /etc/nginx/conf.d #添加启动脚本 ADD run_lnmp.sh /usr/local/bin #启动脚本 CMD ["/usr/local/bin/run_lnmp.sh"] #yum源配置文件 # cat /opt/docker/lnmp/CentOS7.repo [base] name=base baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/ gpgcheck=0 [extras] name=extras baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/ gpgcheck=0 [epel] name=epel baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch/ gpgcheck=0 #nginx配置文件 # cat /opt/docker/lnmp/testou.com.conf server { server_name www.testou.com; location / { root /data/www/wordpress; index index.php; } location ~ \.php$ { root /data/www/wordpress; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } #启动脚本 # cat /opt/docker/lnmp/run_lnmp.sh #!/bin/bash nginx /opt/remi/php82/root/usr/sbin/php-fpm & tail -f /etc/hosts #启动脚本赋执行权限 # chmod +x /opt/docker/lnmp/run_lnmp.sh #构建镜像脚本 # cat /opt/docker/lnmp/build-command.sh #!/bin/bash docker build -t lnmp:1.1-centos-7.9.2009 . #运行构建镜像脚本 # bash build-command.sh #运行容器 # docker run -d -it -p 80:80 -p 9000:9000 lnmp:1.1-centos-7.9.2009 #客户机测试 # tail -n1 /etc/hosts 10.0.0.7 www.testou.com # curl http://www.testou.com web_app
2 使用dockerfile制作tomcat镜像,并实现对jsp测试页访问
2.1
#创建制作镜像目录 # mkdir -p /opt/docker/jdk/jdk-8u241 # cd /opt/docker/jdk/jdk-8u241 #创建dockerfile # vi Dockerfile FROM centos:7.9.2009 LABEL maintainer "Marko.Ou <oxz@qq.com>" ADD jdk-8u241-linux-x64.tar.gz /usr/local RUN ln -s /usr/local/jdk1.8.0_241 /usr/local/jdk; \ chown -R root.root /usr/local/jdk/ ADD profile /etc/profile ENV JAVA_HOME /usr/local/jdk ENV JRE_HOME $JAVA_HOME/jre ENV CLASSPATH $JAVA_HOME/lib:$JRE_HOME/lib ENV PATH $PATH:$JAVA_HOME/bin #目录结构 # tree /opt/docker/jdk/jdk-8u241 /opt/docker/jdk/jdk-8u241 ├── build-command.sh ├── Dockerfile ├── jdk-8u241-linux-x64.tar.gz └── profile 0 directories, 4 files #构建镜像 # bash build-command.sh
2.2
#创建制作镜像目录 # mkdir -p /opt/docker/tomcat/tomcat-8.5.85 # cd /opt/docker/tomcat/tomcat-8.5.85 #创建dockerfile # vi Dockerfile FROM centos-jdk:8u241 LABEL maintainer "Marko.Ou <oxz@qq.com>" ADD apache-tomcat-8.5.85.tar.gz /usr/local RUN ln -s /usr/local/apache-tomcat-8.5.85/ /usr/local/tomcat; \ rm -f /usr/local/apache-tomcat-8.5.85.tar.gz ADD server.xml /usr/local/tomcat/conf RUN useradd java; \ chown -R java.java /usr/local/tomcat/ #启动程序用户权限 ADD linux39.tar.gz /data/tomcat/webapps ADD run_tomcat.sh /usr/local/tomcat/bin #启动脚本执行权限 EXPOSE 8080 8005 8009 CMD ["/usr/local/tomcat/bin/run_tomcat.sh"] #启动脚本 # cat run_tomcat.sh #!/bin/bash su - java -c "/usr/local/tomcat/bin/catalina.sh start" tail -f /etc/hosts #jsp测试页 # cat /opt/docker/tomcat-8.5.85/linux39/app1/showhost.jsp <%@page import="java.util.Enumeration"%> <br /> host: <%try{out.println(""+java.net.InetAddress.getLocalHost().getHostName());}catch(Exception e){}%> <br /> remoteAddr: <%=request.getRemoteAddr()%> <br /> remoteHost: <%=request.getRemoteHost()%> <br /> sessionId: <%=request.getSession().getId()%> <br /> serverName: <%=request.getServerName()%> <br /> scheme: <%=request.getScheme()%> <br /> <%request.getSession().setAttribute("t1","t2");%> <% Enumeration en = request.getHeaderNames(); while(en.hasMoreElements()){ String hd = en.nextElement().toString(); out.println(hd+" : "+request.getHeader(hd)); out.println("<br />"); } %> #目录结构 # tree /opt/docker/tomcat/tomcat-8.5.85 /opt/docker/tomcat/tomcat-8.5.85 ├── apache-tomcat-8.5.85.tar.gz ├── build-command.sh ├── Dockerfile ├── linux39 │ ├── app1 │ │ ├── index.html │ │ └── showhost.jsp │ └── app2 │ ├── index.html │ └── showhost.jsp ├── linux39.tar.gz ├── run_tomcat.sh └── server.xml 3 directories, 10 files #构建镜像 # bash build-command.sh #测试运行容器 # docker run -d -it -p 8080:8080 tomcat-linux39:app1 #客户机测试 # curl http://10.0.0.7:8080/linux39/app1/ volume page for app1 # curl http://10.0.0.7:8080/linux39/app1/showhost.jsp <br /> host: 0d5b84c8646d <br /> remoteAddr: 10.0.0.27 <br /> remoteHost: 10.0.0.27 <br /> sessionId: E21E2030F42FD18480D1AD4CA6687576 <br /> serverName: 10.0.0.7 <br /> scheme: http <br /> user-agent : curl/7.29.0 <br /> host : 10.0.0.7:8080 <br /> accept : */* <br />
3 安装配置harbor服务,并将打包好的镜像提交到harbor仓库
#安装1.7.6版本harbor #先安装docker-ce、docker-ce-cli、docker-compose # yum -y install docker-compose #安装harbor-1.7.6 # cd /usr/local/src # wget https://storage.googleapis.com/harbor-releases/release-1.7.0/harbor-offline-installer-v1.7.6.tgz # tar xf harbor-offline-installer-v1.7.6.tgz -C /usr/local # cd /usr/local/harbor # vi harbor.cfg #1.10版本修改harbor.yml,只改hostname、harbor_admin_password,注释ssl相关 hostname = 10.0.0.7 #harbor服务端地址 harbor_admin_password = 123456 #密码,用户名为admin # ./install.sh #访问web界面:http://10.0.0.7,登录,用户名amin,密码123456 #新建项目 项目->新建项目->项目名称:oxz 访问级别:勾选公开 #客户端修改docker service,增加选项--insecure-registry # vi /usr/lib/systemd/system/docker.service ... ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --insecure-registry 10.0.0.7 # systemctl daemon-reload # systemctl restart docker #客户端修改镜像tag # docker tag lnmp:1.1-centos-7.9.2009 10.0.0.7/oxz/lnmp:1.1-centos-7.9.2009 # docker tag tomcat-linux39:app1 10.0.0.7/oxz/tomcat-linux39:app1 #客户端上传镜像 # docker push 10.0.0.7/oxz/lnmp:1.1-centos-7.9.2009 # docker push 10.0.0.7/oxz/tomcat-linux39:app1 #无法上传时,先登录再上传 # docker login --username=admin --password=123456 10.0.0.7