Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create Docker's own node_modules

Tags:

docker

devops

I am new to Docker. I have a project set up something like this:

app/
dist/
node_modules/       
package.json
Dockerfile           
docker-compose.yml  
.dockerignore        

Dockerfile

FROM node:10.16.2

WORKDIR /app

RUN sed -i '/jessie-updates/d' /etc/apt/sources.list

RUN apt-get update
RUN rm -rf /var/lib/apt/lists/*
RUN npm install --quiet
RUN npm install -g gulp

EXPOSE 3000

docker-compose.yml

version: '2'

services:
  web:
    build: .
    command: gulp
    ports:
      - "3000:3000"
    volumes:
      - .:/app
  package:
    build: .
    command: ./package.sh
    volumes:
      - ./package:/app/package

.dockerignore

# add git-ignore syntax here of things you don't want copied into docker image

.git
*Dockerfile*
*docker-compose*
package-lock.json
node_modules/

When I run docker-compose build --no-cache web && docker-compose up web, it is actually not creating it's own node_modules but using the local system's. I want docker's own node_modules when I run docker. Please help.

I do understand, I am copying all files from local to docker setup, but I want to exclude node_modules and also make it install its own node_modules.

like image 755
Mr_Green Avatar asked Nov 06 '22 14:11

Mr_Green


1 Answers

You're mounting a host volume, that is your project folder into your container by doing

volumes:
      - .:/app

So, when you do something like WORKDIR /app, you're mapping the working directory where your Dockerfile is to /app in your container, so you're actually in your project folder on the host, not a copy. You're running npm install on the host again. If you modify anything inside that folder (app), the changes will be written directly to the host.

.dockerignore has no effect here. When you build an image, Docker will copy the content of the working directory where the Dockerfile exists (the build context) inside the image. That's an automatic process, you don't have to do it YOURSELF. So that's why you would use .dockerignore, so that you can exclude files and directories from being copied during the build process.

You mount volumes when you have data on your host that you would like to make available in a container (read/write access - it's configurable, appending :ro will make it read-only). Be aware that volumes are applied to containers at runtime. .dockerignore is just used when building the image. More on volumes.

like image 96
jperl Avatar answered Nov 14 '22 23:11

jperl