The preparatory work

  1. Package to generate the.next directory
  2. BUILD_ID is the same version as Server static

Docker-based Node application

Generate Dockerfile &.dockerignore

Touch Dockerfile// Generate a docker fileCopy the code

Dockerfile configuration

FROM node:14
# Create app directory
WORKDIR /usr/src/app
COPY package.json ./
COPY yarn.lock ./
RUN yarn install
COPY . .
EXPOSE 3000
CMD [ "yarn", "start" ]
Copy the code

. Dockerignore configuration

node_modules
*.log
Copy the code

reference

Build the mirror

-t yourUsername/node-web-app docker run -p 49160:8080 -d yourUsername/node-web-app Docker logs ID view if failure

Host is a pit, be careful

Buying a server

Aliyun chooses its own server

  1. Create an instance
  2. SSH root@public IP address // Log in to the server
  3. Ssh-copy-id root@public IP address // Upload a private key
  4. Modifying the host file
Public IP variable name // Next login is easy, directly use SSH root@ variable name is okCopy the code
  1. Create a separate user
SSH root@id// log in to adduser username// adduser su - username// switch to SSH username@id// log in to ssh-copy-id username@id as userCopy the code

6. Docker docker official website 7. User add group (solve non-root user can not run docker)

Cat /etc/group Check the group usermod -a -g docker usernameCopy the code
  1. Install Node && YARN (for packaging)
curl -fsSL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt-get install -y nodejs
sudo apt-get install gcc g++ make
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | gpg --dearmor | sudo tee /usr/share/keyrings/yarnkey.gpg >/dev/null

echo "deb [signed-by=/usr/share/keyrings/yarnkey.gpg] https://dl.yarnpkg.com/debian stable main" | sudo tee /etc/apt/sources.list.d/yarn.list

sudo apt-get update && sudo apt-get install yarn
Copy the code
  1. Internal docker-ization of the server
Touch. Env. local// Add the local file as required for the project, // Install dependency yarn // package code yarn build // create folder mkdir blog-data // Enable database docker run --network=host -v /home/xiong/blog-data:/var/lib/postgresql/data -p 5432:5432 -e POSTGRES_USER=blog -e POSTGRES_HOST_AUTH_METHOD=trust -d Postgres :12.2 YARN Typeorm :build // Package Typerom YARN Migration :run// Create a table // Generate an image docker build -t aslanxiong/node-web-app. Docker run --network=host -p 35:3000 -d aslanxiong/node-web-appCopy the code
  1. Ali cloud security policy group adds the port to be accessed

Using deployment scripts

Sh file chmod +x bin/deploy.sh // Add the executable permission SSH xiong@nextjs "sh /home/..." // Run the server script or SSH xiong@nextjs 'bash -s' < bin/deploy.sh// Run the local scriptCopy the code
  1. Use nginx
Docker run --name nginxNextjs -d nginx:1.19.1 docker run --name nginxNextjs --network=host -v /home/xiong/app/nginx.conf:/etc/nginx/conf.d/default.conf -v / home/xiong/app/next/static / : / home/xiong/nginx/HTML / _next/static / - d nginx: 1.19.1Copy the code