Docker compose config file not syncing directory properly in Mac - node.js

Scenario
I use docker-compose for development in NodeJs and MongoDb.
Here is the files structure
├── admin
├── backend
├── docker-compose.yml
├── frontend
└── package-lock.json
Here is the content of docker-compose.yml
version: '3'
services:
swyft-database:
image: mongo
container_name: swyft-database
ports:
- '27017:27017'
swyft-node-server:
image: node
container_name: swyft-node-server
volumes:
- ./backend :/usr/app/node-services
working_dir: /usr/app/node-services
command: npm run dev
ports:
- '3000:3000'
links:
- swyft-database
depends_on:
- swyft-database
Problem
Whenever i run 'docker-compose up' command it creates another 'backend' directory in same directory and fails to execute with following error
swyft-node-server | npm ERR! code ENOENT
swyft-node-server | npm ERR! syscall open
swyft-node-server | npm ERR! path /usr/app/node-services/package.json
swyft-node-server | npm ERR! errno -2
swyft-node-server | npm ERR! enoent ENOENT: no such file or directory, open '
/usr/app/node-services/package.json'
swyft-node-server | npm ERR! enoent This is related to npm not being able to find a file.
swyft-node-server | npm ERR! enoent
swyft-node-server |
swyft-node-server | npm ERR! A complete log of this run can be found in:
swyft-node-server | npm ERR! /root/.npm/_logs/2019-11-14T08_25_24_289Z-debug.log
This is quite bizarre behavior of docker-compose creating same named directory. I believe this has something to do with docker-compose configuration file.
I am running it on MacOs Catalina

You should specify the volume as:
volumes:
- backend :/usr/app/node-services
So backend (without directory specification ./) would be created in that destination and also check if work_dir is needed alongside Volumes.

Related

Docker Compose build then package.json no such file or directory, open

my folder list
root : docekr-compose.yml
1depth folder: /xxxxx/Docker
1depth folder: /xxxxx/sources....
docker-compose.yml
version: '3.7'
services:
clientBackend:
container_name: clientbackend
image: clientbackend-image
build:
context: 3kings_client_backend
dockerfile: Dockerfile.dev
ports:
- 5000:5000
networks:
- 3kingsnet
volumes:
- .:/clientbackend
volumes:
data:
driver: local
networks:
3kingsnet:
driver: bridge
Docker File
FROM node:12-alpine
WORKDIR /clientbackend
COPY . /clientbackend
RUN ls
RUN npm install --silent
ENV NODE_ENV=local
RUN ls
CMD ["npm","run","local"]
build then
Attaching to clientbackend
clientbackend | npm ERR! code ENOENT
clientbackend | npm ERR! syscall open
clientbackend | npm ERR! path /clientbackend/package.json
clientbackend | npm ERR! errno -2
clientbackend | npm ERR! enoent ENOENT: no such file or directory, open '/clientbackend/package.json'
clientbackend | npm ERR! enoent This is related to npm not being able to find a file.
clientbackend | npm ERR! enoent
clientbackend |
clientbackend | npm ERR! A complete log of this run can be found in:
clientbackend | npm ERR! /root/.npm/_logs/2020-04-24T04_39_57_661Z-debug.log
clientbackend exited with code 254
OTL..

How do I create a live-reload development environment in docker? (NodeJS, Redis & Vue)

I've been attempting to create a docker development environment on my windows machine. The structure of my application is an express/node backend, a vue-cli generated client and a redis-server for caching.
I managed to get the project running, however the final piece I'm struggling with is hot reloading. I've installed nodemon as a dependency and added a script to the package.json file. Whenever I make a change to my server, I'd like the project to restart and show the new updates. This is my current setup. From what I've read it's something to do with volumes (I'm very new to docker).
API/Server Dir Structure
backend
- server.js (express server)
- Dockerfile
- docker-compose.yml
- Models
- ect...
Dockerfile
FROM node:8
WORKDIR /app
COPY package.json /app
COPY . /app
RUN npm install
CMD ["npm", "start"]
EXPOSE 3000
docker-compose.yml
version: "3"
services:
web:
container_name: web-container
restart: always
depends_on:
- redis
build: .
volumes:
- .:/app
ports:
- "80:3000"
links:
- redis
redis:
container_name: redis-container
image: "redis:latest"
ports:
- "6379:6379"
volumes:
- ./data:/data
Important:
When adding the volumes field on the web service, the following error is thrown
web-container | > projectname#1.0.0 start /app
web-container | > nodemon server.js --trace-warnings
web-container |
web-container | sh: 1: nodemon: not found
web-container | npm ERR! code ELIFECYCLE
web-container | npm ERR! syscall spawn
web-container | npm ERR! file sh
web-container | npm ERR! errno ENOENT
web-container | npm ERR! projectname#1.0.0 start: `nodemon server.js --trace-warnings`
web-container | npm ERR! spawn ENOENT
web-container | npm ERR!
web-container | npm ERR! Failed at the projectname#1.0.0 start script.
web-container | npm ERR! This is probably not a problem with npm. There is likely additional logging o utput above.
web-container | npm WARN Local package.json exists, but node_modules missing, did you mean to install?
web-container |
web-container | npm ERR! A complete log of this run can be found in:
web-container | npm ERR! /root/.npm/_logs/2019-12-23T18_42_42_900Z-debug.log
If I remove the volumes field from the service, it runs smoothly and as intended, however I can't begin to approach the hot reloading.
Your problem is just that nodemon is not on your executable path. npm install isn't going to do that for you.
There are many ways to fix this, but the simplest would be to just install nodemon globally in the container:
RUN npm install -g nodemon --no-optional

