2

I Dockerkized a MENN(Nextjs) stack App, now everything works fine. I run into issues when i need to install npm packages. let me first show you the structure

src/server/Dockerfile

FROM node:14-alpine
WORKDIR /usr/app
COPY package*.json ./
RUN npm install -qyg [email protected]
RUN npm install -qy
COPY . .
CMD ["npm", "run", "dev"]

src/client/Dockerfile

FROM node:14-alpine
WORKDIR /usr/app
COPY package*.json ./
RUN npm install -qy
COPY . .
CMD ["npm", "run", "dev"]

src/docker-compose.yml

version: "3"
services:

  client:
    build: 
      context: ./client
      dockerfile: Dockerfile
    ports:
      - 3000:3000
    networks:
      - mern-network
    volumes:
      - ./client/src:/usr/app/src
      - ./client/public:/usr/app/public
    depends_on:
      - server
    environment:
      - REACT_APP_SERVER=http://localhost:5000
      - CHOKIDAR_USEPOLLING=true
    command: npm run dev
    stdin_open: true
    tty: true
  
  server:
    build:
      context: ./server
      dockerfile: Dockerfile
    ports:
      - 5000:5000
    networks:
      - mern-network
    volumes:
      - ./server/src:/usr/app/src
    depends_on:
      - db
    environment:
      - MONGO_URL=mongodb://db:27017
      - CLIENT=http://localhost:3000
    command: /usr/app/node_modules/.bin/nodemon -L src/index.js

  db:
    image: mongo:latest
    ports:
      - 27017:27017
    networks:
      - mern-network
    volumes:
      - mongo-data:/data/db

networks:
  mern-network:
    driver: bridge

volumes:
  mongo-data:
    driver: local

Now if i install any packages using the host machine it is as expected updated in package.json file and if run

docker-compose build

the package.json is also updated inside the container which is fine, but i feel like this kinda breaks the whole point of having your App Dockerized! , if multiple developers need to work on this App and they all need to install node/npm in their machines whats the point of using docker other than for deployments? so what I do right now is

sudo docker exec -it cebc4bcd9af6 sh //login into server container

run a command e.g

npm i express

it installs the package and updates package.json but the host package.json is not updated and if i run the build command again all changes are lost as Dockerfile copies in the source code of host into container, is there a way to synchronize the client and host? in a way that if i install a package inside my container that should also update the host files? this way i dont need to have node/npm installed locally and fulfills the purpose of having your App Dockerized!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.