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.
touch Dockerfile
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.
FROMnode:alpine
Next we will create a directory to copy all our application code inside the image.
# Create app directoryWORKDIR/app
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.
# Copy package.json and package-lock.json files to imageCOPYpackage*.json./RUNnpminstall
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.