node-project
.
package.json
file for your
application. For more information on how package.json
works, see the specifics of
npm’s package.json handling. Run
<your project name>
) and version of your application.
For now, you can simply hit RETURN to accept the defaults for most of them, with the following exception:
app.js
, or whatever you want the name of the main file to be. If you want it to be index.js
, hit RETURN to
accept the suggested default file name.
express
version is 4.19.2.app.js
as mentioned earlier for the entry point and write a simple Express app that returns
Hello World!
when navigating to the /
path of the URL.
node_modules
folder in your project), so you can start your project by running the command:
http://localhost:3500
in your browser of choice. You should see the Node.js app running and printing
Hello World!
in the document.
.dockerignore
file and add the contents that should not be copied over
to the Docker file system.
.dockerignore
file and adding folders like node_modules
is a must since dependencies will be installed
while building the image based on the platform preferences used. Copying those from the file system will overwrite the
installed dependencies and might error out during deployment.source code
and node_modules
to be present while running the application. You can opt for multi-stage
builds if your process fits that approach.
node-app
image for platform linux/amd64
and tags it as latest
.
If you are locally testing your application, you can skip the platform
key to build the images
-it
: enables interactivity with TTY.--rm
: tells the Docker Daemon to clean up the container and remove the file system after the container exits.--name node-app
: Name of the container node-app
.-e PORT=3500
: Sets the environment variable PORT in Docker to 3500
.-d
: Runs the container in detached (background) mode. You can skip the flag to see the logs directly in your
terminal window.-p 3500:3500
: Maps port 3500 on your host to port 3500 in the container.node-app
at the end is the name of the image.http://localhost:3500
to see the Node.js application running inside the Docker
container.
Hurray! Now we have created and packaged a Node app for production use.