A, use,docker-compose.ymlandDockerfileBuild the project

  • 1. Basic introduction to the project

    Use express framework routing to access static files in the views directory

  • 2. Basic structure of the project

    .
    ├ ─ ─ Dockerfile
    ├ ─ ─ README.md
    ├ ─ ─ docker-compose.yml
    ├ ─ ─ index.js
    ├ ─ ─ package-lock.json
    ├ ─ ─ package.json
    └ ─ ─ views
        ├ ─ ─ index.html
        └ ─ ─ other.html
    Copy the code
  • Index.js file code

    const express = require('express')
    const app = express()
    const router = express.Router()
    
    const path = __dirname + '/views/'
    const port = 8080
    
    router.use(function (req, res, next) {
      console.log('/' + req.method)
      next()
    })
    
    router.get('/'.function (req, res) {
      res.sendFile(path + 'index.html')
    })
    
    router.get('/other'.function (req, res) {
      res.sendFile(path + 'other.html')
    })
    
    app.use(express.static(path))
    app.use('/', router)
    
    app.listen(port, function () {
      console.log(`Example app listening on port ${port}! `)})Copy the code
  • 4, Dockerfile file content

    FROM node:9.0
    COPY . /app
    WORKDIR /app
    RUN npm install --registry=https://registry.npm.taobao.org
    EXPOSE 8080
    CMD node index.js
    Copy the code
  • Docker-comemess. yml file content

    version: '3'
    
    services: 
      nodejs: 
    Package the image with the Dockerfile file in the current directory
        build: 
          context: .
          dockerfile: Dockerfile
        image: nodejs
        container_name: nodejs
        restart: unless-stopped
        ports: 
          - "8080:8080"
        networks: 
          - app-network
    
    networks:
      app-network:
        driver: bridge
    Copy the code
    • image: Specifies the mirror.
    • restart: Restart rule
    • prots: Exposed port
    • networks: Join a network
  • 6, test run container, then access port 8080

    docker-compose up -d --build
    Copy the code

