What is it about Docker that appeals to us?

  • Build: Allows for the freedom to combine services to Build our applications, avoiding environmental issues between development and production, and not being limited to any platform or language

  • Ship: Manage and design the application development, test, and release lifecycle through a unified user interface

  • Run: Can quickly publish scalable, secure and reliable services on multiple platforms


Use it!

Having said that, let’s play!


0. Install Docker Tools

$ brew install docker docker-machine docker-compose
$ docker helpCopy the code
  • Docker – Open source container application engine

  • Machine – Manage Docker services in local and cloud service providers

  • Compose – Defines, combines, and runs multiple container applications

NOTE:

  • For Mac OS X users, install Virtualbox Brew Cask install Virtualbox first.

  • If you do not like the Docker CLI tool, you can also install Kitematic, which is the GUI management tool of Docker.


Create Node.js Project

$ mkdir docker-express-mongoose-redis-example && npm init $ npm i express express-session connect-redis ioredis mongoose  --save $ touch server.jsCopy the code

server.js

Copy the code

// Import modules

const express = require(‘express’)

const session = require(‘express-session’)

const ioredis = require(‘ioredis’)

const RedisStore = require(‘connect-redis’)(session)

const mongoose = require(‘mongoose’)

// Create App

const app = express()

// Redis Client

const client = ioredis.createClient(6379, process.env.REDIS_PORT_6379_TCP_ADDR)

// Compose Schema

const ComposeSchema = new mongoose.Schema({

  name:  String,

  build: String,

  ports: [String]

// Compose Model

const Compose = mongoose.model(‘Compose’, ComposeSchema) 

// Create Session

app.use(session({

  store: new RedisStore({ client }),

  secret: ‘Dream’

}))

// Routes for redis

app.get(‘/redis’, (req, res) => {

res.send(‘Redis is live! ‘)

app.get(‘/redis/set’, (req, res) => {

client.set(‘key’, ‘Redis is live! ‘);

  res.send(`It’s redis.`)

app.get(‘/redis/get’, (req, res) => {

  client.get(‘key’).then(result => {

res.send(result || ‘Nothing! ‘)

// Routes for redis

app.get(‘/mongoose’, (req, res) => {

res.send(‘Mongoose is live! ‘)

app.get(‘/mongoose/set’, (req, res) => {

  var c = new Compose({

    name: ‘docker’,

    build: ‘.’,

    ports: [‘3000:3000’]

  c.save().then(() => {

      res.send(`It’s mongoose.`);

app.get(‘/mongoose/get’, (req, res) => {

  Compose

    .find({ name: ‘docker’ })

    .then((result) => {

      res.send(result)

app.use((req, res) => {

res.send(‘Hello Docker, Express, Mongoose, Redis! ‘)

mongoose.connect(`mongodb://${process.env.MONGO_PORT_27017_TCP_ADDR}`, (err) => {

  if (err) throw err

  // Start App

  app.listen(process.env.PORT || 3000)

package.json

{" name ":" docker - express - mongoose - redis - example ", "private" : true, "version" : "1.0.0", "description" : ""," main ":" index. Js ", "SCR ipts":{"start":"node Server. Js "}, "author" : ""," license ":" ISC ", "dependencies" : {" connect - redis ":" ^ 3.0.1 ", "express" : "^ 4.13.3", "express - session" :" ^ 1.11.3 ioredis ", "" :" ^ 1.9.1, "" mongoose" : "^ 4.2.0"}}Copy the code


MachineCreate a Docker Host in VirtualBox

$docker-machine $# Create docker Host$docker-machine create -d virtualbox dev $# Start $docker-machine start dev $docker-machine env $eval"$(docker-machine env dev)"Copy the code


Compose: Definition and operation

Create for the projectDockerfile
$ cd docker-express-mongoose-redis-example
$ touch DockerfileCopy the code

Dockerfile

FROM mhart/alpine-node# FROM mhart/alpine-node:base# FROM mhart/alpine-node:base-0.10WORKDIR/SRC ADD.. # If you have native dependencies, you'll need extra toolsRUN apk add --update make gcc g++ python# If you need npm, don't use a base tagRUN npm install# If you had native dependencies you can now remove build toolsRUN apk del make gcc g++ python && \ rm -rf /tmp/* /var/cache/apk/* /root/.npm /root/.node-gyp EXPOSE 3000 CMD ["npm", "start"]Copy the code

createdocker-compose.ymlTo combine Node.js, Redis, and Mongodb services
$ cd docker-express-mongoose-redis-example
$ touch docker-compose.ymlCopy the code

Dockerfile

app:
  build:.
  volumes:-.:/src
  links:- mongo    - redis
  ports:-3000:3000redis:
  image: redis

mongo:
  image: mongoCopy the code
Docker-compose $docker-compose build $docker-compose $docker-compose $docker-compose build $ Docker-compose up $# can also be composed by 'start', $docker-compose start $# stop service $docker-compose stop $# Output log $docker-compose logsCopy the code

Test and access our services: Rocket:
$ open "http://$(docker-machine ip dev):3000"$ open "http://$(docker-machine ip dev):3000/redis"$ open "http://$(docker-machine ip dev):3000/redis/set"$ open "http://$(docker-machine ip dev):3000/redis/get"$ open "http://$(docker-machine ip dev):3000/mongoose"$ open "http://$(docker-machine ip dev):3000/mongoose/set"$ open "http://$(docker-machine ip dev):3000/mongoose/get"Copy the code


Shut down the service and relax

$ docker-compose stop
$ docker-machine stop devCopy the code

NOTE:

  • Compose’s predecessor was FIG.

  • LLDB: $docker-compose logs app

  • If the Node project is large and relies on many modules and changes frequently, you can create and start other services instead of creating containers for the Node project itself.


Other tools

  • Vargant – also an environment builder that predates Docker

Vargant is a great tool for quickly building a variety of service environments and for sharing between teams, and now there are more and more toolchains based on it for those interested.

“Diy, food and clothing” – don’t rely on environmental construction tools, build your own, time and energy enough to try, you will gain more.


The last

Docker is not only playable, it can also package and publish containers for online applications, build its own Paas(Dokku) service, and more.

Docker Compose can also have more advanced gameplay.

Enjoy!

{
  github:'@fundon',
  email:'cfddream#gmail.com',
  twitter:'@_fundon'}Copy the code


Relates

The full text after

Welcome to follow my official account [Node full stack]