Node.js modules not building in Docker - node.js

I'm building this dockerfile using docker-compose and I need it to build native modules in docker (not just copy them from local). This only works when my local modules are built (npm install) As soon as I delete them this runs but there is no node_modules directory and it gives an error: Error: Cannot find module 'express'
FROM mhart/alpine-node:6
MAINTAINER Me
COPY package.json index.js lib /app/
WORKDIR /app
RUN apk add --no-cache make gcc g++ python && \
addgroup -S app && adduser -S -g app app && \
npm install && \
npm cache clean && \
apk del make gcc g++ python
USER app
And here is the app directory:
.dockerignore
.eslintignore
.eslintrc.js
Dockerfile
docker-compose.yml
index.js
lib
npm-debug.log
package.json

The problem was with the way docker binds the app folder from the host to the container. The second line in the volume section from my docker-compose.yml fixed it.
volumes:
- .:/app
- /app/node_modules

Related

Amazon EC2 Docker error: Couldn't find a package.json file in "/"

Our docker deployment to our EC2 instance has been running fine and stopped working with our most recent deployment.
The error when I SSH into the instance says:
[1/4] Resolving packages...
success Already up-to-date.
Done in 0.07s.
yarn run v1.22.19
error Couldn't find a package.json file in "/"
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Here's my Dockerfile:
FROM node:19-alpine3.16
# Create app directory
RUN mkdir -p /usr/src/app
# Make the dir the work directory
WORKDIR /usr/src/app
COPY . .
RUN apk --no-cache --virtual build-dependencies add \
python \
make \
g++ \
&& npm install \
&& apk del build-dependencies
RUN node -v
EXPOSE 80
Here are the things I've done:
Checked the filesystem using docker export | tar t. I can confirm that the package.json is under /usr/src/app
Changed the node version to the latest and tried a few other versions
Restarted the server
Added COPY package.json . both before and after COPY . .
What could be the issue and how can I fix it?

SQLite working with Docker path volume but not with the named one

