Npm install errror during docker container building - node.js

I created very simple docker file for my nodejs web application:
FROM node:8.11.4
FROM mysql:latest
WORKDIR /ess-explorer
COPY . .
RUN npm install
RUN cd config && cp config.json.example config.json && cp database.json.example database.json && cd ../
RUN npm run migrate
EXPOSE 3000
CMD ["npm", "dev"]
And docker.yml
version: '3'
services:
essblockexplorer:
container_name: ess-explorer
build: .
depends_on:
- db
privileged: true
ports:
- 3000:3000
- 3010:3010
db:
container_name: mysql
image: mysql
restart: always
volumes:
- db-data:/var/lib/mysql
environment:
MYSQL_ROOT_PASSWORD: '123'
volumes:
db-data:
After command docker-compose -f docker.yml build evey time I've got an error
Step 5/9 : RUN npm install
---> Running in d3644d792807
/bin/sh: 1: npm: not found
ERROR: Service 'essblockexplorer' failed to build: The command '/bin/sh -c npm install' returned a non-zero code: 127
What am i doing wrong? I found similar issues but i didnt find the real solution for solving this problem

You shouldn't need the mysql image in your Dockerfile at all; ideally your app container (essblockexplorer) accesses the db container (db) via a NodeJS client. All you need to do is;
Remove the FROM mysql:latest line from your Dockerfile.
Access the MySQL database via a NodeJS client using (db) as the hostname (this is automatically loaded as an alias into your container).

Related

Error response from daemon: failed to create shim when installing Nodejs

I am trying to create a docker image with node.js version 16. However I failed to find solution to this problem despite searching stackoverflow and other platform.
So basically I am using docker compose up and this is how my docker-compose.yml looks like:
version: '3.1'
services:
redis:
image: 'redis:alpine'
ports:
- "6379:6379"
webserver:
image: 'nginx:alpine'
working_dir: /application
volumes:
- '.:/application'
- './docker/nginx/nginx.conf:/etc/nginx/conf.d/default.conf'
ports:
- '3000:80'
php-fpm:
build: docker/php-fpm
working_dir: /application
volumes:
- '.:/application'
- './docker/php-fpm/php-ini-overrides.ini:/etc/php/7.4/fpm/conf.d/99-overrides.ini'
node:
build: docker/node
working_dir: /application
volumes:
- './:/application'
ports:
- '8080:8080'
The files are living in docker folder with 3 nested folders.
node/Dockerfile:
FROM node:16
WORKDIR /application
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 8080
CMD ["npm", "start"]
The npm start command consists of npm install and a postinstall where simply npm run dev would be executed.
Unfortunatley, I get the following error
Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "/bin/sh -c 'cd /application && npm install'": stat /bin/sh -c 'cd /application && npm install': no such file or directory: unknown
Also I want to add is, I am running this under WSL2.

docker-compose npm install && npm start in entrypoint

I'd like to start 2 containers using docker-compose. One for the database and one for a Node server using the default node image on DockerHub. In the Node container, I'd like to mount a local folder that's source controlled as a volume. I've seen many examples where a Dockerfile is created that copies source files into the image and then a RUN npm install command is used in the Dockerfile. But that results in a new image being created with the source files in it. In my case, I don't want to save source code files in the Node image. So I'd like the entrypoint of the Node container in my docker-compose file to run npm install as well as npm start, but I can't seem to get that combination to work. Here's my docker-compose.yml:
version: "3.8"
services:
server:
container_name: my_server
image: node:12.16.1
ports:
- "8000:8000"
volumes:
- ../my-server-files-source-controlled:/var/www
working_dir: /var/www
entrypoint: ["npm", "start"]
networks:
- my-network
db:
container_name: my_database
image: postgres
environment:
{ommitted}
ports:
- "5432:5432"
networks:
- my-network
networks:
my-network:
driver: bridge
The command above works fine, but when I change the entrypoint to include npm install, it fails. I've tried the following examples, as well as many others, and they all fail:
entrypoint: ["npm", "install", "&&", "npm", "start"]
entrypoint: ["npm install", "npm start"]
entrypoint: "npm install && npm start"
It seems like I can have npm install or npm start in entrypoint, but not both. How can get both those commands to work in my docker-compose file?
If you want to use multiple commands, you can do so with bash -c "commands":
services:
myservice:
command: bash -c "npm install && npm start"
By the way, in a production deployment, I would suggest using npm ci instead of npm install. Also consider using the --only=prod and --no-audit flags (depending on your setup).

