10 Steps to Setup and Containerize an Express Server

This article is a step by step guide on how to bootstrap and containerize an Express Server using Iron Node as a base image in 10 Easy steps. The end result of this guide is an 83 MB Image that can be used to instantiate an alpine container with express server running on port 3000.

Download POC from GITHUB

Pre-requisites

Steps

  1. Create a folder
  2. Open a terminal application on your machine (CMD, BASH, ASH, etc..) an initialize NPM on the folder
    # This script will initialize
    # an NPM project on the
    # folder you created
    npm init -y
  3. Install express-generator, it enables you to quickly create an express application skeleton.
    # This shell script will install
    # an express generator cli on your
    # machine.
    npm install express-generator -g
  4. Bootstrap the express application skeleton
    # This shell script will initialize
    # an express server app on your folder.
    express --view=pug
  5. Install express server dependencies
    # After bootstrapping the server
    # skeleton, this script will
    # install depeendencies to run
    # the server app.
    npm install
  6. Add the Dockerfile below (Name it "Dockerfile" without extension) to the root directory of your project's source code. It is basically a file that instructs the Docker engine on how to create a Docker image for your application. It uses a base image provided by iron/node which is an optimized variant of alpine node which is more smaller than the original alpine image.
    # Base image is iron/node https://github.com/iron-io/dockers/tree/master/node
    FROM iron/node
    # Folder where the app will be copied
    # will be the target of the docker file commands
    WORKDIR /app
    # Copy package.json + package-lock.json files
    COPY package*.json ./
    # Installing dependencies
    RUN npm install
    # Bundle app source
    COPY . .
    # Exposes Port 3000
    EXPOSE 3000
    # Runs Server at container start
    ENTRYPOINT [ "npm", "start" ]
  7. Add the .dockerignore file below, this will prevent node_modules folder and node logs from getting baked in your image. This will prevent issues from baking images on windows machines (express has varying dependencies on different os installations)
    node_modules
    npm-debug.log
  8. Create a docker image by executing the command below on your app folder.
    # This command will build an
    # iron/node image with your application
    # locally on your machine
    docker build --tag express/iron-api .
    view raw Build Image.sh hosted with ❤ by GitHub
  9. Verify that your docker image was built.
    # List down all images on your machine.
    docker images --all
  10. Instantiate a container from the image you created.
    # This script creates a container out
    # of the image we built earlier. It exposes
    # the application on port 3000.
    docker run -p 3000:3000 --name iron-api express/iron-api

Results

An 83 MB docker image based on iron/node that can be instantiated or pushed through docker registries like Azure Container Registry or Amazon Container Registry.

You have a running container that contains an express server which listens on port 3000.

You can navigate to http://localhost:3000 and view your express server


Related Articles


Full blood geek?

Comments

  1. Replies
    1. Hi,

      Thanks for pointing out the unclear part, I really appreciate feedback. I updated the section and to point out the specific update. You have to a add an extension-less file named "Dockerfile" to the root directory of your source code directory. This is basically a file that instructs the Docker Engine on how to build a container image.

      Thanks and I hope the update helps.
      Allan

      Delete

Post a Comment

Popular posts from this blog

Security: HTTP headers that expose web application / server vulnerabilities

API Gateway: Response Aggregation with Ocelot and ASP.net Core

Building Simple API Gateways with Ocelot and ASP.net Core

API Gateway in a Nutshell