Skip to content

singhrohit487/deploy-Node.js-into-Docker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dockerizing a Node.js web app

The goal of this example is to show you how to get a Node.js application into a Docker container which is only intended for development, and not for a production deployment. We will create a simple web application in Node.js, then we will build a Docker image for that application, and lastly we will instantiate a container from that image.

Create the Node.js app

First, create a new directory where all the files would live. In this directory create a package.json file that describes your app and its dependencies:

image

With your new package.json file, run npm install. If you are using npm version 5 or later, this will generate a package-lock.json file which will be copied to your Docker image.

Then, create a server.js file that defines a web app using the Express.js framework:

image

In the next steps, we'll look at how you can run this app inside a Docker container using the official Docker image. First, you'll need to build a Docker image of your app.

Creating a Dockerfile

Create a file called Dockerfile to build the docker image. Your Dockerfile should now look like this:

image

.dockerignore file

Create a .dockerignore file in the same directory as your Dockerfile with following content:

image

This will prevent your local modules and debug logs from being copied onto your Docker image and possibly overwriting modules installed within your image.

Building your image

Go to the directory that has your Dockerfile and run the following command to build the Docker image. The -t flag lets you tag your image so it's easier to find later using the docker images command:

$ docker build -t /node-web-app .

Run the image

Running your image with -d runs the container in detached mode, leaving the container running in the background. The -p flag redirects a public port to a private port inside the container. Run the image you previously built:

$ docker run -p 49160:8080 -d /node-web-app

Get container ID

$ docker ps

Print app output

$ docker logs

Example

Running on http://localhost:8080

Test

To test your app, get the port of your app that Docker mapped:

$ docker ps

In the example above, Docker mapped the 8080 port inside of the container to the port 49160 on your machine.

Now you can call your app using curl (install if needed via: sudo apt-get install curl):

image

I hope this helped you get up and running a simple Node.js application on Docker.

About

This repo is about how to deploy Node.js application into a Docker container.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published