Using Docker containers you can avoid installing npm packages on your actual machine for any project.

Dockerfile:

1
2
3
4
FROM node:8.11-alpine
WORKDIR /app
COPY . /app
RUN npm install

Line 1: Use a specific version of node.

Line 2: Create and use the /app directory as the working directory.

Line 3: Copy everything on the same directory as the Dockerfile and place it within the /app directory for our image.

Line 4: Run npm install.

With the above Dockerfile we can now build our image and run our container . We have 2 possible routes. Using the Docker CLI or Docker-Compose.

Docker CLI:

The following command will build your Docker Image.

docker build --rm -t npmProject .

The following command will run your container and mount your current directory into the container working directory.

docker run --rm -it -v $(pwd):/app npmProject sh

docker-compose.yml:

Docker-Compose may be a better route because we can mount our directory but ignore the node_modules directory which can sometimes become very heavy.

1
2
3
4
5
6
7
8
9
version: "3"
services:
	my-node-service:
      container_name: repo
      build: .
      image: my-node-project
      volumes:
      	- .:/app/
        - /app/node_modules

Line 1: The version of Docker-Compose we want to use (this is relative to the syntax used)

Line 2: This is default syntax for docker-compose specifying each container needed as a service.

Line 3: Name of the “service”.

Line 4: The name of the container. By default Docker gives random names to all containers.

Line 5: Specifying where the Dockerfile we want to build our image from is.

Line 6: The name of the image we create in the process. if build was not used in the docker-compose.yaml it would try to look for an image with this name locally and then on docker-hub.

Line 7: Beginning for volume mount entries.

Line 8: Mount the current directory to the containers /app directory.

Line 9: This line is special, I am not sure why yet but this line essentially tells docker-compose ignore the mounting of the node_modules directory within the container to our actual machine.

Thats all for today. I wonder if this is a good tutorial.