Docker installation

  1. Install Docker
curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
Copy the code
  1. Docker – compose
Sudo curl - L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname - s) - $(uname -m)" - o /usr/local/bin/docker-composeCopy the code

Second, establish the project catalog

Create my own project directory under Liunx (using my own project as an example)

  • Nginx: nginx configuration directory
  • Static: indicates the directory for storing static resources
  • Utils-dist: Vue package directory (content pulled directly from Git)
  • Utis-koa: node code directory (content pulled directly from Git)
  • Docker-compose. Yml: docker-compose configuration file

Docker-compose configuration

version: '3.1'
services:
  app-utils:
      container_name: app-utils
      # build container
      # build: ./utils-koa/Dockerfile
      build:
        context: ./utils-koa
        dockerfile: Dockerfile
      ports:
        - "3000:3000"
      environment:    Set time zone 8 east Shanghai time
        - SET_CONTAINER_TIMEZONE=true
        - CONTAINER_TIMEZONE=Asia/Shanghai
      volumes:    # mount host time zone 8
        - /etc/localtime:/etc/localtime:ro
  nginx:
    restart: always
    image: nginx
    ports:
      - "80:80"
    environment:    Set time zone 8 east Shanghai time
        - SET_CONTAINER_TIMEZONE=true
        - CONTAINER_TIMEZONE=Asia/Shanghai  
    volumes:
      - ./nginx/conf.d/:/etc/nginx/conf.d
      - ./utils-dist:/var/www/html/
      - /etc/localtime:/etc/localtime:ro
Copy the code

Note: If scheduled tasks are used in the project, you need to configure the time zone. For other configurations, see the Docker-compose document

Iv. Node project configuration

Go to your node directory and create a Dockerfile file to write the configuration

FROM node:12-slim

# Install latest chrome dev package and fonts to support major charsets (Chinese, Japanese, Arabic, Hebrew, Thai and a few others)
# Note: this installs the necessary libs to make the bundled version of Chromium that Puppeteer
# installs, work.
RUN apt-get update \
    && apt-get install -y wget gnupg \
    && wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
    && sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
    && apt-get update \
    && apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
      --no-install-recommends \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /nodeapp

ADD ./server /nodeapp/server
ADD ./package.json /nodeapp

RUNmkdir -p /nodeapp/logs \ && npm i

EXPOSE 3000

# Install puppeteer so it's available in the container.
# Add user so we don't need --no-sandbox.
# same layer as npm install to keep re-chowned files from using up several hundred MBs more space
RUNgroupadd -r pptruser && useradd -r -g pptruser -G audio,video pptruser \ && mkdir -p /home/pptruser/Downloads \ && chown  -R pptruser:pptruser /home/pptruser \ && chown -R pptruser:pptruser /nodeapp

# Run everything after as non-privileged user.
USER pptruser

CMD ["node"."./server/index.js"]
Copy the code

Note: Lines 28-34 need to be configured if the puppeteer package is used in the project

5. Nginx configuration

Create a conf.d folder in the nginx folder and create the nginx. Conf configuration file in that folder

server {
    listen* :80;
    listen[: :] :80;
    server_name localhost;
    return 301 http://localhost$request_uri;
}

server {
    listen* :80;
    listen[: :] :80;
    server_name  localhost;
    location / {
        root   /var/www/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html;
    }

    location /api {
       # domains that allow cross-domain requests
       # add_header 'Access-Control-Allow-Origin' $http_origin;
       Allow clients to submit cookies
       # add_header 'Access-Control-Allow-Credentials' 'true';
       Allow client request methods
       # add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, DELETE, PUT';
       Allow the client to submit the request header
       # add_header 'Access-Control-Allow-Headers' 'Origin, x-requested-with, Content-Type, Accept, Authorization';
       The response header that the client is allowed to access
       # add_header 'Access-Control-Expose-Headers' 'Cache-Control, Content-Language, Content-Type, Expires, Last-Modified, Pragma';
       # Process precheck requests
              if ($request_method = 'OPTIONS') {
                Precheck the request cache time
                add_header 'Access-Control-Max-Age' 1728000;
                add_header 'Content-Type' 'text/plain; charset=utf-8';
                add_header 'Content-Length' 0;
                return 204;
            }
            proxy_pass  http://app-utils:3000;
            proxy_redirect     off;
            proxy_set_header   Host             $host;
            proxy_set_header   X-Real-IP        $remote_addr;
            proxy_set_headerX-Forwarded-For $proxy_add_x_forwarded_for; }}Copy the code

Change localhost to its own service name

Six, start,

#Start the docker
docker-compose up
#Restart Docker after the code is updated
docker-compose up --force-recreate --build -d
Copy the code

Docker common command

#View the status of existing containers
docker ps -a
#Enter the container content to view logsDocker exec it id /bin/bash #idCopy the code