Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?

Getting started with Docker

Deploying a simple Node web-application inside a Docker container.

First we need to install Docker on the system. Docker provide great documentation to do that. https://docs.docker.com/install/

Setting up the application folder

# Linux Demo
# Check Docker is installed in the system
sudo docker --version

# Create a new folder 
mkdir node_application
cd node_application

# Create the node application
# Folder Tree
.
└───app.js
└───Dockerfile
└───package.json
└───package-lock.json
└───public
|   └───css
|       └───main.css
└───views
    └───index.pug

Dockerfile content

Dockerfile
# use node long term edition
FROM node:carbon

# app dir:: docker storing dir
WORKDIR /home/<user>/appname

# install app dependencies using package.json::
# These file also need to be included in the package
# COPY package.json and package-lock.json
COPY package*.json ./

# Application command
RUN npm install

# Bundle the application
COPY . .

# Open port 3000 :: This is the node server port
EXPOSE 3000

# Commands to start the node server
CMD ["npm", "start"]

Content of the node server: app.js

var express = require('express');
var path = require('path');

//sample appication

var app = express();

//view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug')

//public folder
app.use(express.static(path.join(__dirname, 'public')));

app.get('/', (req, res) => {
    res.render('index')
});

app.listen(3000, '0.0.0.0')
console.log("App running on port 3000");

Content of package.json

{
  "name": "dockerdemo",
  "version": "1.0.0",
  "description": "test app for docker",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js"
  },
  "author": "<Author>",
  "license": "ISC",
  "dependencies": {
    "express": "^4.16.2",
    "pug": "^2.0.0"
  }
}

Build and run the application inside a docker container

# Build the docker image
sudo docker build -t <image name> .

# Run the docker image
sudo docker run -p 49160:3000 -d <image name>

"""
Navigate to localhost:49160 to see node application running
from the container
"""

Stats and logs

# Stats and logs about the container 
sudo docker ps
sudo docker logs <container id>

# List all docker images in the system
sudo docker image ls 

# List all running docker images in the system
sudo docker container ls --all
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.