ts-node app running using docker run but not docker compose - node.js

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.

Related

Dockerfile can not see package.json file

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.

package.json not found when moving from Dockerfile to docker-compose

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.

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