Install Npm Docker

/ Comments off
Table of contents
  1. Npm Install Docker Not Working
  2. Install Npm Docker For Windows
  3. Npm Install Docker Cache
  4. Install Npm Docker Download

FROM node WORKDIR /usr/src/app COPY. /usr/src/app RUN npm install CMD 'npm' 'start' Copy that to a file named Dockerfile, then build and run it.t nodejs-tutorial $ docker run -p 3000:3000 nodejs-tutorial. It’s simple, and it works. The only problem? It is full of mistakes and bad practices for building Node.js Docker images. # switch cd /mnt; # install npm install; # start npm start; # Expected Output # Listening on port 5000. (Repeat the steps for Postman to validate that the endpoints are working) Now that it’s working let’s stop it container and delete it and focus on creating a Dockerfile next the step, but first.

To install private npm packages in a Docker container, you will need to use Docker's build-time variables.

Background: runtime variables

You cannot install private npm packages in a Docker container using only runtime variables. Consider the following Dockerfile:

Which will use the RisingStack Alpine Node.JS Docker image, copy the package.json into our container, installs dependencies, copies the source files and runs the start command as specified in the package.json.

In order to install private packages, you may think that we could just add a line before we run npm install, using the ENV parameter:


However, this doesn't work as you would expect, because you want the npm install to occur when you run docker build, and in this instance, ENV variables aren't used, they are set for runtime only.

Instead of run-time variables, you must use a different way of passing environment variables to Docker, available since Docker 1.9: the ARG parameter.

Create and check in a project-specific .npmrc file

A complete example that will allow you to use --build-arg to pass in your NPM_TOKEN requires adding a .npmrc file to the project.

Use a project-specific .npmrc file with a variable for your token to securely authenticate your Docker image with npm.

  1. In the root directory of your project, create a custom .npmrc file with the following contents:

    Note: that you are specifying a literal value of ${NPM_TOKEN}. The npm cli will replace this value with the contents of the NPM_TOKEN environment variable. Do not put a token in this file.

  2. Check in the .npmrc file.

Update the Dockerfile

The Dockerfile that takes advantage of this has a few more lines in it than the earlier example that allows us to use the .npmrc file and the ARG parameter:

This adds the expected ARG NPM_TOKEN, but also copies the .npmrc file, and removes it when npm install completes.

Install Npm Docker

Build the Docker image

To build the image using the above Dockerfile and the npm authentication token, you can run the following command. Note the . at the end to give docker build the current directory as an argument.

This will build the Docker image with the current NPM_TOKEN environment variable, so you can run npm install inside your container as the current logged-in user.

Note: Even if you delete the .npmrc file, it will be kept in the commit history. To clean your secrets entirely, make sure to squash them.

Docker allows us to run our applications as containers. A container is a standalone executable package that is lightweight and has everything needed to run an application be it libraries, tools, runtime, settings, or code.

In this tutorial, we will build a Node.js application, create an image, and also build a container using the image. Enjoy!

Dockerize the Node.js Application

To start this tutorial, we need to make sure Docker and Node.js are installed on our system. If Docker is not, you can refer to the links below to download:

To dockerize a Node.js application, we need to follow these steps:

  • Create a Node.js application
  • Create a Dockerfile
  • Building your Docker Image
  • Expose

Step 1 - Creating a Node.js Application

First, we will start by creating a directory for our project and then install some dependencies for our simple Hello World website.

Install npm And Express Framework

Install npm and Express, which is a Node.js framework. Then, initialize npm in our directory.

npm creates a package.json that holds the dependencies of the app. Next, install the Express framework dependency.


Create an app.js file with an HTTP server that will set up the Hello World site:

Run The Application

The app is ready to launch:

Go to http://localhost:8080/ in your browser to view it.

Step 2 - Create A DockerFile

Npm Install Docker Not Working

Create a file in the root directory called Dockerfile.

The first thing is we need to define which image we want to build from. Here we will use version 9 of node available from Docker Hub:

Next, create the working directory for your application.

Install Npm Docker For Windows

Install the app dependencies using the npm binary.

Copy the rest of the application to the app directory.

Expose the port and start the application.

Your Dockerfile should look like this:

Npm Install Docker Cache

Install npm docker downloadNpm

NOTE: Above you will notice we used two distinct COPY commands to reduce the application rebuild time

.dockerignore file

Create a .dockerignore file so as not to copy unnecessary files to the container:

This prevents the local module and debug logs from being copied onto your Docker image.

Step 3 - Building your Docker Image

Building your Docker image is quite easy and can be done using a single command.

The -t flag lets you tag your image so it’s easier to find later

For example:


You should get something similar to the output above after executing the command. This means that the docker image was created successfully and the app is working fine.

Now that the build is complete, you can check your image:


Step 4 - Run a Container

Install Npm Docker Download

Now we can run the docker image using the command:

-d flag indicates the docker container is running in the background. The -p flag specifies which host port will be connected to the docker port.




Use the docker ps command to check the running container.


This tutorial has helped you with the basics of running a simple Node.js application using Docker and using a Dockerfile to build a Docker image. Here are a couple of other articles if you’d like more useful information on how to get started with Docker or more basics on understanding docker concepts for you to enjoy.


Peer Review Contributions by: Louise Findlay

About the author

Francisca Adekanye

Francisca is an undergraduate student pursuing a degree in Computer Engineering. Cisca loves technical writing, contributing to open source projects, and also involving herself in tech communities.