Why am I doing this first

Recently, there are many iterations of the project version in hand, and then they have to be redeployed every time, which is really very, very troublesome. Therefore, it should be an urgent small demand to do some automatic deployment functions like this.

What is the Docker

  • Virtualization at the operating system level
  • Is a container that is independent of the host and other processes

The characteristics of

  • It consumes less system resources than traditional VMS
  • The vm starts faster than traditional VMS
  • Continuous delivery and deployment (this is what I mainly want to do because our project is deployed very frequently)
  • Easier migration

The core concept

  • The mirror
  • The container
  • warehouse

Docker installation (since our service is Linux, the installation here is mainly Linux,Windows installation.MAC Installation Tutorial)

1. Check the CentOS version. The Docker kernel version must be later than 3.10

uname -r
Copy the code

2. Update the yum package

sudo yum update
Copy the code

3. Install required packages

Lvm2 is a devicemapper driver dependent on sudo yum install yum-utils  device-mapper-persistent-data lvm2Copy the code

4. Add software source information

sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
Copy the code

5. View the available version of Docker-CE

Yum list docker - ce - showduplicates | sort - r / / if you need to show only the table version, List sudo yum-config-manager --enable docker-ce-edge sudo yum-config-manager --enable docker-ce-testCopy the code

6. Install the specified version of Docker-CE

Sudo yum install docker - ce - 18.06.3. Ce - 3. El7Copy the code

7. Set startup

Sudo systemctl enable docker sudo systemctl start docker sudo systemctl start dockerCopy the code

8. Verify whether the installation is successful (the client and service parts indicate that docker has been successfully installed and started)

docker version
Copy the code

9. Docker startup status

systemctl status docker
Copy the code

10. Configure mirror acceleration

  • Azure China image dockerhub.azk8s.cn
  • Seven niuyun accelerator reg-mirror.qiniu.com
Json vi /etc/docker/daemon.json // configure /etc/docker/daemon.json {"registry-mirrors": [" https://dockerhub.azk8s.cn ", "https://reg-mirror.qiniu.com"]} / / restart docker service, Sudo systemctl daemon-reload sudo systemctl restart dockerCopy the code

11. Download an Nginx to see how fast it works

docker pull nginx
Copy the code

= = = = = = = = = = = = = = = = = = = = = = = the installation is over = = = = = = = = = = = = = = = = = = = = = = =

Nginx service

1. Pull the official image – docker-oriented read-only template (docker image testing has been done before is not needed)

docker pull nginx
Copy the code

2. View the installation result

docker images nginx
Copy the code

3. Create an index. HTML and say Hello docker!!

mkdir www echo 'hello docker!! ' >> www/index.htmlCopy the code

4. View the index. HTML content

cat www/index.html
Copy the code

5. Enable and map ports

docker run -p 8000:80 -v $PWD/www:/usr/share/nginx/html nginx
Copy the code

6. Enter the IP address and port number on the page. The index content is displayed.

7. Background execution (this will give you a UUID to use when shutting down the service)

docker run -p 8000:80 -v $PWD/www:/usr/share/nginx/html -d nginx
Copy the code

8. Stop the service

Docker start 050 docker stop 050 docker start 050Copy the code

9. Check the process

Docker ps-a docker ps-aCopy the code

10. Uuid of container 050

 docker exec -it 050 /bin/bash
Copy the code

11. Go to the dummy terminal to view the index.html created earlier

cd /usr/share/nginx/html
cat index.html
Copy the code

12. Exit the pseudo terminal

exit
Copy the code

12. Delete the mirror

Docker stop 050 delete docker rm 050Copy the code

= = = = = = = = = = = = = = = = = = = = = start a simple nginx service completes the = = = = = = = = = = = = = = = = = = = =

Docker running process

1. Image —– Docker-oriented read-only template

2. Container —— Running instance of the image

3. Registry —— A server that stores images

= = = = = = = = = = = = = = = = = = = = = = = = = = = = luxuriant line = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Dockerfile custom image

Create an nginx

cd docker
mkdir nginx
Copy the code

2. Create a Dockerfile and write the configuration

cd nginx
vi Dockerfile
Copy the code
// latest RUN echo '<h1>Hello, candy! </h1>' > /usr/share/nginx/html/index.htmlCopy the code

3. Customize an image

// Nginx mirror, version is candy // note: the last. Docker build-t nginx:candyCopy the code

4. Run

// Specify candy for this version of docker run -p 800:80 nginx:candyCopy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = luxuriant line = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Customize PM2 images

Pm2 – Utilize multi-core resources

1. Create a PM2 folder

mkdir pm2
Copy the code

2. Create a package. Json

npm init -y
Copy the code

3. Create a simple program –app.js

vi app.js
Copy the code

4. Write something random about app.js

Const Koa = require(' Koa ') const app = new Koa() app.use(CTX => {math.random () > 0.8? abc() : '' ctx.body = 'Hello Docker' }) app.listen(3000, () => { console.log('app started at http://localhost:3000/') })Copy the code

5. Create the process.yml file

vi process.yml
Copy the code

6. process.yml

apps:
  - script : app.js
    instances: 2
    watch  : true
    env    :
      NODE_ENV: production
Copy the code

7. Customized Dockerfile

vi Dockerfile
Copy the code

8. Edit Dockerfile

