npm install package through running node container - node.js

I've followed the steps in the node.js documentation for creating a Dockerfile. I'm trying to run the command docker exec -it mynodeapp /bin/bash in order to go inside the container and install a new package via npm, but I get the following error
OCI runtime exec failed: exec failed: container_linux.go:346: starting container process caused "exec: \"/bin/bash\": stat /bin/bash: no such file or directory": unknown
Any ideas what I'm doing wrong?
for ref this is how my docker-compose and dockerfile look like
FROM node:latest
RUN mkdir /app
WORKDIR /app
RUN npm install -g nodemon
COPY package.json package.json
RUN npm install
COPY . .
EXPOSE 8080
CMD [ "node", "server.js" ]
and
version: '3'
services:
nodejs:
container_name: mynodeapp
build: .
command: nodemon --inspect server.js
ports:
- '5000:8080'
volumes:
- '.:/app'
networks:
- appnet
networks:
appnet:
driver: 'bridge'

Change docker exec mynodeapp -it /bin/bash to docker exec -it mynodeapp /bin/sh
According to docker documentation the correct syntax is the following:
docker exec [OPTIONS] CONTAINER COMMAND [ARG...]
-i and -t are the options
mynodeapp is CONTAINER
/bin/bash - is
a COMMAND inside container
And another problem is that there is no bash shell inside container, so you can use sh shell.

Related

Issue with docker permissions

I just learning about docker Volumes.
I have this code aside Node.js project.
this is my Docker file:
FROM node:14
WORKDIR /app
COPY package.json .
RUN npm install
COPY . .
EXPOSE 80
VOLUME [ "./app.feedback" ]
CMD ["node", "server.js"]
and this is my command
docker run -d -p 3000:80 --rm --name feedback-app -v feedback:/app/feedback -v "C:\Users\amit\personal\data-volumes-01-starting-setup:/app" -v /app/node_modules feedback-node:volumes
and this is the error
OCI runtime create failed: container_linux.go:380: starting container process caused: process_linux.go:545: container init caused: rootfs_linux.go:75: mounting "/var/lib/docker/volumes/ebfb2e17b5bf6768527c205789667de5ca31b29b9a9603297b7fbc2881c95701/_data" to rootfs at "/app/node_modules" caused: mkdir /var/lib/docker/overlay2/87136c8400ec6a42517fb51eb41084c6a6e5825f877addb7dea71af167a7a43d/merged/app/node_modules: permission denied: unknown.
PS C:\Users\amit\personal\data-volumes-01-starting-setup>
Use this base image instead:
FROM node:lts-alpine

Nodejs Serverless Docker Image

I am trying to setup a docker image for my local serverless development and I'm having issues reaching the files using volumes.
Dockerfile
FROM node:8.10
ADD . /code
WORKDIR /code
RUN npm install -g serverless
RUN npm install serverless-offline
EXPOSE 3000
# COPY . /code
CMD ["serverless", "offline", "--host", "0.0.0.0", "--port", "5000"]
docker-compose-yml
version: "3"
services:
serverless_proj_1:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:5000"
volumes:
- .:/code
- /code/node_modules
Docker is listening to all my serverless endpoints correctly:
But when i'm triggering one of the api endpoints from Postman this is the error I'm getting:
docker container exec apps-services_serverless_proj_1_1
pwd returns /code
docker container exec apps-services_serverless_proj_1_1 ls -al returns my codebase
docker container exec apps-services_serverless_proj_1_1 ls /code -al again returns my codebase(both commands have same total)
docker container exec apps-services_serverless_proj_1_1 ls /code/node_modules -al returns all my dependencies(total 3074)
I don't see an RUN npm install. since you are marking a place holder for the node_modules you will need to install them inside the container.
EDIT
Please try attaching to your running docker container and looking in the node_modules folder. Just to double check that a) you have one, b) it is located where it should be and c) it contains all the modules you expect.
You can do this by running docker container exec -it serverless_proj_1 /bin/bash this should put you in the /code dir by default, then just run ls -al

docker run on container but not run on server [duplicate]

