ubuntu 云服务器 部署node express 持续化集成docker镜像且开放外网访问 -2021-5-2

屈晨
2023-12-01
  1. 使用expresss创建后台框架

    npm install express --save   
    
    npm install -g express-generator 
    
    express my-backed
    
    -----配置外网访问-----
    项目目录/bin/www/文件:
    server.listen(port);
    改为:
    server.listen(port, '0.0.0.0');
    
  2. 将代码放入gitee(github)

  3. 持续集成(后端)以下3个文件相互耦合, 且只执行font-admin.sh文件即可( bash -x /opt/dockerFile/nodeBacked.sh)

    1. docker 镜像构筑文件 nodeBackedFile(绝对地址:/opt/dockerFile)

      FROM node:latest
      
      # Create app directory
      RUN mkdir -p /opt/code
      WORKDIR /opt/code
      
      # Bundle app source
      COPY . /opt/code 
      
      EXPOSE 3000
      
    2. 镜像内部执行文件command.sh(绝对地址:/opt/dockerFile)

      #!/bin/bash
      cd /opt/code/node-backed
      
      npm install
      
      npm run start
      
    3. 持续集成shell 文件nodeBacked.sh(可使用集成工具执行这个文件,也可手动执行 bash -x )(绝对地址:/opt/shellFile)

      #!/bin/bash
      myPath="/opt/code/"
      if [ -d "$myPath" ]; then
      	rm -rf "$myPath"
             	mkdir -p  "$myPath" && cd "$myPath"
      else
      	mkdir -p  "$myPath" && cd "$myPath"
      fi
      
      if [ $? -ne 0 ]; then 
      	echo "faild"
      else
      	git clone git@gitee.com:jiekeklu6/node-backed.git
      	echo $?
      	if [ $? -eq 0 ]; then
      		cp /opt/dockerFile/nodeBackedFile .
      		cp /opt/dockerFile/command.sh .
      		docker stop node-b
      		docker rm node-b
      		docker rmi node-backed:latest
      		docker build -f nodeBackedFile -t node-backed:latest .
      		docker run -d --name "node-b" -p 3000:3000  node-backed:latest /bin/bash -c "bash /opt/code/command.sh"
      	fi
      fi
      
    4. 持续集成(前端后台)以下2个文件相互耦合,且只执行font-admin.sh文件即可( bash -x /opt/dockerFile/font-admin.sh)

      1. docker 镜像构筑文件 fontAdminFile(绝对地址:/opt/dockerFile)

        FROM httpd:2.4
        
        COPY ./font-admin/dist/ /usr/local/apache2/htdocs/
        COPY ./fontCommand.sh /usr/local/apache2/
        
        EXPOSE 80
        
      2. 持续集成shell 文件font-admin.sh(可使用集成工具执行这个文件,也可手动执行 bash -x )(绝对地址:/opt/shellFile)

        #!/bin/bash
        myPath="/opt/font-admin/"
        if [ -d "$myPath" ]; then
        	rm -rf "$myPath"
               	mkdir -p  "$myPath" && cd "$myPath"
        else
        	mkdir -p  "$myPath" && cd "$myPath"
        fi
        
        if [ $? -ne 0 ]; then 
        	echo "faild"
        else
        	git clone git@gitee.com:jiekeklu6/font-admin.git
        	echo $?
        	if [ $? -eq 0 ]; then
        		echo "git clone success"
        		cp /opt/dockerFile/fontAdminFile .
        		docker stop fontadmin
        		docker rm fontadmin
        		docker rmi fontadmin:latest
        		docker build -f fontAdminFile -t fontadmin:latest .
        		docker run -dit --name fontadmin -p 8080:80 fontadmin
        	fi
        fi
        
 类似资料: