-1

Here is my project structure:

|   Dockerfile
|   package-lock.json
|   package.json
+---node_modules
\---server
        index.js

First, I create a folder named "node-docker" in d:\ .

Then I execute the following command to initialize the folder.

npm init

Here is the Dockerfile content:

FROM node:alpine
WORKDIR /app
CMD ["npm","run","dev"]
EXPOSE 80

The package.json content is as below:

{
  "name": "node-docker",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "dependencies": {
    "express": "^4.18.2"    
  },
  "devDependencies": {
    "nodemon": "^2.0.20"  
  },
  "scripts": {
    "dev": "nodemon server/index.js",  
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC"
}

And the file server/index.js content is as follows:

import http from 'http';
import Express from 'express';
let app = new Express();
let httpServer = http.createServer(app);
let port = 80;
app.get('/', (req, res) => {
  res.send('hello world')
});
httpServer.listen(port, () => {
    console.log('Express server is running on localhost:' + port);
});

When I execute the npm run dev command, when I update the string 'hello world' to 'hello world1', the browser shows the updated content when I refresh the browser, so the nodemon working properly.

I am using the following command to build a docker image:

D:\node-docker> docker build -t node-docker .

I am using the docker desktop to start the image container with the following optional setting.

Home path: d:\node-docker
Container path:/app

The container is started properly. Unfortunately, when I update the string 'hello world' in server/index.js to 'hello world1', the browser does not show the latest content even I refresh the browser. Would you tell me how to fix the problem?

PS: I got the same result, when I perform the above steps on 2 individual computers.

The KNVB
  • 3,588
  • 3
  • 29
  • 54
  • I am unable to reproduce this. After updating the `server/index.js`, can you go into the container and see if the file is updated? If not, then probably the bind mount is not configured properly. – vighnesh153 Aug 04 '23 at 05:01
  • The file in the container is updated. But the node server is not restarted. Do I need to use the command line to create a bind mount? – The KNVB Aug 04 '23 at 06:10
  • If the file is updated in the container, then I think bind mount is working correctly. It is hard to tell what could be the issue seems it is not reproducible. You don't **need** to use command line because docker desktop is capable of doing this, but if you have a CLI command that isn't working, then it will be easier to reproduce and debug. – vighnesh153 Aug 04 '23 at 06:32
  • I attached the screen capture for your reference. https://drive.google.com/file/d/1Zi5Fb3vkfEgLXVs1bz4jj8sPG3CfR2wL/view?usp=sharing – The KNVB Aug 04 '23 at 06:42

1 Answers1

0

It is hard to debug with the information that you shared what the issue could be. You could make use of a CLI command instead and it will be easier to debug. Or, you could also make use of docker-compose, which comes bundled with Docker.

  1. Create a docker-compose.yml file in your root directory and paste the following content:
version: '3'
services:
  app:
    build: .
    ports:
      # binds your host port :8080 with :80 container port
      - 8080:80
    volumes:
      # binds your CWD with the /app directory from the container
      - ./:/app
  1. Run docker-compose up to start the container.
vighnesh153
  • 4,354
  • 2
  • 13
  • 27