Language and Framework Guides
Run a NodeJS Container App
8min
docker is a technology that allows you to package and run applications in containers back4app containers are based on docker technology and use docker containers to isolate and run the applications this provides several benefits, such as improved performance, enhanced security, and simplified deployment and scaling to run a node js application on back4app containers, you need to create a dockerfile that contains the instructions for building the docker image here is a step by step guide on how to create a dockerfile for a node js application at anytime if you want to check a sample working nodejs project on back4app containers go to https //github com/templates back4app/containers python flask sample 1 choose a base image the first step is to choose a base image for your dockerfile for node js applications, you can use an official node js image from the docker hub you can choose a specific version of node js by specifying the tag in the image name for example, to use node js version 14, the first line of your dockerfile would be from node 14 2 set the working directory next, you need to set the working directory in the container where the application files will be located you can use the workdir instruction to set the working directory for example workdir /app 3 copy the application files now, you need to copy the application files into the working directory you can use the copy instruction to copy the files for example copy /app 4 install dependencies in order to run the application, you need to install its dependencies you can use the npm install command to install the dependencies you can run the command in the dockerfile using the run instruction for example run npm install 5 set environment variables if your application requires environment variables to be set, you can set them in the dockerfile using the env instruction for example env port 8080 6 expose the port to make your application accessible from outside the container, you need to expose the port on which it is listening you can use the expose instruction to expose the port for example expose 8080 7 define the entry point the last step is to define the entry point for the container, which is the command that will be run when the container starts for node js applications, you can use the npm start command as the entry point for example cmd \["npm", "start"] here is the complete dockerfile for a simple node js application bashcopy code from node 14 workdir /app copy /app run npm install env port 8080 expose 8080 cmd \["npm", "start"]#sample dockerfile for nodejs apps from node 16 env node env=production workdir /app copy \["package json", "package lock json ", " /"] run npm install production copy expose 8080 cmd \[ "node", "index js" ] once you have created the dockerfile, you need to paste it on your projet root or on any folder(just specify that on the root parameter on app settings) and create or re deploy your application back4app will create a docker image based on this file and a container using this image this way, you can run your node js application in a bak4app containers