docker

Delete the previous version first

$ sudo yum remove docker docker-client docker-client-latest docker-common docker-latest docker-latest-logrotate docker-logrotate docker-engine
Copy the code

Install required dependencies

Y y y y y y y y y y y y y y y y y y y y y y y --add-repo https://download.docker.com/linux/centos/docker-ce.repoCopy the code

Install the docker – ce

$ sudo yum install docker-ce docker-ce-cli containerd.io
Copy the code

Changing the Image Installation Source (Acceleration)

$vim /ect/docker/daemon.json // configure the following {"registry-mirrors": ["https://registry.docker-ce.com"] } $ systemctl daemon-reload $ systemctl restart dockerCopy the code

jenkins

The installation

// Docker pull Jenkins/Jenkins: LTS // create and run the container, need to map internal to external docker, otherwise internal can not use docker, map file internal docker command to external // Sock // file in the Jenkins container needs to be changed to Jenkins docker run-itd --name jenkins -p 8000:8080 -p 50000:50000 -v /usr/bin/docker:/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock Jenkins/Jenkins: LTS docker logs -f jenkins_pjmCopy the code

Configuration Jenkins

Open IP:8080 and enter Jenkins

Enter the initial password

Click install the recommended plug-in and wait for it to complete

Create a user

Configuration instance, the default line, after completion can enter the main interface

Configuration to accelerate

https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json

Installing a plug-in

  • Generic Webhook Trigger(Webhook hooks that Trigger events when git or GitLab commits)

  • Build With Parameters

  • Role-based Authorization Strategy(User-Role Authorization)

  • ThinBackup (backup)

Add SSH credentials (via Github pull code)

$ssh-keygen -t rsa -b 4096 -c '[email protected]' // The public key is id_rsa.pub in the generated folder, and the key is ID_rsaCopy the code

Generate personal account token(used by Webhook calls)

nginx

The default directory is /usr/share/nginx/htmlCopy the code

Disabling the Firewall

// Disable systemctl stop firewalld.service systemctl disable firewalld.service // Disable the firewall on a certain port firewall-cmd --add-port=8080/tcp --permanent firewall-cmd --reloadCopy the code

The whole process

Code push -> trigger webhook -> Jenkins receives to start trigger process -> Jenkins pulls code to local working directory -> Execute shell in local working directory -> build image through Dockerfile of pulled code -> Determine whether docker is useful the image is running container -> delete, if not directly through the image to create a container, the image nginx exposed port 80 (docker), map to the external custom port -> complete the whole process, through IP: port can be accessedCopy the code

Jenkins create task

Shell code

#! /bin/bash # Build webapp image docker build from Dockerfile --no-cache -t webapp -a | grep webapp - container & # > / dev/null if there is no running if [0] $? - can be; Then echo "webapp-container is not running" else echo "webapp-container is running Find the running webapp - container vessel and stop it matchingStarted = $(docker ps - filter = "name = webapp - container" - q | xargs) if [-n $matchingStarted ]; Then docker stop $matchingStarted fi # find all webapp-container and delete it matching=$(docker ps-a --filter="name=webapp-container" -q | xargs) if [ -n $matching ]; Then docker rm $matching # create and run a webapp-container that mirrors the webApp, and map the inner nginx exposed port 80 to the outer port 3333 docker run-itd --name webapp-container -p 3333:80 webappCopy the code

Making configuration webhook

The project configures the Dockerfile

COPY --FROM=<name> Can reference the image generated at this stage FROM node:lts-alpine as Build -stage # will be the source file. WORKDIR /app # Dockerfile creates a new layer of RUN NPM on the docker each time it is executed Install - registry=https://registry.npm.taobao.org RUN NPM RUN the build # # production stage pull nginx mirror the FROM Nginx: stables -alpine as production-stage # COPY --from=build-stage /app/dist /usr/share/nginx/html # nginx EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]Copy the code

Project configuration. Dockerignore

node_modules
.DS_Store

# node-waf configuration
.lock-wscript

build/Release
.dockerignore
Dockerfile
*docker-compose*

# Logs
Logs
*.log

# Runtime data
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw*
pdis
*.pdi
*.seed
.git
.hg
.svn
Copy the code

Differentiating environment builds (from nodes)

The new server can be successfully connected only after the environment is configured

# yum install -y Java 1.8.0-openJDK # yum install -y Java 1.8.0-openJDKCopy the code

Jenkins is configured from the node

The new server is configured with a public key

Vim /root/.ssh/authorized_keys Add the configuration and saveCopy the code