I. Normal development of node project

Project directory

Partial test code

.env

MONGO_URL=mongodb://mongodb/node-docker-test
Copy the code

App.js

const express = require('express')
require('dotenv').config({ path: '.env' })

require('./db/mongo')

const app = express()

app.use('/'.(req, res, next) = > {
  res.end('-----docker node is running-------')
})

app.listen(3000.() = > {
  console.log(`server is running at port 3000`)})Copy the code

db/mongo.js

const mongoose = require('mongoose')
const chalk = require('chalk')

mongoose.set('useCreateIndex'.true)

mongoose
  .connect(process.env.MONGO_URL, {
    useNewUrlParser: true.useUnifiedTopology: true,
  })
  .then(() = > {
    console.log(chalk.cyan(`db is connect`))
  })
  .catch((error) = > {
    console.log(chalk.red(`db is error ${error}`))
  })

mongoose.connection.on('disconnected'.function () {
  console.log('Mongoose connection disconnected')
})

mongoose.connection.on('open'.function () {})

mongoose.connection.on('error'.function (error) {
  console.log(error)
  mongoose.connection.close(function () {})})module.exports = mongoose
Copy the code

Create.dockerignore

Create. Dockerignore sets the dockerignore file

node_modules
npm-debug.log
Copy the code

Create a Dockerfile at the root of the project

LABEL Maintainer ="[email protected]" # Specifies developer COPY. /app # Copies the current project file to the /app file of the container. WORKDIR /app # set the directory of the image. RUN the command below are in the working directory to RUN the RUN the ls # test WORKDIR RUN NPM install - registry=https://registry.npm.taobao.org # # ls installation depends on the RUN CMD [" NPM ", "start"] # specifies the command to be executed when the container is startedCopy the code

4. Pack the image

Clear docker images and containers

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
$ docker ps
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
Copy the code

Package images with Dockerfile

$ docker build -t node-docker-test .
Copy the code
Sending build context to Docker Daemon 33.79kB Step 1/9: FROM node:10.15.3 10.15.3: Pulling from library/node c5e155d5a1d1: Pull complete 221d80d00ae9: Pull complete 4250b3117dca: Pull complete 3b7ca19181b2: Pull complete 425d7b2a5bcc: Pull complete 69df12c70287: Pull complete ad53476a61f2: Pull complete 204bb8bac4a1: Pull complete Digest: Sha256: c5e919a89352d3ce6a883dde54a5d51dde12229c2d11088593cd1f3efefcc16e Status: Downloaded newer image for node: 10.15.3 ---> 5a401340b79f
Step 2/9 : LABEL maintainer="[email protected]"
 ---> Running in cf46f8922ad5
Removing intermediate container cf46f8922ad5
 ---> 8f71629478c5
Step 3/9 : COPY . /app
 ---> a48c555db795
Step 4/9 : WORKDIR /app
 ---> Running in b45438ed4fa2
Removing intermediate container b45438ed4fa2
 ---> 0addf993831e
Step 5/9 : RUN ls
 ---> Running in ed804a076299
Dockerfile
app.js
db
package-lock.json
package.json
Removing intermediate container ed804a076299
 ---> 5b9ef7249334
Step 6/9 : RUN npm install --registry=https://registry.npm.taobao.org
 ---> Running in d76053046c13NPM WARN [email protected] No repository field. Added 86 packages from 52 ficol3 in 4.349s Removing intermediate container d76053046c13 ---> 8a6082ec2165
Step 7/9 : RUN ls
 ---> Running in 12b4474d6b70
Dockerfile
app.js
db
node_modules
package-lock.json
package.json
Removing intermediate container 12b4474d6b70
 ---> f7028b7aadeb
Step 8/9 : EXPOSE 3000
 ---> Running in 37ebfe720a9e
Removing intermediate container 37ebfe720a9e
 ---> 12c8b2fdc2db
Step 9/9 : CMD [ "npm", "start"]
 ---> Running in 0216d6f3f580
Removing intermediate container 0216d6f3f580
 ---> c5448c344879
Successfully built c5448c344879
Successfully tagged node-docker-test:latest
Copy the code

Check whether the image is successfully packed

$ docker images
Copy the code
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
node-docker-test    latest              c5448c344879        About a minute ago   912MB
node                10.15.3             5a401340b79f        16 months ago        899MB
Copy the code

Docker pulls and runs Mongo

$ docker run -d -p 27017:27017 --name node-docker-mongodb mongo
#-d Background running
#-p 27017:27017 Specifies the port
#--name node-mongodb specifies the container name
Copy the code

6. Run the packaged Node project image

View the image after the node is packed

$ docker images
Copy the code
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
node-docker-test    latest              c5448c344879        4 minutes ago       912MB
mongo               latest              923803327a36        8 hours ago         493MB
node                10.15.3             5a401340b79f        16 months ago       899MB
Copy the code

View the Mongo container in action

$ docker ps
Copy the code
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES B68469F44a7e mongo "docker-entryPoint.s..." 53 seconds ago Up 52 seconds 0.0.0.0:27017->27017/ TCP node-docker-mongodbCopy the code

Run the Node-docker-test image

$ docker run -d -p 3333:3000 --name node-docker-service --link node-docker-mongodb:mongodb c5448c344879
#-d Background running
#-p 3333:3000 Specifies the container to expose ports externally
#--name node-docker-service Specifies the container name
#--link node-docker-mongodb:mongodb connects to the node-docker-mongodb container. Node-docker-mongodb is alias mongodb under node-docker-service
#Is that why the mongodb connection address in the '. Env 'file
#By the mongo: / / localhost: 20710 / node - docker - test
#Mongodb ://mongodb/node-docker-test

a910c52aa4c86ee4708414e6be36988dfe11ddf7c8b37ae8416f1450cc56aee2
Copy the code
$ docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES a910c52aa4c8 c5448c344879 "npm start" 28 seconds ago Up 28 seconds 0.0.0.0:3333->3000/ TCP node-docker-service b68469f44a7e mongo "docker-entrypoint.s "0.0.0.0:3333->3000/ TCP node-docker-service b68469f44a7e mongo "docker-entrypoint.s... 2 minutes ago Up 2 minutes 0.0.0.0:27017->27017/ TCP node-docker-mongodbCopy the code

See the node – docker – service logs

$ docker logs a910c52aa4c8

>[email protected] start/app
> node app.js

server is running at port 3000
db is connect
Copy the code

The test interface

$ curl localhost:3333

-----docker node is running-------
Copy the code