I have Node.js app which is using Sequelize ORM for working with SQLite DB and umzug library for migrations. App is containerized with Docker.
When I run my container like:
docker run -v /home/user/data:/app/data image:tag
everything is working fine, but when I want to use named volume:
docker volume create appdata
docker run -v appdata:/app/data image:tag
I'm getting this error:
DatabaseError [SequelizeDatabaseError]: SQLITE_ERROR: duplicate column name: isPublic
Here is my Dockerfile:
FROM node:14-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
# Install client dependencies
RUN mkdir -p ./public ./data \
&& cd client \
&& npm install \
&& npm rebuild node-sass
# Build
RUN npm run build \
&& mv ./client/build/* ./public
# Clean up src files
RUN rm -rf src/ ./client \
&& npm prune --production
EXPOSE 5000
ENV NODE_ENV=production
CMD ["node", "build/server.js"]
Any ideas what might be the reason? Something with permissions maybe?

node_modules not mounted in docker container

I have dockerized a php application which require some npm dependencies, so I've installed in the Docker container nodejs and the required packages using:
FROM php:8.0.2-fpm-alpine
WORKDIR /var/www/html
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install mysqli
RUN apk add icu-dev
RUN docker-php-ext-configure intl && docker-php-ext-install intl
RUN apk add --update libzip-dev curl-dev &&\
docker-php-ext-install curl && \
apk del gcc g++ &&\
rm -rf /var/cache/apk/*
COPY docker/php-fpm/config/php.ini /usr/local/etc/php/
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
RUN apk add --update nodejs nodejs-npm
RUN npm install gulp-cli -g
RUN npm install
COPY src src/
CMD ["php-fpm"]
EXPOSE 9000
this is my docker-compose.yml:
version: '3.7'
services:
php-fpm:
container_name: boilerplate_app
restart: always
build:
context: .
dockerfile: ./docker/php-fpm/Dockerfile
volumes:
- ./src:/var/www/html
the problem's that when I enter in the container using: docker exec -ti boilerplate_app sh
and launch that command: ls -la I can't see any node_modules folder, infact, if I try to execute the installed dependency gulp I get:
Local modules not found in /var/www/html
Try running: npm install
What I did wrong?
There are two issues:
You are running npm install in a folder that does not contain any package.json listing the required node modules to install. If you inspect the logs you should see something like
no such file or directory, open '/var/www/html/package.json'
Moreover, when you mount your local src folder, you are replacing the content of /var/www/html/ with src content, which might not include any node_modules folder
volumes:
- ./src:/var/www/html

Why can't yarn.lock found in my Docker container?

I am run a single Node.js script like so: docker run -it --rm -u node --name build_container -v $(pwd):/home/node/app -w "/home/node/app" node:lts bash -c "yarn install --no-cache --frozen-lockfile".
However, the script log shows the displays info No lockfile found, and, what is even weirder, a message that says a package-lock.json was found. However, the work directory has no package-lock.
Are there any ideas what could be the issue?
I would suggest using your own Dockerfile to build your image - and then run it inside the build - like so:
Dockerfile
FROM node:12-alpine
# Create work directory
RUN mkdir -p /express
WORKDIR /express
# Bundle app sources
COPY . .
# Build app
RUN yarn install --prod --frozen-lockfile && yarn run build
EXPOSE 3000
# Set default NODE_ENV to production
ENV NODE_ENV production
ENTRYPOINT [ "yarn", "start" ]
.dockerignore
node_modules
build
dist
config
docs
*.log
.git
.vscode
And then build the image:
docker build -t <your image name> -f <Dockerfile (if omitted uses local folder .Dockerfile> <path to code your code>
Once built, run it as you would a normal image - as everything is already in.

New package.json packages are not showing in Docker container

I am using Docker with Docker Compose and these are my files:
#DOCKERFILE
FROM mhart/alpine-node
# Create app directory
RUN mkdir -p /home/app
# Bundle app soure
COPY . /home/app
# From now on we work in /home/app
WORKDIR /home/app
# Install yarn and node modules
RUN echo -e 'http://dl-cdn.alpinelinux.org/alpine/edge/main\nhttp://dl-
cdn.alpinelinux.org/alpine/edge/community\nhttp://dl-
cdn.alpinelinux.org/alpine/edge/testing' > /etc/apk/repositories \
&& apk add --no-cache yarn \
&& yarn
EXPOSE 8080
This is the docker-compose file for dev:
app:
build: .
command: yarn start:dev
environment:
NODE_ENV: development
ports:
- '8080:8080'
volumes:
- .:/home/app
- /home/app/node_modules
The problem I am having is that this setup seems to work just once because no matter which new module I add to the package.json, whenever I run docker-compose build it will not install the new package.
The reason why I am using the volumes is because nodemon would not work without .:/home/app, but if the node modules are not installed in the host then it will fail, reason why I need /home/app/node_modules. I suspect this could be the cause of my error, but I am not sure how to circumvent that.
I solved this by moving my src code inside an src directory.
This means my docker-compose.yml file now looks like this:
app:
build: .
command: yarn start:dev
environment:
NODE_ENV: development
ports:
- '8080:8080'
volumes:
- ./src:/home/app/src
Since I am not mounting the whole dir with the node_modules, new ones seem to be installed correctly.
The package.json should be copied into app directory and "npm install" should be invoked in Dockerfile before copying the bundle line.
#DOCKERFILE
FROM mhart/alpine-node
# Create app directory
RUN mkdir -p /home/app
WORKDIR /home/app
# Install app dependencies
COPY package.json /home/app
RUN npm install
# Bundle app soure
COPY . /home/app
# Install yarn and node modules
RUN echo -e 'http://dl-cdn.alpinelinux.org/alpine/edge/main\nhttp://dl-
cdn.alpinelinux.org/alpine/edge/community\nhttp://dl-
cdn.alpinelinux.org/alpine/edge/testing' > /etc/apk/repositories \
&& apk add --no-cache yarn \
&& yarn
EXPOSE 8080
If there is any new dependency registers in package.json, it should be installed when the docker build command is invoked.

Resources