skip to Main Content

So… I trying start learning Docker. But I can’t sync the host and the container using volumes on change and save code (using the npm run dev). All the time I have to restart the docker-compose up --build to the "sync" kick in, but after this any change don’t update the folder or code. My exemple code:

install Next.js typescript as base.

npx create-next-app@latest --ts

Docker-compose:

version: "3"
services:
  node-myname:
    build:
      context: .
      dockerfile: DockerFile.dev
    ports:
      - "3000:3000"
    command: "npm run dev"
    restart: always
    volumes:
      - .:/usr/src/home/node/app/
  

DockerFile:

FROM node:16

WORKDIR /home/node/app

COPY . .

RUN npm install

The recomended is using volumes insted bind…

I can’t find a workaround.

2

Answers


  1. Chosen as BEST ANSWER

    After total reinstall Docker Windows (using Revo uninstaller)... the sync is working (i can create a file .txt and this is show on docker exec)! But, after that the hot reaload using dev don't work on Docker. I solve this using another StackOverflow post: Solution Docker Hot Reload Install on the next.config.js.

    Another thing... if i copy all the stuff from Nextjs host to the Docker container the Sync go way again... so... I can only copy COPY package.json ./ from host.

    next.config.js:

     /** @type {import('next').NextConfig} */
        const nextConfig = {
          reactStrictMode: true,
          swcMinify: true,
          // add to hot reaload work... windows
          webpackDevMiddleware: config => {
            config.watchOptions = {
              poll: 800,
              aggregateTimeout: 300,
            }
            return config
          },
        }
        
        module.exports = nextConfig
    

    Dockerfile:

    FROM node:16
    
    WORKDIR /home/node/app
    
    COPY package.json ./
    
    RUN npm install
    

    docker-compose:

    version: "3"
    services:
      node-myname:
        build:
          context: .
          dockerfile: DockerFile.dev
        ports:
          - "3000:3000"
        command: "npm run dev"
        restart: always
        volumes:
          - .:/home/node/app
      
    

    and all finaly work as intended. I don't know why... but if i try the same path as before i get a error... but all work now!

    Git Hub Final Code


  2. May be you need to use same path?!

        volumes:
          - .:/home/node/app/
    

    UPD: Checkout: https://github.com/theanurin/stackoverflow.68511005 and see INITLOG.md with steps "how to setup the project"

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search