Docker-compose up : no such file or directory, open '/usr/src/app/package.json'

I'm using docker and docker-compose to run my express nodejs api.
Here is my docker file:
FROM node:10-alpine
ARG NODE_ENV=development
ENV NODE_ENV=${NODE_ENV}
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN chmod 755 /usr/src/app
CMD [ "npm", "start" ]
And as I mentioned I'm using docker-compose, here is the docker-compose.yml file content:
version: "3"
services:
service:
build: .
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3001:3001
command: npm start
After running docker-compose up, I'm facing an error says it's not able to find package.json.
Here is the error:
service_1 | npm ERR! path /usr/src/app/package.json
service_1 | npm ERR! code ENOENT
service_1 | npm ERR! errno -2 service_1 | npm ERR! syscall open
service_1 | npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
service_1 | npm ERR! enoent This is related to npm not being able to find a file.
service_1 | npm ERR! enoent
service_1 |
service_1 | npm ERR! A complete log of this run can be found in:
service_1 | npm ERR! /root/.npm/_logs/2019-04-17T07_54_07_773Z-debug.log
xuser-api_service_1 exited with code 254
Please help to find my mistake.
your working directory is /usr/src/app and you copied the package file on root directory .
you have to something like this
# set working directory
WORKDIR /usr/src/app
# install node_modules
ADD package.json /usr/src/app/package.json
RUN npm install
# copy codebase to docker codebase
ADD . /usr/src/app
you may be using an old image which does not contain latest changes.
make sure you using the latest image of your docker file.
docker-compose build
then run
docker-compose up
if you doing frequent changes to Dockerfile for testing then use.
docker-compose up --build
Extend your build section to this:
build:
context: MySuperAngularProject
dockerfile: ./Dockerfile
In context you may set folder with your Angular project with Dockerfile

Docker compose for node server fails

I wrote a node server that runs via npm script (npm run dev).
I created the following Dockerfile for it:
FROM node:10.12.0
ARG COMMIT_REF
ARG BUILD_DATE
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
ENV APP_COMMIT_REF=${COMMIT_REF} \
APP_BUILD_DATE=${BUILD_DATE}
ENV PORT 8009
EXPOSE 8009
CMD [ "npm", "run", "server" ]
Building and running the server with:
docker build . -t server and docker run -it -p 8009:8009 --env-file .env server works great and the server runs and exposed on port 8009.
Noe I try to create docker-compose.yml for it. This is what I wrote:
version: '3'
services:
server:
image: server
build: .
env_file:
- .env
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
working_dir: /.
ports:
- 8009:8009
When I run it using docker-compose up I get:
Starting bed_server_1 ... done
Attaching to bed_server_1
server_1 | npm ERR! path /package.json
server_1 | npm ERR! code ENOENT
server_1 | npm ERR! errno -2
server_1 | npm ERR! syscall open
server_1 | npm ERR! enoent ENOENT: no such file or directory, open '/package.json'
server_1 | npm ERR! enoent This is related to npm not being able to find a file.
server_1 | npm ERR! enoent
server_1 |
server_1 | npm ERR! A complete log of this run can be found in:
server_1 | npm ERR! /root/.npm/_logs/2019-02-04T22_27_50_916Z-debug.log
bed_server_1 exited with code 254
Any idea what is wrong?
The image's CMD runs npm run server in whatever directory it starts up in. The docker-compose.yml working_dir: / tells the container to start up in the root directory. That's not what you want; you want the container to start up in the directory you COPY'd its source code to.
You will probably fix your immediate issue if you remove the working_dir: line.

"npm ERR! enoent ENOENT: no such file or directory" - only happens on 1 computer

I'm using a Windows 10 machine for development at home, a Windows 10 machine for development at work, and an Ubuntu 1604 server for production....
When I run "docker-compose -f docker-compose.yml -f docker-compose.dev.yml up" it always works fine on my Windows 10 development machine at home, but when I run the same command on my Windows 10 machine at work it always throws the error...
database | npm ERR! path /usr/src/app/package.json
database | npm ERR! code ENOENT
database | npm ERR! errno -2
database | npm ERR! syscall open
database | npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
database | npm ERR! enoent This is related to npm not being able to find a file.
database | npm ERR! enoent
database |
database | npm ERR! A complete log of this run can be found in:
database | npm ERR! /root/.npm/_logs/2019-01-17T16_28_12_239Z-debug.log
I'm using the exact same code, both on a windows 10 OS! I started using Docker specifically to avoid issues like this and its driving me crazy.
Does anyone know what may be happening???
Dockerfile for my Node app...
FROM node:10
# Create app directory
WORKDIR /usr/src/app
# Install app dependencies
COPY package*.json ./
RUN npm install
RUN npm install -g nodemon
# Bundle app source into container
COPY . .
EXPOSE 8080
docker-compose.yml file...
version: "2"
services:
app:
container_name: database
build: .
links:
- mongo
mongo:
container_name: mongo
image: mongo:4
and docker-compose.dev.yml file for running in development....
version: "2"
services:
app:
restart: always
volumes:
- .:/usr/src/app
ports:
- "8080:8080"
command: ["npm", "start"]
mongo:
ports:
- "27017:27017"
I had exactly this problem when running my "docker-compose up" command from WSL bash.
Sharing drive c to Docker and launching via Power Shell fixed my issue.

Resources