FROM keymetrics/pm2:latest-alpine # go to app directory ADD. /usr/src/app /usr/ SRC /app/RUN NPM config set registry https://registry.npm.taobao.org/ && \ NPM I # exposed outside of port 3000 # pm2 EXPOSE using the command in the docker pm2 - docker CMD [pm2 - runtime, "start", "process.yml"]Copy the code

9. Run customizations

docker build -t pm2 .
Copy the code

10. Run the docker

docker run -p 3000:3000 pm2
Copy the code

11. Background startup

docker run -p 3000:3000 -d pm2
Copy the code

12. Close

docker stop a06
Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = luxuriant line = = = = = = = = = = = = = = = = = = = = = = = = = = = =

Docker Compose Docker Compose

  • Docker Compose (Docker Compose) – Docker Compose (Docker Compose)
  • Docker Compose is a tool, command line tool.
  • This tool allows you to define multi-container Docker applications using YML files
  • These containers can be created or managed by a single command as defined in the YML file

1. Install the docker – compose

yum install docker-compose
Copy the code

2. Create a folder

mkdir helloworld
cd helloworld
Copy the code

3. New docker – compose. Yml

Version: '3.1' Services: hello-world: image: hello-worldCopy the code

4. Start

docker-compose up
Copy the code

5. Parse the parameters

Yml # docker-compose version version: '3.1' # docker-compose Ports: -27017 :27017 mongo-express: image: mongo-express restart: always ports: - 8000:8081Copy the code

Nginx —- Docker deploys a project with a front and back end separation

1. Synchronize the local code to the server in the root directory

  • I use WebStorm for my editor, so I use the Deployment plug-in that comes with it
  • Editing a Configuration File

!

  • Editing Mapping Information

  • Edit ignore file (highlight, be sure to ignore node_modules, otherwise it will be very easy to upload failed, I initially did not configure here, stuck here for a while)

  • Select the required file, right click can be synchronized to the server

2. In the root directory nginx/conf. D/docker. Conf, configure port, path, the proxy service

server { listen 80; location / { root /var/www/html; index index.html index.htm; } the location/CDS {proxy_pass http://192.168.0.210:9008/cds; proxy_redirect off; proxy_cookie_path / "/; httponly; SameSite=Lax"; }}Copy the code

3. Create docker-comemess. yml pairs for all project clusters in the root directory

Version: '3.1' services: nginx: restart: always image: nginx ports: -3000 :80 Volumes: - ./nginx/conf.d/:/etc/nginx/conf.d - ./hellowVue/dist:/var/www/html/Copy the code

4. Perform build and Deployment synchronization for the front-end project to the service provider

5. Start the service

docker-compose up -d
Copy the code

= = = = = = = = = = = = = = = = = = = = = = = = = = = = to be continued = = = = = = = = = = = = = = = = = = = = = = = = = = = =

The project has been deployed at this point, and tomorrow we will continue to update the automated script file to automatically update the code and restart the service every time we build locally.

Webhook implements continuous integration

1. The concept

Webhooks are very similar to the subscription-publish model of asynchronous programming, in that one side triggers events and the other side listens for execution. When I perform some operations, the server code is automatically updated, and some custom scripts are executed. For example, when I push, the server automatically pulls the code and updates the restart

2. Online configuration

  • Go to Github, or GitLab, my project is on GitLab so let’s take this as an example

2. Create a weblinks.js file in the local root directory

Var HTTP = require(' HTTP ') // Var handler = require('gitlab-webhook-handler') var handler = require('gitlab-webhook-handler') var handler = require('gitlab-webhook-handler') var handler = createHandler({  path: '/webhooks', secret: Function run_cmd(CMD, args, function run_cmd(CMD, args, callback) { var spawn = require('child_process').spawn; var child = spawn(cmd, args); var resp = ""; child.stdout.on('data', function (buffer) { resp += buffer.toString(); }); child.stdout.on('end', function () { callback(resp) }); } / / debug / / run_cmd (the 'sh' ['. / deploy - dev. Sh '], function (text) {the console. The log (text)}); http.createServer(function (req, res) { handler(req, res, function (err) { res.statusCode = 404 res.end('no such location') }) }).listen(3000,() =>{ console.log('WebHooks Listern  at 3000'); }) handler.on('error', function (err) { console.error('Error:', err.message) }) // handler.on('*', function (event) { // console.log('Received *', event.payload.action); // // run_cmd('sh', ['./deploy-dev.sh'], function(text){ console.log(text) }); // }) handler.on('push', function (event) { console.log('Received a push event for %s to %s', event.payload.repository.name, event.payload.ref); If (event.paypay. ref === 'refs/heads/master'){console.log('deploy master.. ') run_cmd('sh', ['./deploy-dev.sh'], function(text){ console.log(text) }); } }) // handler.on('issues', function (event) { // console.log('Received an issue event for % action=%s: #%d %s', // event.payload.repository.name, // event.payload.action, // event.payload.issue.number, // event.payload.issue.title) // })Copy the code

4. Create a script file deploy-dev.sh in the root directory

Git pull # force recompile docker-compose down Docker-compose up -d -- force-set --build # custom mirror # docker build -t myapp:pm2./backend # Restart the container # docker stop myapp # docker rm myapp # docker run --name myapp -p 3000:3000 -d myapp:pm2Copy the code

5. Do the same and send to the server

6. Run weblinks.js on a service so you can hear every push you do

node webhooks.js
Copy the code

= = = = = = = = = = = = = here each time you push will automatically update service deployment. = = = = = = = = = = = = = = = = = =