NodeJS
Overview
This tutorial will describe how to convert your NodeJS application to docker application for compatibility with FarmStack connector.
Dockerization Process
Create a new file named Dockerfile
in the application folder and open in your favorite text editor.
Copy this code into the Dockerfile
First we are going to define the image we are going to use. Here we are using latest alpine image of node to keep the size of NodeJS application small. You can use any image available in the Node's Dockerhub.
Next we will create a directory to copy all our application code inside the image.
Since we are using node image, node
and npm
are already installed in this image. We just need to copy our package.json
and package-lock.json
files.
Here, we copy package.json
files before copying complete project. This is done to take advantage of Docker layers caching and only install dependencies if the files have changed. You can find more information about this here.
Now we will copy your app's source code to docker image.
Our sample-nodejs
app binds to port 8081
so we will map this port to docker daemon
by using EXPOSE
command. If your app uses any other port, kindly change it
In this last step, define the command to start your application. Our sample application starts withnpm start
command. You can also use a shell script file here which executes to start your server.
This should be your final Dockerfile
You can find the final Dockerfile
here.
.dockerignore file
Create a .dockerignore
file in the same directory as your Dockerfile
. Add the following lines to the file:
Next Steps
Last updated