djt
djt

Reputation: 7535

Using "npm install" for development environment in Docker

I'm trying to work on a ReactJS project for development purposes in Docker, and this project will be shared across a team.

However, I'm having trouble understanding how this works because I want to volume share my app directory into the container, but it seems I have to copy package.json into the container to npm install and therefore run nom run start

my directory structure is:

app/
  src/
  public/
  node_modules/
  package.json
docker/ 
  admin/
    dockerfile
docker-compose.yml

dockerfile

FROM node:7.8.0

WORKDIR /app
RUN npm install

EXPOSE 3000
CMD npm run start

docker-compose.yml

version: '3'
services:
  admin:
    build:
      context: ./docker/admin
      dockerfile: dockerfile
    image: project/admin
    volumes:
     - ./app:/app
    ports:
        - "3000:3000"

So the problem is: when the dockerfile runs, npm install fails because the volume sharing from the docker-compose hasn't happened yet, so it thinks there's no package.json. So it sounds like I have to copy package.json into the container, which isn't ideal for development as far as making changes.

Furthermore, I believe you can only COPY files into the container from within the same directory as the dockerfile, which would mean I'd have to move the dockerfile to the root of the project. I don't love this idea because I'll have other containers in this project as well, and they'll need dockerfiles too.

Is there better solution for handling Node development from within Docker? Essentially, I'm looking to make the "up and running" process as simple as possible for other devs on the team.

Upvotes: 1

Views: 780

Answers (1)

galkin
galkin

Reputation: 5519

During development you need:

  • rebuild your frontend
  • restart you Node.js application
  • rerun you tests
  • debug your code
  • etc

If you will try put you files to Docker container during development, you will have overhead on development process. You computer and developer will spend more time, then with native usage node.js

That why wrapping Node.js in Docker during development is bad practice.

When you need Docker during development?

  • setup Developer/CI/QA environments with external dependency like MongoDB, Redis, etc. docker-compose is the best solution rights now.
  • making deliverable artifact. Dockerfile + .dockerignore will describe how make your artifact

Upvotes: 1

Related Questions