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 GITHUBPre-requisites
- NodeJS Installed (Download Here)
- Express Generator (Installation Guide Here)
- Docker (Download Here)
Steps
- Create a folder
-
Open a terminal application on your machine (CMD, BASH, ASH, etc..) an initialize NPM on the folder
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# This script will initialize # an NPM project on the # folder you created npm init -y -
Install express-generator, it enables you to quickly create an express application skeleton.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# This shell script will install # an express generator cli on your # machine. npm install express-generator -g -
Bootstrap the express application skeleton
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# This shell script will initialize # an express server app on your folder. express --view=pug -
Install express server dependencies
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# After bootstrapping the server # skeleton, this script will # install depeendencies to run # the server app. npm install -
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# 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" ] -
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
node_modules npm-debug.log -
Create a docker image by executing the command below on your app folder.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# This command will build an # iron/node image with your application # locally on your machine docker build --tag express/iron-api . -
Verify that your docker image was built.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# List down all images on your machine. docker images --all -
Instantiate a container from the image you created.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
# 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
- Common NodeJS + Docker Issues
- Azure Container Instances (ACI)
- Microservices: Picking the .NET Framework for your containerized applications.
- Microservices: Why choose containers over virtual machines.
Please explain step 6.
ReplyDeleteHi,
DeleteThanks 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