Hello Docker Community,
I’m currently facing an issue with Docker on my Windows 11 machine. I’m trying to bind my local folder to a Docker container, but it’s not working as expected. The same setup works fine on my Mac OS.
Here’s my setup:
I am trying to dockerize environment for a simple node js app.
My docker-compose.yaml
services:
mymongodb:
image: mongo:latest
ports:
- "27018:27017"
volumes:
- prateek-mongo:/data/db
my_custom_app:
build: ./
ports:
- "3001:3001"
volumes:
- ./:/usr/src/app
volumes:
prateek-mongo:
.Docker file
FROM mhart/alpine-node
WORKDIR /usr/src/app
COPY . .
RUN npm install
EXPOSE 3001
CMD ["npm","run", "dev"]
index.js file
import express from "express";
import { Entry } from "./db.js";
const app=express();
const PORT=process.env.PORT||3001;
app.get('/',async (req,res)=>{
try {
const entry = new Entry({ text: 'This is an second entry' });
await entry.save();
res.send('Entry added second time !');
} catch (err) {
res.status(500).send('Error occurred');
}
});
app.listen(PORT,()=>{
console.log(`Server Started at PORT ${PORT}`);
})
Folder Structure
.gitignore
db.js
docker-compose.yml
Dockerfile
index.js
package-lock.json
package.json
The issue is that when I run the container, the local folder doesn’t seem to bind with the Docker container. I’ve tried several solutions but none seem to work. Any help would be greatly appreciated.
also when I remove this piece from my docker-compose file
volumes:
- ./:/usr/src/app
it work fine in windows,but I am not able to make my server start with changes ,I have to again compose up .
Thank you in advance!