This question already has answers here:
docker running but can't access on the server
(2 answers)
Closed 3 years ago.
i create Jenkinsfile, Dockerfile, Dockerfile.test to CI and CD my server API on GitHub, i build it on Jenkins and the build was successfully, and my docker run on the container as well,
on Jenkinsfile stages, i create for test and deploy on server API,
and using docker for the container
i also run Jenkins on docker also,
using docker-compose
here is my Dockerfile on my ubuntu server
FROM jenkins/jenkins:lts
USER root
and here is my docker-compose on ubuntu server
version: '3'
services:
jenkins:
build: .
container_name: jenkins
privileged: true
restart: always
ports:
- 8080:8080
volumes:
- ./jenkins_home:/var/jenkins_home
- /var/run/docker.sock:/var/run/docker.sock
- /usr/bin/docker:/usr/bin/docker
registry:
image: registry
container_name: registry
restart: always
ports:
- 5000:5000
what i did above , i follow this intruction
then i tried ro run it and login on my jenkins server,
my jenkinsfile something like this
try {
stage('Checkout') {
checkout scm
}
stage('Environment') {
sh 'git --version'
echo "Branch: ${env.BRANCH_NAME}"
sh 'docker -v'
sh 'printenv'
}
stage('Build Docker test'){
sh 'docker build -t employee-test -f Dockerfile.test --no-cache .'
}
stage('Docker test'){
sh 'docker run --rm employee-test'
}
stage('Clean Docker test'){
sh 'docker rmi employee-test'
}
stage('Deploy'){
if(env.BRANCH_NAME == 'master'){
sh 'docker build -t employee --no-cache .'
sh 'docker run -d -p 4000:4000 -e DB_USERNAME=admin -e DB_PASSWORD=adminxxx -e DB_NAME=employee employee'
}
}
}
catch (err) {
throw err
}
}
and my Dockerfile for those jobs
FROM node:carbon
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get -y install autoconf automake libtool nasm make pkg-config git apt-utils
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
RUN npm -v
RUN node -v
COPY ./server/ /usr/src/app
RUN npm install
EXPOSE 4000
ENV PORT 4000
ENV DB_USERNAME admin
ENV DB_PASSWORD adminxxx
ENV DB_NAME employee
CMD [ "npm", "run", "dev" ]
the jenkins job build it successfully and on the last stage my Jenkins, u can see that i run it on my docker container on my ubuntu server, after that finish, i tried to call server API on postman for http://ip-server:4000 , but it was nothing response, and i did set up the firewall tcp on my ubuntu serrver though
how can i solve this? so after Jenkins job finish, what i want i could call that server API on my postman to test it
Configuration looks good, So it seems that docker compose caching the volumes so please run this commands to cleanup every thing
docker rm employee
docker image rm employee
docker-compose down -v
docker-compose up
Please make sure that jenkins log this
'docker run -d -p 4000:4000 -e DB_USERNAME=admin -e DB_PASSWORD=adminxxx -e DB_NAME=employee employee'

Docker port forwarding for nodejs app

I'm having problems configuring docker for my nodejs app.
I have previously set up containers for both php and rails with port forwarding working flawlessly, but for this instance i can't seem to get it to work.
Running: docker ps, i get the following:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
a60f9c82d600 29c7d94a8c58 "/bin/sh -c 'npm s..." 5 seconds ago Up 3 seconds 3000/tcp romantic_albattani
As you can see I'm not getting the usual: 0.0.0.0:3000->3000/tcp that I am expecting.
docker-compose ps gives:
Name Command State Ports
------------------------------
My docker-compose.yml:
web:
build: .
volumes:
- .:/app
volumes_from:
- box
ports:
- "3000:3000"
box:
image: busybox
volumes:
- /node_modules
My Docker file:
FROM node:8.7.0
# The base node image sets a very verbose log level.
ENV NPM_CONFIG_LOGLEVEL warn
WORKDIR /tmp
COPY package.json /tmp/
RUN npm install
WORKDIR /app
ADD . /app
RUN cp -a /tmp/node_modules /app/
#ENV PORT=3000
EXPOSE 3000
CMD npm start
I'm running the command: docker-compose up --build
Any help at this point is appreciated.
I don't know if a docker inspect would be useful, but if so, tell me and i will also post it.
Edit: Changed my Dockerfile to follow the answer.
Your docker-compose.yml file has bad formatting, since you are not getting any errors i will assume you pasted it here wrong, here is the version with the fixed indenting:
web:
build: .
volumes:
- .:/app
volumes_from:
- box
ports:
- "3000:3000"
box:
image: busybox
volumes:
- /node_modules
Your Dockerfile has a bug, you are missing the ENTRYPOINT and/or CMD stanzas, instead you are using the RUN stanza with the wrong intent, here is a working Dockerfile with the fix applied:
FROM node:8.7.0
# The base node image sets a very verbose log level.
ENV NPM_CONFIG_LOGLEVEL warn
WORKDIR /tmp
COPY package.json /tmp/
RUN npm install
WORKDIR /app
ADD . /app
RUN cp -a /tmp/node_modules /app/
#ENV PORT=3000
EXPOSE 3000
CMD npm start
Your Dockerfile halted the execution of docker-compose at the docker image building stage because of the RUN npm start which is a process that starts and listens until stopped (because you want it to start your node app and listen for connections) causing docker-compose to never finish the docker image creating step, let alone the other steps like creating the needed containers and finish the entire docker-compose runtime process.
In short:
When you use RUN it is meant to run a command do some work and return sometime to continue the building process, it should return and exit code of 0 and the process will move on to the next Dockerfile stanza, or return another exit code and the building process will fail with an error.
When you use CMD you tell the docker image what is the starting command of all the containers started from this image (it can also be overridden at run time with docker run). It is tightly related to the ENTRYPOINT stanza, but for basic usage you are safe with the default.
Further reading: ENTRYPOINT, CMD and RUN

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