Second, in combination withnginxThe agent tonodeservice

  • 1. Create a nginx-conf/nginx.conf file

    server {
      listen 80;
      listen[: :] :80;
      server_tokens off;
    
      You can change it to your own domain name
      server_name location;
    
      # Access/directory to access
      location / {
        root /var/www/html;
        index index.html index.htm;
      }
      # Reverse proxy to node service
      location /nodejs {
        Nodejs is the name of the node container
        proxy_passhttp://nodejs:8080/nodejs; }}Copy the code
  • 2, modify docker-comemess. yml file

    version: '3'
    
    services: 
      nodejs: 
    Package the image with the Dockerfile file in the current directory
        build: 
          context: .
          dockerfile: Dockerfile
        image: nodejs
        container_name: nodejs
        restart: unless-stopped
        # ports: 
    # - "8080-8080"
        networks: 
          - app-network
    # Add static server
      webServer:
        image: nginx:stable-alpine
        container_name: webServer
        restart: unless-stopped
        ports: 
          - "80:80"
    Data volume, which associates local files with directories in the Docker container
        volumes: 
          - ./web:/var/www/html
          - ./nginx-conf:/etc/nginx/conf.d
        depends_on: 
          - nodejs
        networks: 
          - app-network
    
    networks:
      app-network:
        driver: bridge
    Copy the code
  • 3, modify the index.js file (mainly considering the path written on the nginx reverse proxy)

    . router.get('/nodejs'.function (req, res) {
      res.sendFile(path + 'index.html')
    })
    
    router.get('/nodejs/other'.function (req, res) {
      res.sendFile(path + 'other.html')})...Copy the code
  • 4. Delete the image and generate a new image

    docker-compose up -d --build
    Copy the code
  • 5. View logs

    docker-compose  logs -f
    Copy the code
  • 6. Type anything in the web/index.html file that localhost can access. Localhost /nodejs/other can access the proxy nodeJS service

Three, connectionmysql

  • 1, modify docker-comemage. yml file and add mysql configuration

    version: '3'
    
    services: 
      nodejs: 
    Package the image with the Dockerfile file in the current directory
        build: 
          context: .
          dockerfile: Dockerfile
        image: nodejs
        container_name: nodejs
        restart: unless-stopped
        # ports: 
    # - "8080-8080"
    # This time added
        depends_on:
          - db
        networks: 
          - app-network
    # Add static server
      webServer:
        image: nginx:stable-alpine
        container_name: webServer
        restart: unless-stopped
        ports: 
          - "80:80"
    Data volume, which associates local files with directories in the Docker container
        volumes: 
          - ./web:/var/www/html
          - ./nginx-conf:/etc/nginx/conf.d
        depends_on: 
          - nodejs
        networks: 
          - app-network
    Connect to database (new)
      db:
        image:  Mysql: 5.7 # daocloud. IO/library/mysql: 8
        container_name: db
        restart: unless-stopped
        ports: 
          - "3306:3306"
        environment: 
          - MYSQL_ROOT_PASSWORD=123456
          - MYSQL_USER=test1 MYSQL_USER=test1
          - MYSQL_PASSWORD=test1 # test1
          - MYSQL_DATABASE=nodeApp
          - TZ=Asia/Shanghai
        volumes: 
          - ./mysql/data:/var/lib/mysql                                                                             
          - ./mysql/conf:/etc/mysql/conf.d                                                                          
          - ./mysql/logs:/log    
          - ./mysql/init:/docker-entrypoint-initdb.d                                                                                   
        command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    # command: --default-authentication-plugin=mysql_native_password #
    
        networks: 
          - app-network
    
    networks:
      app-network:
        driver: bridge
    Copy the code
  • 2. Create mysql and the corresponding directory structure in this project

  • 3. Configure the database connection in index.js

    const mysql = require('mysql')
    const connection = mysql.createConnection({
      host: 'db'.port: 3306.// You can also use the root user directly
      user: 'root'.password: '123456'.database: 'nodeApp'
    })
    Copy the code
  • 4. Define access routes

    router.get('/nodejs/mysql'.function (req, res) {
      connection.query('select 1 + 1 as total'.function (err, results, fields) {
        if (err) {
          console.log(err)
          res.end(JSON.stringify(err))
        }
        console.log('Test code')
        res.end('total' + results[0].total)
      })
    })
    Copy the code
  • 5, test,

  • 6, supplementary note, to find the mirror can go to the domestic mirror warehouse

Four, aboutdocker-compose.ymlThe deployment ofmysqlA few additions to

  • 1. If you are using a higher version of mysql, enable authorization in mysql/init

    -- Mysql5.7 and above
    use mysql;
    ALTER USER 'root'@The '%' IDENTIFIED WITH mysql_native_password BY '123456'; 
    flush privileges; 
    Copy the code
  • 2. If you are not using root in your Node code, you need to authorize the connection

    -- Non-root user authorization
    use mysql;
    ALTER USER 'test1'@The '%' IDENTIFIED WITH mysql_native_password BY 'test1';
    flush privileges;
    Copy the code

Use your own mirror image

  • 1. Ali’s private mirror

  • 2. Create your own image on it (must be created first)

  • 3, copy the Dockerfile and index.js dependencies from the project to a separate folder, and create a. Dockerignore file

      test1 tree .
    .
    ├ ─ ─ Dockerfile
    ├ ─ ─ index.js
    ├ ─ ─ package-lock.json
    └ ─ ─ package.json
    
    0 directories, 4 files
    Copy the code
    .idea
    .node_modules
    node_modules
    .vscode
    Copy the code
  • 4. Run commands to generate an image

    docker build -t kuangshp/node_express . 
    Copy the code
  • 5. View the locally generated image

  • 6. Publish the local image to ali Cloud private warehouse

    # In my ali cloud Docker private warehouse side can view my own
    sudo docker login [email protected] registry.cn-shanghai.aliyuncs.com
    sudo Docker tag (ImageId) registry.cn-shanghai.aliyuncs.com/xx/node_express: [the mirror version number]
    sudo The docker push registry.cn-shanghai.aliyuncs.com/xx/node_express: [the mirror version number]
    Copy the code
  • 7. Modify the configuration in docker-comemage. yml

    version: '3'
    
    services: 
      nodejs: 
    Package the image with the Dockerfile file in the current directory
        # build: 
    # context: .
    # dockerfile: Dockerfile
        image: registry.cn-shanghai.aliyuncs.com/kuangshp/node_express:latest
        # image: nodejs
        container_name: nodejs
        restart: unless-stopped
        # ports: 
    # - "8080-8080"
        depends_on:
          - db
        networks: 
          - app-network
    # Add static server
      webServer:
        image: nginx:stable-alpine
        container_name: webServer
        restart: unless-stopped
        ports: 
          - "80:80"
    Data volume, which associates local files with directories in the Docker container
        volumes: 
          - ./web:/var/www/html
          - ./nginx-conf:/etc/nginx/conf.d
        depends_on: 
          - nodejs
        networks: 
          - app-network
    Connect to database
      db:
        image:  Mysql: 5.7 # daocloud. IO/library/mysql: 8
        container_name: db
        restart: unless-stopped
        ports: 
          - "3306:3306"
        environment: 
          - MYSQL_ROOT_PASSWORD=123456
          - MYSQL_USER=test1 MYSQL_USER=test1
          - MYSQL_PASSWORD=test1 # test1
          - MYSQL_DATABASE=nodeApp
          - TZ=Asia/Shanghai
        volumes: 
          - ./mysql/data:/var/lib/mysql                                                                             
          - ./mysql/conf:/etc/mysql/conf.d                                                                          
          - ./mysql/logs:/log    
          - ./mysql/init:/docker-entrypoint-initdb.d                                                                                   
        command: --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
    # command: --default-authentication-plugin=mysql_native_password #
    
        networks: 
          - app-network
    
    networks:
      app-network:
        driver: bridge
    Copy the code
  • 8, run,

    docker-compose up -d --build
    Copy the code