This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.

Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

preface

  • Online music poke me ah!
  • Music blog source code online!
  • In the last two months, I did not write an article for a period of time, not because I was lazy, but because I had been working overtime for the project for a week. The project manager came off work early and said that the version could not be postponed.
  • To do a thing, the most sad is that you have worked very hard, without even a little complaint, just want to hear others praise to you, the heart may be enough, the most afraid that people will not appreciate, but come to say you this is not good, that is not good.
  • Of course, I will spend some time studying in the evening. After all, time is like milk.
  • The server I used to work in Shunde was Win, so MY server was always Win. When I changed my job to Guangzhou, the server was Linx, but I found that Linx was good. Now, the space is a hard problem for me, so I decided to migrate to Linx system.
  • The following step by step, take you into the whole process | Node (including the deployment of combat).

It’s a little green on your head. Here, uncle, I’ll cut it for free.

demand

At present, music blog is deployed on Win7, and the main applications are:

  • The back-end have
    • Java As a third-party application, a Java image must be installed (👌🏻).
    • Install Node image (✔️)
  • The database is
    • If Java connects to Oracle, you need to install an Oracle image
    • Install Mysql image (👌🏻)
  • Front-end projects deployed on Nginx require an Nginx image (👌🏻)
  • The lyrics service is also deployed on Node
  • Music files are stored on Minio

Need to migrate the above application to Linx, damn!

A lot of companies don’t let you touch the server, so now you can look at it and follow me and record the whole process from nothing to nothing on Linux. This article is mainly written to still use Win server, ready to migrate Linux friends, you can practice migrating server to Linux, Linux has many advantages, the most visible is space, fast speed… In the process of migration, there will be a lot of pits waiting for you to solve, the demand above will write, this article mainly explains Docker, Linux deployment Node, other applications continue to update, please look forward to!

Docker deploys NodeJs applications

Music blog backend to Node as the main background, Node image needs to be installed.

1.1 Pulling a Node Image

Let’s take a small Node example and start it on Docker.

Start by pulling nodeJS images from domestic mirroring sites.

docker pull hub.c.163.com/nce2/nodejs:0.12.2
Copy the code

After downloading it, check out our image and find his name, which we’ll use later.

1.2 Creating a Node program

Write the simplest Node small example based on express framework, return Hello Word. Note that we are listening on port 8888.

Create package.json and write the relevant information and dependencies.

