Docker自动化部署实战

引用自:https://www.yunliantaida.com

简述

初创公司,最简单高效的前段自动化部署方案,分享给大家.跟着我一步步操作有问题评论区提问

准备

  • 一台Centos7至少内存为4G的机器。

安装Docker

curl -sSL https://get.docker.com/ | sh

安装Gitlab

sudo yum install -y git vim gcc glibc-static telnet
sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
  • 新建 /etc/yum.repos.d/gitlab-ce.repo 文件
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1

开始安装

sudo EXTERNAL_URL="http://www.syappmp.com" yum install -y gitlab-ce
sudo gitlab-ctl reconfigure

域名设置一个内网可以访问到这台机器IP的域名
访问 http://www.yunliantaida.com 可以看到你安装的gitlab
设置好用户名密码

安装gitlab ci runner

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.rpm.sh | sudo bash
sudo yum install gitlab-ci-multi-runner -y
sudo usermod -aG docker gitlab-runner
sudo service docker restart
sudo gitlab-ci-multi-runner status

返回 gitlab-runner: Service is running! 标示安装成功

GitLab CI设置

  • 关注我待更新

创建NuxtJS

  • 开发机安装好NodeJS, Yarn具体安装百度有很多
  • 直接场景Nuxt项目
yarn create nuxt-app yunliantaida
cd yunliantaida
npm run dev

应用现在运行在 http://localhost:3000 上运行。

编写DockerFile

  • nutx项目目录下新建Dockerfile
FROM node:10.16.3-alpine
ENV NODE_ENV=production
ENV HOST 0.0.0.0
RUN mkdir -p /nuxt
COPY .nuxt/  /nuxt/.nuxt
COPY static/  /nuxt/static
COPY nuxt.config.js  /nuxt/
COPY package.json  /nuxt/
WORKDIR /nuxt
EXPOSE 3000
RUN ls
RUN npm config set registry https://registry.npm.taobao.org
RUN npm install
CMD ["npm", "start"]

编写DockerRun脚本

  • nutx项目目录下新建dockerrun.sh
#!/bin/sh
IMAGE_NAME=nginx/www.syappmp.com
APP_NAME=www.syappmp.com
APP_VERSION=1.0
#检查程序是否在运行
is_exist() {
  pid=$(docker ps -a --filter name=${APP_NAME} | awk '{if (NR>1){print $1}}')
  #如果不存在返回1,存在返回0
  if [ -z "${pid}" ]; then
    return 1
  else
    return 0
  fi
}
is_images_exist() {
  pidDockerImages=$(docker images | grep '' | awk '{print $3}')
  if [ -z "${pidDockerImages}" ]; then
    return 1
  else
    return 0
  fi
}
is_images_exist
if [ $? -eq "0" ]; then
  docker rmi ${pidDockerImages}
fi
is_exist
if [ $? -eq "0" ]; then
  echo ">>> Dcoker is already running PID=${pid} <<<"
  docker stop ${pid}
  docker rm ${pid}
fi
docker run --name ${APP_NAME} -d -p 8020:80 ${IMAGE_NAME} &
echo ">>> start ${APP_NAME}:${APP_VERSION} running successed PID=$! <<<"

编辑自动化部署脚本

  • nutx项目目录下新建 .gitlab-ci.yml (注意前面的.要复制)
stages:
  - build
  - deploy

build:
  stage: build
  tags:
    - test
  script:
    #打包
    - npm install
    - npm run build
    - docker build -t nginx/www.syappmp.com .
run:
  stage: deploy
  tags:
    - test
  script:
    #执行脚本
    - sh dockerrun.sh

  • 将Nutx项目上传到GitLab上,每次提交推送git push会自动化打包编译部署运行Docker容器
  • 访问 http://内网服务IP:8020
  • 如果有同学GitLab不会用可以百度一下

请帮忙点个赞 :)

转自:https://www.jianshu.com/u/9c5cb1ee4c46

你可能感兴趣的:(Docker自动化部署实战)