Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Node.js Development Environment with Docker under Windows

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Docker to deploy Node.js development environment under Windows". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to deploy Node.js development environment with Docker under Windows".

Desired results:

1. Still edit the source code under windows, run the code in the docker container, and finally see the running result in windows's browser to facilitate subsequent debug.

2. The program I developed and the running environment can be packaged together into the image of docker, and the image can be handed over to the partner to run or published directly to the server.

Installation and startup

The core of the docker engine is the linux container that runs on the linux operating system. So to use the docker container on windows, you first need to provide a linux runtime environment.

Go to the docker official website to download the msi installer. If your system version (such as 64bit windows 10 pro, enterprise and education) supports hyper-v virtual technology, then there is no need to use additional virtual machines (virtualbox). The installer will automatically install docker (docker for windows) for you.

The hyper-v package must be enabled for docker for windows to work. The docker for windows installer will enable it for you, if needed. (this requires a reboot). If your system does not satisfy these requirements, you can install docker toolbox, which uses oracle virtual box instead of hyper-v.

If you can not use hyper-v virtual technology, the docker project team also provides docker toolbox tools, which can easily install docker in the windows environment.

For more details, refer to the official docker documentation-toolbox.

The execution system in this article is win7, so docker toolbox is used.

The installation process accompanies the installation of the oracle vm virtualbox virtual machine, as shown in the following figure:

Kitematic is the gui management tool of docker. Open docker terminal to start docker quickly.

You can see that docker assigns an ip:192.168.99.100 to default machine at this time. We can execute the docker command directly under this terminal.

Because running docker in windows adds an extra layer of virtual machines, there are several concepts to understand:

The docker host refers to the linux virtual machine, that is, the ip address of the docker host (that is, the linux virtual machine) is 192.168.99.100.

Just like a normal ssh access virtual machine, we can also log in to the docker host with ssh through this ip to execute commands. (user name: docker; password: tcuser)

If you want to execute linux commands (such as mount) with root privileges, you need to open virtualbox.

Set up image

There is no image in the newly installed docker host (docker images looks at the existing image). To run an node application, you need to build a node environment image. You can use the lightweight linux image on docker hub as the basic image (such as centos). Install node; manually or you can directly pull an image with node installed (docker pull pull image). Downloading images from docker hub may be very slow and can be downloaded with the help of domestic cloud service providers (for example).

Now suppose we pull a centos image that does not contain a node environment.

$docker run-it centos bash

Start a container and enter the container's bash for interactive operations. Install node in the same way as under centos. Exit exits the container when the installation is complete. Rest assured that if you don't add the-- rm parameter when you don't use docker rm or docker run, even if you exit the container, the container itself and its changes will not disappear. You can use docker ps-a to view all containers, and docker ps to see which containers are running.

$docker commit

Submit the previously modified container to the new image. This image is the image of the installed node environment (named nodejs).

In subsequent development, we can use docker run-it nodejs bash to start the container.

Share files between windows and virtual machine

We now need to run the source code in the docker container, and the docker container is in the docker host, so first of all, we need to make sure that the docker host (that is, the linux virtual machine) can access the source code files in windows.

Open virtualbox, click "Settings"-> "shared folder", specify the path and name, and then check "Auto-mount" and "fixed allocation". "Auto-mount" allows the virtual machine to mount the folder automatically the next time it starts, otherwise it needs to be manually mounted every time it starts.

If all goes well, restart the virtual machine and enter the mount command to see where the shared folder is mounted. If you enter the directory, you can see the files synchronized with those under windows.

If automatic mount encounters problems, cancel this option and mount manually using the following command:

Mount-t vboxsf docker_share

Run node code in a docker container

First edit the test code app.js under the shared folder of windows:

Var http = require ('http'); http.createserver (function (req, res) {res.writehead (200,{' content-type': 'text/plain'}); res.end (' hello world\ n');}) .birthday (1337); console.log ('server running at http://0.0.0.0:1337/');)

You can see this file in the docker host.

Start a container with a nodejs image. We want the container to access the files in the docker host as source code, and to access the running execution port under windows to view the results. Start the container with the following command:

$docker run-v / docker_share:/app-p 1337 purl 1337-it nodejs bash

-v host directory: the container data volume directory enables the host directory to be mounted into the container and accessible by the container.

-p host port: the container port enables the container port to be mapped to the host and can be accessed outside the container.

Note: multiple ports can be mapped by repeating multiple-p commands.

(docker version 0.11 or later can use the-net=host parameter to map all ports in the container to the docker host. Therefore, you can also use:)

$docker run-v / docker_share:/app-- net=host-it nodejs bash

Enter the corresponding data volume directory in the container and execute node app.js

Under windows, you can see the result by visiting 192.168.99.100ren 1337 through a browser.

At this point, the basic node application can run under docker.

Further development work

1. Npm install

Generally speaking, in node projects, there are node_modules dependencies that need to be installed with npm install. Under docker, this command can also be executed in the container in which the application is expected to run. But be careful to add the-- no-bin-links directive to avoid creating soft connections.

Npm install-no-bin-links

two。 Associate other services to interconnect with containers

Many node applications associate enabling services such as redis, mysql, and so on. Directly, we can open multiple terminals of the same container as under windows to run the service or execute the code. Enter a running container with the following command:

$docker exec-it bash

More elegantly, we run different services on different containers and then associate them using the interconnection of link name:alias containers.

3. Respond to code changes in real time

During the development process, it will be troublesome if you need to end the node process and restart it every time you modify the code. You can use tools to monitor code changes and automatically restart the process, so that you only need to refresh under the browser to see the new running effect.

Install nodemon:

Npm install-g nodemon

Still take app.js as an example, use the command when running app.js in the container:

Nodemon-l app.js

Note: if you are not running under the container, you can use nodemon app.js, but in the container, you need to turn on chokidar polling with the-l or-- legacy-watch parameter to listen for changes to files in the mount directory.

Try to change the app.js content, and after saving it, you can find that nodemon automatically restarts:

Refresh the browser to see the modified result:

For more detailed use of nodemon, see github-nodemon.

Package the source code and environment as image

Edit the dockerfile and .dockerboards files in the project directory. Dockerfile:

From nodejs# create app directoryrun mkdir-p / usr/src/appworkdir / usr/src/app# install app dependenciescopy package.json / usr/src/app/run npm install# bundle app sourcecopy. / usr/src/appexpose 8080cmd ["npm", "start"]

From specifies the basic image, and then lists the operation commands that need to be done based on the basic image. You can refer to building a new image environment (including copying source code and executing npm install).

.dockerignore:

Node_modulesnpm-debug.log

Ignore files in a new build image.

Use the command under the project directory in the docker host (the directory where dockerfile resides):

Docker build-t. / / Note that the point at the end can not be omitted.

You can get an image of your own build. The image is automatically added to your docker host, and you can directly start the container running code of the new image (the code is contained in the image, so there is no need to mount it to the data volume repeatedly, but it cannot be modified under windows).

Thank you for reading, the above is the content of "how to use Docker to deploy Node.js development environment under Windows". After the study of this article, I believe you have a deeper understanding of how to use Docker to deploy Node.js development environment under Windows, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report