Home > Enterprise >  Docker sync volumes
Docker sync volumes

Time:08-29

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.

CodePudding user response:

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"

CodePudding user response:

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!

  • Related