Dockerize the Express Server API

We've containerized the angular app, we are now two steps away from our complete set up.

Containerizing an express app should now be straight forward. Create a directory in themean-dockerdirectory calledexpress-server.

$ mkdir express-server

Add the followingpackage.jsonfile inside the app.

mean-docker/express-server/package.json

{
  "name": "express-server",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node server.js"
  },
  "dependencies": {
    "body-parser": "~1.15.2",
    "express": "~4.14.0"
  }
}

Then, we'll create a simple express app inside it. Create a file

server.js

$ cd express-serve
 $ touch server.js
 $ mkdir routes && cd routes
 $ touch api.js

mean-docker/express-server/server.js

// Get dependencies
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');

// Get our API routes
const api = require('./routes/api');

const app = express();

// Parsers for POST data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// Set our api routes
app.use('/', api);

/**
 * Get port from environment and store in Express.
 */
const port = process.env.PORT || '3000';
app.set('port', port);

/**
 * Create HTTP server.
 */
const server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */
server.listen(port, () => console.log(`API running on localhost:${port}`));

mean_docker/express-server/routes/api.js

const express = require('express');
const router = express.Router();

/* GET api listing. */
router.get('/', (req, res) => {
    res.send('api works');
});

module.exports = router;

This is a simple express app, install the dependencies and start the app.

$ npm install
$ npm start

Going tolocalhost:3000in your browser should serve the app.

To run this app inside a docker container, we'll also create a Dockerfile for it. It should be pretty similar to what we already have for the angular-client.

mean-docker/express-server/Dockerfile

# Create image based on the official Node 6 image from the dockerhub
FROM node:6

# Create a directory where our app will be placed
RUN mkdir -p /usr/src/app

# Change directory so that our commands run inside this new directory
WORKDIR /usr/src/app

# Copy dependency definitions
COPY package.json /usr/src/app

# Install dependecies
RUN npm config set registry https://registry.npm.taobao.org && npm install

# Get all the code needed to run the app
COPY . /usr/src/app

# Expose the port the app runs in
EXPOSE 3000

# Serve the app
CMD ["npm", "start"]

跟angular-client Dockerfile很像, 除了输出端口.

添加.dockerignore来忽略不需要的文件。

mean-docker/express-server/.dockerignore

node_modules/

现在可以构建镜像然后并运行基于镜像的容器了。

$ docker build -t express-server:dev .
$ docker run -d --name express-server -p 3000:3000 express-server:dev

在浏览器中访问localhost:3000可以看到服务api.

一旦完成, 可以停止容器如下

$ docker stop express-server

results matching ""

    No results matching ""