docker can't find file to start app after build

I am using Typescript here and using node:latest in Docker, and I am using docker-compose as well,
I always failed to run it with docker-compose, when I run docker run ( manual ) it was work well,
here is my Dockerfile
FROM node:latest
RUN mkdir -p /home/myapp
WORKDIR /home/myapp
RUN npm i -g prisma2
ENV PATH /home/myapp/node_modules/.bin:$PATH
COPY package.json /home/myapp/
RUN npm install
COPY . /home/myapp
RUN prisma2 lift save --name 'init'
RUN prisma2 lift up
EXPOSE 8100
RUN npm run build
RUN pwd
RUN ls
RUN ls dist
CMD node dist/server.js
and my docker-compose.yml:
version: "3"
services:
app:
environment:
DB_URI: postgres://myuser:password#postgres:5555/prod
NODE_ENV: production
build:
context: .
dockerfile: Dockerfile
depends_on:
- postgres
volumes:
- ./home/edupro/:/home/myapp/
- ./node_modules:/home/myapp/node_modules
ports:
- "8100:8100"
postgres:
container_name: postgres
image: postgres:10-alpine
ports:
- "5555:5555"
environment:
POSTGRES_USER: myuser
POSTGRES_PASSWORD: password
POSTGRES_DB: prod
when it finishes doing CMD node /dist/server.js ( which folder I build because I am using TYpescript )
it gets an error like this :
Cannot find module '/home/edupro/dist/server.js'
I have to try to change volumes in docker-compose.yml as well like this:
- /home/myapp/node_modules:/home/myapp/node_modules
or
- ./:/home/myapp/node_modules
but still the same. do I miss something ? or did wrong mount?
how is the correct way to resolve that?
you need to remove the volume sections from your compose since that will overwrite all the files you build in your dockerfile, so delete this:
volumes:
- ./home/edupro/:/home/myapp/
- ./node_modules:/home/myapp/node_modules

Docker is not building container with changes in source code

I'm relatively new with Docker and I just created an Node.js application that should connect with other services also running on Docker.
So I get the source code and a Dockerfile to setup this image and a docker-compose to orchestrate the environment.
I had a few problems in the beginning so I just updated my source code and found out that it's not getting updated in the next build of docker-compose.
For example I commented all the lines that connect to Redis and MongoDB. I run the application locally and it's fine. But when I create it again in a container, I get the errors "Connection refused..."
I tried many things and this is what i get at the momment:
Dockerfile
FROM node:9
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node app.js
EXPOSE 8090
docker-compose.yml
version: '3'
services:
app:
build: .
ports:
- "8090:8090"
container_name: app
redis:
image: redis:latest
ports:
- "6379:6379"
container_name: redis
mongodb:
image: mongo:latest
container_name: "mongodb"
volumes:
- ./data/db:/data/db
ports:
- 27017:27017
up.sh
sudo docker stop app
sudo docker rm app
docker-compose build --no-cache app
sudo docker-compose up --force-recreate
Any ideas on what could be the problem? Why doesn't it use the current source code? It is using some sort of cache.

docker compose run with command error

From this documentation, it seems like that I can execute a single command from a service like this:
docker-compose run SERVICE CMD
But when I run
docker-compose up pwa npm test
I get the error
ERROR: No such service: npm
From my configurations, it will execute npm start, but I'd like to know how to execute other commands.
Files
Dockerfile:
From node:8
WORKDIR /app
copy package.json /app/
RUN npm install --quiet
CMD npm start
docker-compose.yml:
version: '3'
services:
pwa:
build: .
ports:
- '3000:3000'
volumes:
- ./src:/app/src
- ./public:/app/public
Versions
Docker version: 17.03
Docker compose version: 1.11.2
As docs say, the command is docker-compose run, not docker-compose up. The later expects all service names.
Do as this:
docker-compose run pwa npm test

Resources