I've got this structure of the project:
- project
-- apps
--- microservice-one
---- Dockerfile
-- package.json
-- docker-compose.yml
Here is my Dockerfile from the microservice-one:
FROM node:12.13-alpine As development
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
CMD ["node", "dist/main"]
and docker-compose.yml
services:
microservice-one:
container_name: microservice-one
build:
context: ./apps/microservice-one
dockerfile: Dockerfile
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
expose:
- '3000'
command: npm run start
why after run docker-compose build, my console throw me this error:
#12 0.725 npm ERR! code ENOENT
#12 0.726 npm ERR! syscall open
#12 0.728 npm ERR! path /usr/src/app/package.json
#12 0.729 npm ERR! errno -2
#12 0.734 npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
I think that the problem is in my Dockerfile and COPY package*.json ./, is a chance that my Dockerfile can not see package.json?
Thanks for any help!
When your context is ./apps/microservice-one, the Dockerfile can only copy files from that directory and directories below it.
Your Dockerfile is written as if it assumes that the context is the current directory, so if you change the context and the dockerfile values in the docker-compose file, it should work.
services:
microservice-one:
container_name: microservice-one
build:
context: .
dockerfile: ./apps/microservice-one/Dockerfile
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
ports:
- 3000:3000
expose:
- '3000'
command: npm run start
From your filesystem structure the directory hosting Dockerfile is the build context -> microservice-one.
When you perform a docker build ..., the build context and all it's content gets wrapped up and sent over to the docker daemon, which then tries to build your container. At that time there is only access to the build context, nothing outside. ( https://docs.docker.com/engine/reference/commandline/build/#build-with-path )
So unless you move/copy your file over it will remain invisible.
Related
Using this Dockerfile:
FROM node:14-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
docker build -t ts-node-api .
docker run -it --entrypoint sh --expose 3000 -it ts-node-api
or
docker run --expose 3000 -it steve/ts-node-api
works correctly, apart from not connected to the db, which is a later problem.
However, run the same Dockerfile as such:
version: "3"
services:
mongo:
image: mongo
volumes:
- ./data:/data/db
ports:
- "27017:27017"
ts-node-api:
depends_on:
- mongo
build:
context: .
dockerfile: services/ts-node-api/Dockerfile
ports:
- 3000:3000
using docker compose results in:
=> ERROR [6/6] RUN npm run build 0.3s
------
> [6/6] RUN npm run build:
#9 0.244 npm ERR! code ENOENT
#9 0.244 npm ERR! syscall open
#9 0.244 npm ERR! path /usr/src/app/package.json
#9 0.245 npm ERR! errno -2
#9 0.246 npm ERR! enoent ENOENT: no such file or directory, open '/usr/src/app/package.json'
#9 0.246 npm ERR! enoent This is related to npm not being able to find a file.
#9 0.246 npm ERR! enoent
#9 0.248
#9 0.249 npm ERR! A complete log of this run can be found in:
#9 0.249 npm ERR! /root/.npm/_logs/2022-08-26T18_29_26_477Z-debug.log
Any help would be greatly appreciated, thank you.
Your two build setups are different. The docker build command you show is equivalent to
build:
context: .
# dockerfile: Dockerfile
which has a shorthand
build: .
If the Compose file is in an ancestor directory of the service, so you cd into that subdirectory and then run the docker build . command, you need to use that subdirectory name as the context directory
build: services/ts-node-api
The Compose block you show is equivalent to
docker build -f services/ts-node-api/Dockerfile .
which causes COPY commands to be interpreted relative to the root of your tree, not the directory containing your application (compare the two context: directories), which is why it doesn't work as expected.
I'm getting to know React and created a small application using Create-React-App, which is run inside a docker container using the following dockerfile:
FROM node:latest
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install app dependencies
COPY package.json ./
COPY package-lock.json ./
RUN npm install --silent
RUN npm install -g react-scripts#3.4.1 -g --silent
#add app to container
COPY . ./
CMD ["npm", "start"]
That works fairly well and I now wanted to integrate this into a docker-compose workflow, to run an api backend using asp.net core and this React application side-by-side:
version: '3.7'
services:
backend:
asp.netcore
db:
sql2019linux
ui:
container_name: ui
build:
context: ./UI
dockerfile: Dockerfile
ports:
- '8080:3000'
volumes:
- './:/app'
- '/app/node_modules'
This now fails with the node error:
npm ERR! code ENOENT
npm ERR! syscall open
npm ERR! path /app/package.json
npm ERR! errno -2
npm ERR! enoent ENOENT: no such file or directory, open '/app/package.json'
npm ERR! enoent This is related to npm not being able to find a file.
npm ERR! enoent
The backend and db services are coming up finde. What exactly is going sideways here and how to I fix it?
I think this may be your problem.
volumes:
- './:/app'
Looks like you are mounting the directory that the docker-compose files is in to the app directory of the container and npm start is probably bawking because it cannot find the package.json in the workdir of the built image (which is also /app).
Try removing the volumes.
Question
When running npm run start:debug command inside a docker container, I get this error:
# npm run start:debug
> api#0.0.0 start:debug /usr/src/api
> nest start -e "node --inspect-brk 0.0.0.0:9229" --watch -p tsconfig.json
sh: 1: nest: not found
npm ERR! code ELIFECYCLE
npm ERR! syscall spawn
npm ERR! file sh
npm ERR! errno ENOENT
npm ERR! api#0.0.0 start:debug: `nest start -e "node --inspect-brk 0.0.0.0:9229" --watch -p tsconfig.json`
npm ERR! spawn ENOENT
Running npm ls --depth=0 shows that I have #nestjs/cli installed:
# npm ls --depth=0
api#0.0.0 /usr/src/api
+-- #nestjs/cli#6.14.2
+-- #nestjs/common#6.11.11
...
Why isn't the nest cli binary being found?
My Setup
This is how I launch the shell:
docker-compose -f docker-compose-base.yml -f docker-compose-dev.yml run api /bin/sh
My docker-compose files:
# -base
version: '3'
services:
api:
build: .
restart: on-failure
volumes:
- /usr/src/api/node_modules
container_name: api
# -dev
version: '3'
networks:
# Use lb_lbnet network created by the load balancer repo (lb)
# We do this because we need the load balance to resolve container names defined here to forward traffic
# This is only needed for dev
default:
external:
name: lb_lbnet
services:
db:
image: postgres:11
container_name: db
restart: always
env_file:
- ./db.env # uses POSTGRES_DB and POSTGRES_PASSWORD to create a fresh db with a password when first run
volumes:
- ./postgres-data:/var/lib/postgresql/data
# only used to upload DB dump:
# - ./backup:/tmp/backup
api:
restart: 'no'
build:
context: .
args:
NODE_ENV: development
depends_on:
- db
ports:
- 9229:9229
volumes:
- ./:/usr/src/api
- ./node_modules:/usr/src/api/node_modules
# enable to debug hgezim-express-shopify-auth
- ../../hgezim-express-shopify-auth:/usr/hgezim-express-shopify-auth
env_file:
- .env
command: /bin/bash -c 'echo "Starting" && npm install && npm run start:debug'
My Dockerfile:
FROM node:12
WORKDIR /usr/src/api
COPY package*.json ./
ARG NODE_ENV=production
ENV NODE_ENV=${NODE_ENV}
RUN npm install # && npm ls --depth=0 # commented this out since it returns non-zero exit code
COPY . .
VOLUME [ "/usr/src/api/node_modules" ]
RUN ["/usr/local/bin/npm", "run","lint"]
RUN ["/usr/local/bin/npm", "run","build"]
# not using an execution list here so we get shell variable substitution
CMD /bin/bash -c 'npm run start:$NODE_ENV'
Nest CLI needs to be installed globally for the command line to work. Looks like you have it installed locally via package.json so nest was not added to PATH. Either add RUN npm install -g #nestjs/cli to your Dockerfile, or change start:debug script to use the local version (something like node_modules/<nestcli module>/.bin/nest).
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
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.