NodeJS app _ node. js developed based on Docker

Source: Internet
Author: User
This is the first of the two articles. This article covers a detailed tutorial on replacing Vagrant with Docker when developing a Node application using the Express framework. The application will use connect-redis middleware to persistently store session information in Redis. the second article will introduce how to productize the development settings. for this Node Application

This application contains a package. json, server. js, and A. gitignore file, which can be easily written.

. Gitignore

node_modules/*

Package. json

{ "name": "docker-dev", "version": "0.1.0", "description": "Docker Dev", "dependencies": {  "connect-redis": "~1.4.5",  "express": "~3.3.3",  "hiredis": "~0.1.15",  "redis": "~0.8.4" }}

Server. js

var express = require('express'), app = express(), redis = require('redis'), RedisStore = require('connect-redis')(express), server = require('http').createServer(app);app.configure(function() { app.use(express.cookieParser('keyboard-cat')); app.use(express.session({  store: new RedisStore({   host: process.env.REDIS_HOST || 'localhost',   port: process.env.REDIS_PORT || 6379,   db: process.env.REDIS_DB || 0  }),  cookie: {   expires: false,   maxAge: 30 * 24 * 60 * 60 * 1000  } }));});app.get('/', function(req, res) { res.json({ status: "ok" });});var port = process.env.HTTP_PORT || 3000;server.listen(port);console.log('Listening on port ' + port);

Server. js pulls all dependencies and starts a specific application. this specific application is set to store session information in Redis and expose a request endpoint. It will return a JSON status message in response. this is a very standard thing.

One thing to note is that the connection information for Redis can be rewritten using environment variables-this will play a role in migrating from the development environment dev to the production environment prod later.

Docker file

For the purpose of development, we will allow Redis and Node to run in the same container. Therefore, we will use a Dockerfile to configure this container.

Dockerfile

FROM dockerfile/ubuntuMAINTAINER Abhinav Ajgaonkar # Install RedisRUN  \ apt-get -y -qq install python redis-server# Install NodeRUN  \ cd /opt && \ wget http://nodejs.org/dist/v0.10.28/node-v0.10.28-linux-x64.tar.gz && \ tar -xzf node-v0.10.28-linux-x64.tar.gz && \ mv node-v0.10.28-linux-x64 node && \ cd /usr/local/bin && \ ln -s /opt/node/bin/* . && \ rm -f /opt/node-v0.10.28-linux-x64.tar.gz# Set the working directoryWORKDIR  /srcCMD ["/bin/bash"]

Let's take a line to understand,

FROM dockerfile/ubuntu
This tells docker to use the dockerfile/ubuntu image provided by Docker Inc. As the benchmark image for building.

RUN \
Apt-get-y-qq install python redis-server
The benchmark image does not contain anything at all-so we need to use apt-get to get everything the application needs to run. this statement will install python and redis-server. redis server is required, because we will store session information in it, and the necessity of python is that npm can be built as the C extension required by the Redis node module.

RUN \ cd /opt && \ wget http://nodejs.org/dist/v0.10.28/node-v0.10.28-linux-x64.tar.gz && \ tar -xzf node-v0.10.28-linux-x64.tar.gz && \ mv node-v0.10.28-linux-x64 node && \ cd /usr/local/bin && \ ln -s /opt/node/bin/* . && \ rm -f /opt/node-v0.10.28-linux-x64.tar.gz

This will download and extract the 64-bit NodeJS binary file.

WORKDIR /src

This statement tells docker to do cd/src once the container has been started and before executing the things specified by the CMD attribute.

CMD ["/bin/bash"]

As the last step, run/bin/bash.

Build and run containers

Now that the docker file is ready, let's build a Docker image.

docker build -t sqldump/docker-dev:0.1 .

Once the image is built, we can run a container using the following statement:

docker run -i -t --rm \      -p 3000:3000 \      -v `pwd`:/src \      sqldump/docker-dev:0.1

Let's take a look at what happened in the docker running command.

-I will start the container in interactive mode (contrast-d is in separation mode). This means that the container will exit once the interaction session ends.

-T will allocate a pseudo-tty.

-- Rm will remove the container and its file system when exiting.

-P 3000: 3000 forwards port 3000 on the host to port 3000 on the container.

-V 'pwd':/src
This statement will mount the current working directory to/src in the host (for example, our project file) container. instead of using the ADD command in Dockerfile, we can immediately see any modifications made in the text editor in the container.

Sqldump/docker-dev: 0.1 is the name and version of The docker image to be run. This is the same as the name and version we used to build the docker image.

Because Dockerfile specifies CMD ["/bin/bash"], once the container is started, we will log on to a bash shell environment. if docker runs the command successfully, it will be as follows:

Start Development

Now the container is running. before writing code, we need to sort out some standard, non-docker-related things. first, use the following statement to start the redis server in the container:

service redis-server start

Then, install the project dependency and nodemon. Nodemon, observe the changes in the project file, and restart the server in due time.

npm installnpm install -g nodemon

Finally, run the following command to start the server:

nodemon server.js

Now, if you navigate to http: // localhost: 3000 in your browser, you will see something like the following:

Let's add another endpoint like Server. js to simulate the development process:

app.get('/hello/:name', function(req, res) { res.json({  hello: req.params.name });});

You will see that nodemon has detected your modifications and restarted the server:

Now, if you navigate the browser to http: // localhost: 3000/hello/world, you will see the following response:

Production Environment

Containers in the current status are far from being released as products. redis data will not be persistent when it is restarted across containers. For example, if you restart the container, all session data will be wiped out. the same thing happens when you destroy the container and start a new container. Obviously this is not what you want. I will discuss this issue in the second part of the productization content.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.