Based on Docker development Nodejs application _node.js

Source: Internet
Author: User
Tags redis install node docker run install redis redis server

About this Node application

This application contains a Package.json, Server.js, and a. gitignore file, which are simple enough to be available.

. 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 * * 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 will pull all the dependencies and start a specific application. This particular application is programmed to store session information in Redis and exposes a request endpoint that responds to a status message that returns a JSON. It's all very standard stuff.

One thing to note is that connection information for Redis can be overridden using environment variables--which will work later on when migrating from development environment Dev to production environment prod.

Docker file

For development purposes, we will let Redis and node run in the same container. To do this, we will use a dockerfile to configure this container.

Dockerfile

From Dockerfile/ubuntu

maintainer Abhinav Ajgaonkar <abhinav316@gmail.com>

# Install Redis
RUN  \
 apt-get-y-qq install python redis-server

# install Node
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 & amp;& \
 ln-s/opt/node/bin/*. && \
 rm-f/opt/node-v0.10.28-linux-x64.tar.gz

# Set the Worki ng directory
workdir  /src

CMD ["/bin/bash"]

We have a line to understand,

From Dockerfile/ubuntu
This tells Docker to use the Dockerfile/ubuntu mirror provided by Docker INC. As the base mirror of the build.

RUN \
apt-get-y-qq Install Python redis-server
The Datum mirror doesn't contain anything at all--so we need to use apt-get to get everything that the application needs to run. This sentence will install Python and redis-server. The Redis server is necessary because we will store session information in it, and the need for Python is to build the C extension required by NPM for 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 downloads and extracts a 64-bit Nodejs binary file.

Workdir/src

This will tell Docker once the container has been started, do a cd/src before executing what the cmd attribute specifies.

CMD ["/bin/bash"]

As a last step, run/bin/bash.

Build and run the container

Now that the Docker file is written, let's build a Docker mirror.

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

Once the mirrors have been 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's going on in the Docker Run command.

-I launches the container in interactive mode (contrast-D is in detach mode). This means that once the interactive session is finished, the container exits.

-T assigns a pseudo-tty.

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

-P 3,000:3,000 forwards Port 3000 on the host to Port 3000 on the container.

-V ' pwd ':/src
This will mount the current working directory to the/SRC inside the host (for example, our project file) container. We hang the current directory as a volume instead of using the Add command in Dockerfile, so any changes we make in the text editor can be seen immediately in the container.

sqldump/docker-dev:0.1 is the name and version of the Docker mirror to run-this is the same name and version that we used to build the Docker mirror.

As the dockerfile specifies cmd ["/bin/bash"], as soon as the container starts, we log into a bash shell environment. If Docker runs the command successfully, it will look like this:

Start developing

Now that the container is running, we'll need to sort out some standard, Docker-related things before we start writing code. First, use the following statement to start the Redis server inside the container:

Service Redis-server Start

Then, you want to install project dependencies and Nodemon. Nodemon will observe the changes in the project file and restart the server in due course.

NPM Install
npm install-g Nodemon

Finally, start the server with the following command:

Nodemon Server.js

Now, if you navigate to http://localhost:3000 in the browser, you should 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 the changes you have made and restarted the server:

And now, if you navigate the browser to Http://localhost:3000/hello/world, you will see the following response:

Production environment

The

container in its current state is far from being published as a product. The data in the Redis will not remain persisted across the container reboot, for example, if you restart the container, all session data will be destroyed. The same thing happens when you destroy the container and open a new container. Obviously it's not what you want. I will cover this issue in the second part of the product 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.