{
  "name": "webtest"."version": "1.0.0"."description": "Node.js on Docker"."author": "lpxxn"."main": "server.js"."scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "express": "^ 4.13.3"}}Copy the code

Create a server. Js

'use strict';

var express = require('express');

var PORT = 8888;

var app = express();
app.get('/'.function (req, res) {
  res.send('Hello world\n');
});

app.listen(PORT);
console.log('Running on http://localhost:' + PORT);
Copy the code

1.3 create Dockerfile

We run the program on Docker, it is inseparable from the Dockerfile protagonist, when building the image, Docker is executing the Dockerfile file, this file is necessary to create the image file.

FROM hub.c163..com/nce2/nodejs:0.122.# Create app directory RUN mkdir -p /home/Service WORKDIR /home/Service # Bundle app source COPY . /home/Service RUN npm  install EXPOSE8888
CMD [ "npm"."start" ]
Copy the code

Parameters that

  • The FROM hub.c.163.com/nce2/nodejs:0.12.2:

    • The FROM mirror is to build the basis of source Image, this is the name of the mirror hub.c.163.com/nce2/nodejs:0.12.2, namely we started FROM domestic pull down the Image on the server. If there is no local Docker will pull the image itself.

    • So 2.1 pull Node image step, we can steal 😁.

  • RUN mkdir -p /home/service:

    • RUN can be used to RUN commands, such as RUN NPM I and RUN NPM -v.

    • RUN is used to create a folder in the Image that will be used to hold our code in the future.

    • The new folder here says it was created inside Docker, not Linux.

  • WORKDIR/home/Service:

    • WORKDIR is the folder we created as the working directory.
  • COPY the/home/Service:

    • COPY is to COPY all the files in the current directory of the local computer to the /home/service folder of Image.

    • Copy the Linux Node applets to the Docker /home/service folder.

  • The RUN NPM install:

    • RUN uses NPM to install all the dependencies our app needs.
  • EXPOSE 8888:

    • Since our Web app listens on port 8888, we expose this port to the host so THAT I can access the Web externally.
  • CMD [” NPM “, “start”] :

    • Run the NPM start command, which will run Node service.js to start our Web app.

1.4 build Image

The Node image has been installed, the Dockerfile has been created, and the Node applet has been created. The next step is to use the Dockerfile to package the Node applet into the Docker.

Build an Image by running the following command in the directory where your Dockerfile resides.

docker build -t mynodeapp .
Copy the code

Don’t forget that last point.

Take a look at our image after you build it.

RUN NPM I dependencies in Dockerfile

Not in Linux, but packaged in a Docker image.

1.5 Running An Image

Now that the application has been packaged, run the package.

Docker run -d p 8888:8888 myNodeApp # docker run -d p 8888:8888 ac5 # docker run -d p 8888:8888 ac5 #Copy the code

Parameters that

  • -d indicates that the container is running in the background.

  • -p indicates port mapping.

  • Map port 8888 of the local device to port 8888 of the container, so that the extranet can access our Web through port 8888 of the local device. (By local I mean Linux)

  • Ac5 is the ID of our Image, because the first three can already locate the Image, so I did not write the following ones.

In other words: when docker run starts the image, we can write the name of the image, or write the first three id of the image.

Check the ID of the Container we just ran with docker ps.

Note: here the id is the container ID (image run ID), and the above mentioned AC5 is the image ID (application package image ID).

1.6 Viewing Container Logs

Printing log 7370 is our Container ID. Like Image ID, you can write all the Container ID. Normally, we write the first four characters, which is enough to identify the Container.

docker ps 7350
Copy the code

If you think of a Container, you can execute the following commands (inside the Container), and once inside you can operate just like normal Linux. To exit, run the exit command.

1.7 test

Let’s use curl to see if we can access our web.

curl -i localhost:8888
Copy the code

Impassability. After the Docker app starts, it is not accessible.

Do something with baidu.

curl -i www.baidu.com
Copy the code

Verification result: Access to the external network is ok, indicating that the network is ok, but the internal service port is unavailable.

The first problem comes: lack of access, caused by. Add sudo administrator privileges.

sudo docker run -d -p 8888:8888 ac5
Copy the code

This is a classic Linux problem, no permissions.

Let’s curl up again.

The port is through.

To verify whether a port can be accessed, run the curl command.

Second,Online musicBackground Node deployment history

Speaking of Docker installation using Node, I also incidentally posted in the actual combat process, my online music background Node deployment encountered the error also share. (Linux white)

2.1 NPM I error Occurs

An error occurred while executing the Dockerfile file to NPM I while running the Node project in Docker.

Try several methods, at that time can not solve the problem, simply Node service code with nodules_modules folder also thrown in, outside has been installed, our Docker can directly start.

This is also a solution, of course, it is certainly not good, so we first run the project first, first get through, and then optimize to solve the problem.

To use NPM version 7.13.0, we add RUN NPM I [email protected] to the Dockerfile.

Note that Docker is installed in the upgrade oh ~

2.2 Dockerfile files should be stored in the same directory as the project

Now we have upgraded the NPM, no error is reported, but no information is displayed, the most afraid is this kind of operation does not report error message.

I guess it’s the Node version.

Change the imported Node version FROM 10 to FROM Node :latest in the Dockerfile file.

Because DOCker PS is a container that runs successfully, it will be displayed in this container. It was mentioned in the 2.5 for friends who still use Win server.

One thing to note is that Dockerfile is best placed in the same directory as your project. Dockerfile is initially placed in a different location, causing problems with the path.

Summary: both NPM and Node versions use new points, and there may be conflicts with previous versions. Dockerfile is best placed in the same directory as your project. Initially, Dockerfile is placed in a different location, causing problems with the path

2.3 Nodemon: Permission Denied

Error: Nodemon: Permission Denied

2.2Dockerfile is not installed in the same directory as the Node version. It is best to update the Dockerfile to the latest version.

The real reason is not the Node version, but the Dockerfile file is not in the same directory as the project, so it runs with no information.

Change the Node version back to: FROM Node :10.22.0, consistent with local development.

NodeJs applications are deployed on Linux

When the above 1.7 test port failed, I silently did another thing. I ran the Node applet on Linux to see if I could access it and verify whether it was a Docker problem.

There are two ways to install Node on Linux:

  • Use the pre-downloaded package, put it in Linux, unpack it and install it.

  • Download the package via wget.

3.1 Install the downloaded package in Linux and decompress it

3.1.1 Download files matching your system from the official website

Node

Node中文网

I run the uname -a command to check that the bit of my Linux system is 64-bit (note: x86_64 indicates a 64-bit system, and i686 i386 indicates a 32-bit system).

Download the file in the red box, version v6.10.0

3.1.2 Upload and decompress the downloaded tar file to make it global by establishing a soft connection

What is a soft connection?

The upload server can be any path of its own. At present, my path is CD /app/software/

I will change the name of the decompressed file to nodejs. This place is optional, as long as it is written correctly when establishing the soft connection.

#1.Decompress tar -xvf node-v610.. 0-linux-x64.tar.xz

#2.Change the name mv node-v610.. 0-linux-x64  nodejs
Copy the code

Check whether there are node and NPM files in the bin directory under nodejs. If so, perform soft connection. If not, perform the above steps.

Establish a soft connection and become global:

ln -s /app/software/nodejs/bin/npm /usr/local/bin/

ln -s /app/software/nodejs/bin/node /usr/local/bin/
Copy the code

The last step is to verify that nodeJS has become global.

The node -v command displays the NodeJS version on the Linux command line.

3.2 Download the package through wGET

3.2.1 install wget

yum install -y wget
Copy the code

3.2.2 Downloading the latest Nodejs bin package

Wget HTTP: / / https://nodejs.org/dist/v9.3.0/node-v9.3.0-linux-x64.tar.xzCopy the code

Alternatively, you can download the latest bin package on any system you like and upload it to CentOS via FTP.

3.2.3 unzip the package

Bag type:

  • tar

  • tar.gz

For tar packages, perform the following operations in sequence:

Xz -d node-v9.3.0-linux-x64.tar.xz tar -xf node-v9.3.0-linux-x64.tarCopy the code

If the package type is tar.gz, decompress the package and run the following command:

The tar - ZXVF node - v0.10.24. Tar. GzCopy the code

3.2.4 Deploying the bin file

* /node-v9.3.0-linux-x64/bin * /node-v9.3.0-linux-x64/bin Perform the operations in sequence after confirmation.

Ln -s ~/node-v9.3.0- Linux -x64/bin/node /usr/bin/node ln -s ~/node-v9.3.0- Linux -x64/bin/npm /usr/bin/npmCopy the code

Note that the ln command used to create associations (similar to Windows shortcuts) must be given to the full path, otherwise association errors may occur.

3.2.5 testing

node -v

If the version number is displayed correctly, the deployment succeeds.

-bash: /usr/local/bin/node: Permission denied

After checking, the problem said that there was no permission, so delete the Node environment, reinstall the lower version of node, decompress in Linux, do not decompress in Win, there are several files decompression failed after win decompression. (pit)

Then configure it globally.

ln -s /app/software/nodejs/bin/npm /usr/local/bin/

ln -s /app/software/nodejs/bin/node /usr/local/bin/
Copy the code

Uninstall Node.js in Linux

First uninstall the NPM

sudo npm uninstall npm -g
Copy the code

To uninstall the node

Yum remove nodejs NPM -y delete all nodes and node_modules in /usr/local/lib. Delete all nodes and node_modules in /usr/local/include Go to /usr/local/bin to delete the node executable fileCopy the code

Linux: -bash: unzip: command not found

An error occurs when the. Zip file is decompressed in Linux. Procedure

-bash: unzip: command not found
Copy the code

Solutions:

yum install unzip zip
Copy the code

3.3 Establishing a Soft Connection May Fail

Delete node and NPM in /usr/lcoal/bin if any of the following problems occur while establishing a soft connection.

3.4 Installing NPM and CNPM on Linux

Installation of the node.

Wget HTTP: / / https://nodejs.org/dist/v10.15.0/node-v10.15.0-linux-x64.tar.xzCopy the code

Decompression.

The tar xf - node - v10.15.0 - Linux - x64. Tar. XzCopy the code

Configure global variables.

# vim/etc/profile PATH = $PATH: / usr/local/node/node - v10.15.0 - Linux - x64 / binCopy the code

Update the configuration file.

source /etc/profile
Copy the code

Install CNPM.

npm install -g cnpm --registry=https://registry.npm.taobao.org
Copy the code

The last

We have completed one of the requirements: Node as the main background and need to install the Node image.

Node is the background of our JavaScript, must rise, my friends!

We love project-driven learning, don’t we?

I, too, use my personal project – online music to drive me to learn Linux system, Docker, Java, Nginx and other front-end knowledge that may not be accessible. I think this may be the “breadth” of management.

See you soon for Oracle

Related literature

Docker practice – Deploying Nodejs applications

The browser cannot access the Docker app after it starts

The easiest step to install Nodejs on a Linux system

A universal way to install Nodejs on Linux

“Permission Denied” in Node

Specifying the Node version

Linux: -bash: unzip: command not found

Install and uninstall Node.js in Linux

Linux – Install NPM and CNPM

In the past to recommend

Typora drag image generated online | Gitee figure bed

Multi-picture detailed explanation, one time to understand the prototype chain (ten thousand words)

Vue-cli3 builds the component library

Vue implements dynamic routing (and the interviewer blows project highlights)

Axios manipulation in a project you didn’t know about (Core principles of handwriting, compatibility)

VuePress builds project component documentation

Vue-typescript-admin-template background management system

The original link

Juejin. Cn/post / 702392…