From 8bf718b6906b7a4b5f50f97f7ecb4982334230f2 Mon Sep 17 00:00:00 2001 From: skumar064c Date: Mon, 3 Apr 2017 21:52:43 -0700 Subject: [PATCH] initial commit --- Dockerfile | 21 +++++++++++++++++++++ README.md | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 16 ++++++++++++++++ server.js | 14 ++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 Dockerfile create mode 100644 README.md create mode 100644 package.json create mode 100644 server.js diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..0319336 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +# +# dockerized node server +# + +FROM node:6.10.1 + +MAINTAINER Shiva Chandra Kumar K + +RUN mkdir -p /tmp/app + +WORKDIR /tmp/app + +ADD . /tmp/app + +RUN npm install + +ENV PORT 8000 + +EXPOSE $PORT + +CMD ["npm", "start"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..07e4b67 --- /dev/null +++ b/README.md @@ -0,0 +1,53 @@ +# docker-example +An example Docker image creation from scratch. + +### Install Docker Machine + +Follow the procedure [here](https://docs.docker.com/machine/install-machine/) to install docker-machine. Machine let's you start virtual hosts using virtual-box (in our case) and run docker on it. + +Note: you can run the following natively too. I'm using a virtual host to keep the dev env isolated. + +Get some help using : + +```sh +$ docker-machine --help +``` + +### Create a VM (dev) running boot2docker + +Let's create a machine (virtual host). Ensure virtual-box is installed and running on your local machine. + +```sh +$ docker-machine create --driver virtualbox dev + +$ docker-machine env dev (shows the new VM env configuration) + +# Copy the project's Dockerfile | package.json | server.js files into the VM. + +$ docker-machine scp -r . dev:/home/docker/ + +$ docker-machine ssh dev (to login into the new VM) + +(optional) + +$ docker-machine start dev (start the VM) + +$ docker-machine stop dev (stop the VM) + +$ docker-machine ls (show list of VMs) + +$ docker-machine kill dev (kill the VM) + +$ docker-machine rm dev (to remove the VM completely) +``` + +### Getting Started + +Once you have ssh into `dev` VM which is a boot2docker image. You should see that `docker` is pre-installed on it. You are ready to start off. + +```sh + +$ docker build -t ksck23/node . + +$ docker run -it --rm --name app ksck23/node +``` diff --git a/package.json b/package.json new file mode 100644 index 0000000..6162cbf --- /dev/null +++ b/package.json @@ -0,0 +1,16 @@ +{ + "name": "node-docker-example", + "version": "1.0.0", + "description": "Running example node.js server using docker", + "main": "server.js", + "scripts": { + "start": "node server.js", + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [ + "node", + "docker" + ], + "author": "Shiva Chandra Kumar K ", + "license": "MIT" +} diff --git a/server.js b/server.js new file mode 100644 index 0000000..7c0eb1e --- /dev/null +++ b/server.js @@ -0,0 +1,14 @@ +const http = require('http'); + +const hostname = '0.0.0.0'; +const port = process.env.PORT || 8000; + +const server = http.createServer((req, res) => { + res.statusCode = 200; + res.setHeader('Content-Type', 'text/plain'); + res.end('Hello World\n'); +}); + +server.listen(port, hostname, () => { + console.log(`Server running at http://${hostname}:${port}/`); +});