The background,

A netizen needs to test the project of blockchain transaction. The test scripts need to be run in a NodeJS environment, such as server.js, package.json, and mainly rely on web3 (of course, other packages). To facilitate testing, a better solution is to use Docker to build an environment. This article experimented on Ubuntu 16.04.

Second, try to

Docker already has an official NodeJS container. The latest version is 10.5.0. Use the command directly:

docker pull node:alpine
Copy the code

Pull the mirror and set it aside. Note: To save mirror volume, use alpine version.

Create a Dockerfile with the following contents:

FROM node:alpine
 
# Create app directory
RUN mkdir -p /home/Service
WORKDIR /home/Service
 
# Bundle app source
COPY . /home/Service
RUN npm install
 
CMD [ "npm", "start" ]
Copy the code

The last line of the Dockerfile indicates that when you start the container, run the NPM start command, which will automatically find the server.js file. Other names need to be modified and will not be mentioned in this article. Server. js and package.json files are in the same directory as Dockerfile. Using the command

docker build -t mynode .
Copy the code

Build the Docker image. Note: -t myNode represents the image name after construction, which can be viewed by Docker Images.

However, the build fails, and the following message is displayed:

Step 5/6: RUN NPM install --> Running in F99a9f66b07d NPM WARN Deprecated [email protected]: Use Mz or FS -extra^3.0 with Promise Support NPM WARN deprecated [email protected]: ⚠️ WARNING ⚠️ tar.gz module has been deprecated and your application is vulnerable. Please use tar module instead: https://npmjs.com/tar npm ERR! code ENOGIT npm ERR! Error while executing: npm ERR! undefined ls-remote -h -t git://github.com/frozeman/WebSocket-Node.git npm ERR! npm ERR! undefined npm ERR! No git binary found in $PATH npm ERR! npm ERR! Failed using git. npm ERR! Please check if you have git installed and in your PATH.Copy the code

According to the previous error message, I thought there was a problem with Promise and web3 version. After checking the package.json file, the web3 version is 1.0.0-beta.34, which is the latest version. Promise is fine, and continues with the error indicating that the container doesn’t have Git (because you need to download websocket-Node with Git). Another way to think about it is to go into the Node :alpine container and call NPM start manually. Start container command:

docker run -it -v /home:/home node:alpine sh
Copy the code

-v /home:/home mounts the /home directory of the host to the /home directory of the container for testing (because js files are stored in the /home directory). The result was the same.

Change nodeJS version

The nodeJS version is 8.11.2, but dockerhub only has 8.11.3. First check whether the version is wrong and pull the image:

Docker pull node: 8.11.3 - alpineCopy the code

That’s what happened, but it still went wrong. That’s not the problem. (Note: This is only to record the process of solving the problem at that time)

Fourth, continue to change ideas

Git is not installed. Consider using an Ubuntu image that requires git installed, and then consider downloading the NodeJS dependency package from your local host. Execute in server.js directory:

npm install
Copy the code

There is still a problem with the installation process (the specific information is not recorded in time, but the node/ NodeJS version is too early).

Update node

Update command:

npm install npm@latest -g
n stable
Copy the code

Update nodeJS

View the NodeJS version:

$ nodejs -vv4.2.6
Copy the code

Uninstall nodejs

$ sudo apt remove nodejs
Copy the code

New add file: / etc/apt/sources list. D/nodesource. List, written in the documents:

deb https://deb.nodesource.com/node_6.x xenial maindeb-src https://deb.nodesource.com/node_6.x xenial main
Copy the code

Save the file. Then download the GPG key:

curl -s https://deb.nodesource.com/gpgkey/nodesource.gpg.key | sudo apt-key add -
Copy the code

Update the source:

sudo apt-get update
Copy the code

View alternative NodeJS versions:

Sudo apt-cache policy nodejsnodejs: Installed: 6.14.3-1nodesource1 Candidate: 6.14.3-1Nodesource1 Version table: * * * nodesource1 6.14.3-1, 500, 500 https://deb.nodesource.com/node_6.x xenial/main amd64 Packages, 100 / var/lib/DPKG/status 4.2.6 ~ DFSG - 1 ubuntu4. 1 500 500 http://mirrors.cloud.aliyuncs.com/ubuntu xenial updates/universe amd64 Packages 4.2.6 ~ ubuntu4 DFSG - 1 500 500 http://mirrors.cloud.aliyuncs.com/ubuntu xenial/universe amd64 PackagesCopy the code

Nodejs installation:

sudo apt-get install nodejs
Copy the code

View the NodeJS version:

Nodejs - vv6.14.3Copy the code

7. Keep trying

After updating node and nodejs, run the following command in the server.js directory of the host:

npm install
Copy the code

The installed dependency packages are stored in node_modules. Run NPM start to see normal results. The dependency packages have been installed successfully.

Pack the Docker

Since server.js depends on packages in node_modules, you can package this directory into the /home/service directory of the image, use it as the base image, and copy server.js and package.json to the /home/service directory. Docker environment can be completed. The Dockerfile content is not much different. Built Docker images are available

docker pull latelee/node
Copy the code

The pull.

The methods and ideas tried in this paper are for reference only.