Docker quickly deploys a Node App

⭐️ more front-end technology and knowledge, search subscription number JS bacteria subscription

Using Docker to rapidly deploy Node App requires the following steps:

  • Install Docker and VSCode Docker plug-in (easy to operate Docker)
  • Creating a Node application
  • Create a Dockerfile and package the application as an image
  • Instantiate the image to create a Container

First, install Docker

Docker installation see the official documentation, the documentation is very detailed docs.docker.com/docker-for-…

Then install the Docker VSC plug-in, install this plug-in only for the convenience of Docker operation.

After the installation, open the Docker service and run the following command to check whether the installation is complete:

$docker -v Docker version 18.09.2, build 6247962Copy the code

Finally, use the domestic mirror source:

https://registry.docker-cn.com

Create a node App

Our goal is to expose a port in the Nginx Container where our Packaged Node App can be accessed externally.

npm init
npm install -S express
Copy the code

After installation, create a simple Node App:

// app.js
const express = require('express')
const url = require('url')
const execa = require('execa') 

const app = express()

app.use((req, res, next) = > {
    console.log(url.resolve(req.url))
    res.end('hello world')
})

app.listen(8888, () = > {console.log('listening at port: 8888')})Copy the code

Create Dockerfile

Then VSC calls > DockerFile to create a new Dockerfile through the Docker plug-in and select Node from the options

The plugin automatically creates Dockerfile, docker-composing. Yml, docker-composing. Debug. yml,.dockerignore in the current directory

The directory structure is as follows:

.├ ─ Dockerfile ├─ app.js ├─ Docker-come.├.yml ├─ Docker-come.yml ├─ package.├.json ├─ yarn.lockCopy the code

In Docker image, we must package only the necessary files, such as.dockerignore. gitignore files can not be packaged.

The Docker plugin automatically creates the.dockerignore configuration file for us:

node_modules
npm-debug.log
Dockerfile*
docker-compose*
.dockerignore
.git
.gitignore
.env
*/bin
*/obj
README.md
LICENSE
.vscode
Copy the code

Finally, let’s edit the Dockerfile

FROM node
ENV NODE_ENV production
WORKDIR /usr/src/app
COPY ["package.json"."package-lock.json*"."npm-shrinkwrap.json*".". /"]
RUNnpm install --production --silent && mv node_modules .. /COPY.EXPOSE 8888
CMD node app.js
Copy the code
  • FROM Specifies the base image
  • ENV Configures environment variables
  • WORKDIR Specifies the working directory
  • COPY COPY configuration files such as package.json to the working directory
  • RUN Runs the command to install dependencies
  • COPY COPY the project file app.js to the specified working directory
  • EXPOSE Exposes port 8888
  • CMD executes the command to start the project

The difference between the RUN and CMD stackoverflow.com/questions/3 saw this article…

Packaged into image

Now that Dockerfile and project files are created, we need to use Docker to package the project into iAMge:

>dockerbuild

Enter the specified image alias:

Or use the following docker build command

Docker build-t essearch/ es-elasticSearch :1.7.6 --build-arg number_of_shards=5 --build-arg number_of_replicas=2 --no-cache .Copy the code

Finally create the container

Finally, a container is created to access the application through exposed ports

>dockerrun

Or execute docker run:

$ docker run --rm -d -p 8888:8888/tcp test:latest
265575b6dc2866a6fa778ee74ad71d823554e1d96f577b31034ba544177204cb
Copy the code

Using curl to verify:

$ curl localhost:8888
hello world%                                    
Copy the code
docker container ls Get all running Containers
docker images Get all images
Copy the code

The done 🔥

Please pay attention to my subscription number, push technical articles about JS irregularly, only talk about technology not gossip 😊