Accessing a node.js Hapi server running inside a docker container - javascript

I have built a docker container from centOS using a node.js hapi server. The server works fine running on its own, I get the right output in the console when running it inside the container. However, I don't know how to get at it.
Output from docker container
$ docker run -p 49000:3000 work/learning
Server running at: http://f878541bb9f8:3000
Pack Server running at: http://f878541bb9f8:5000
Docker file
# DOCKER-VERSION 0.3.4
FROM centos:centos6
# Enable EPEL for Node.js
RUN rpm -Uvh http://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
# Install Node.js and npm
RUN yum install -y npm
ADD . /src
RUN cd src; npm install
EXPOSE 3000
CMD node /src/server.js

Related

im getting this error when im running my server with docker

I am getting this error when I am running my node JS server with docker when i build it with gets build successfully but when I run this I get this error
I tried deleting the docker image and but I did not work
Try this dockerfile
# Use an official node runtime as a parent image
FROM node:16.13.1
# Set the working directory to /home/app
WORKDIR /app
# Bundle app source
COPY package*.json ./
# If you are building your code for production
# RUN npm install --only=production
RUN npm install
COPY . .
# Make port 80 available to the world outside this container
EXPOSE 80
CMD npm run dev

Customize run command for heroku container:release

On Heroku I'm trying to release a pushed image as described here: https://stackoverflow.com/a/50788844/2771889
docker build --file Dockerfile.prod --tag registry.heroku.com/my-app/web .
heroku container:login
docker push registry.heroku.com/my-app/web
heroku container:release web
My heroku.yml looks like this (used when I deploy from GitHub, and works fine):
build:
docker:
web: Dockerfile.prod
release:
image: web
command:
- yarn typeorm migration:run && yarn console seed
run:
web: yarn start:prod
It seems that the run command is not taken into account. When running container:release the logs show:
Starting process with command node
Whereas during a release from GitHub I'd see the correct command:
Starting process with command /bin/sh -c yarn start:prod
The release command however is recognized and executed correctly.
How can I make sure container:release runs the container with the correct command?
I had to add a CMD to my Dockerfile:
# ...
CMD yarn start:prod
This doesn't break GitHub deployments with heroku.yml.

How to run React Boilerplate with forever

I'm going to run react-boilerplate application forever in the server.
I found forever and I'm not sure how I pass parameters to forever. The command to run server is like following:
PORT=80 npm run start:production
Seems like forever start PORT=80 npm run start:production doesn't help me.
One thing is that PORT=80 part is setting the env variable, this kind of command should be in front of other commands. The other thing is that to run npm scripts with forever, you need to use different syntax, so PORT=80 forever start -c "npm run start:production" /path/to/app/dir/.
If you're running forever form the project folder, the path should be ./
Or you can run a react application with pm2 or with nohup
1) install pm2 globally
npm install pm2 -g
2) navigate to the project folder and execute, space is required after --
pm2 start npm -- start
3) to see running instances
pm2 ps
4) to see the other options
pm2 --help
To run with nohup
1) navigate to the project folder
nohup bash -c 'npm start' &
pm2 is superb production process manager for Node. In addition to starting and daemonizing any application, it has a built in load balancer.
Install pm2:
npm install pm2 -g
To add start and add deamon to your app, navigate to the app folder and:
pm2 start app.js
To make pm2 autoboot on server restart:
$ pm2 startup
Then copy and paste the code generated.
For this you will need:
Install forever usingnpm install -g forever
Run the forever command PORT=<YOUR PORT> forever start -c "<command>" ./
Your command can be for example npm start and npm run dev.
Use ./ only if you are in the project folder.
Port means your port number, usually 80 or 443.

gulp watch seems to freeze in Docker

I'm trying to "dockerize" our development environment. We have a gulp build system that watches changes to our js/sass/jade files. This is all setup to work just fine outside of docker.
I've created a docker container and I mount my code base into it (using a volume). All the precursor npm installs and bower installs finish successfully. My last step runs gulp and it runs properly and builds but then does not pick up any subsequent changes to any of our js/sass/jade files.
I'm running the build system with the following command:
docker run -it -v $(pwd):/code/ client gulp reset
Does anyone have a similar setup in their development environment? What did you do to get your gulp watch to work and display the building?
EDIT: I guess I could do the gulp build/watch outside of docker and only mount the generated files but I'd rather contain that all inside of docker so that the host machine doesn't need to worry about any dependencies to build/run our app
EDIT2: Here are my dockerfile and docker-compose.yml
#Dockerfile
FROM node:0.12.5
RUN mkdir /code
WORKDIR /code
RUN mkdir client
WORKDIR client
RUN mkdir .tmp
ADD ./client/package.json /code/client/package.json
ADD ./client/bower.json /code/client/bower.json
RUN npm install gulp -g
RUN npm install bower -g
RUN npm install
RUN npm rebuild node-sass
RUN bower --allow-root install
CMD gulp reset
and
client:
build: .
volumes:
- .:/code
I've never been able to get any inotify-based file watcher to ever work over with virtual-box guest additions, and based on this ticket it's unlikely to be available anytime soon. My preferred approach is the following:
Assuming my local source code is in /code
Run my watcher locally on /code
When a change is detected, rsync local /code to remote /code (mounted as a container-only volume) in the container
Example rsync:
docker run --rm --volumes-from sourcecode my/image \
rsync \
--delete \
--recursive \
--safe-links \
--exclude .git --exclude node_modules \
/local/repo/ /container/repo
This avoids lots of issues and allows you to get granular with what you want your container environment to see.

node.js server on raspberry pi

I am new to node.js and Raspberry pi, I just followed the tutorial in http://joshondesign.com/2013/10/23/noderpi
now npm -version and node -v shows well.
then I put the server.js file in home/pi/app directory
every time I run sudo node /home/pi/app/server.js or move to the app directory run sudo node server.js
It comes out an error, which is:
sudo : node: command not found?
How to fix that? And is there any method to set the server start automatically every time I boot the Pi?
Try
sudo $(which node) /home/pi/app/server
This way, you are searching the executable file location before running sudo, since your user's PATH is not searchable in a sudo environment.
About autorun, you should search Google for sysvinit or systemd, depending on your operating system. They are able to start daemons as root after boot.
You may want to check this thread: On EC2: sudo node command not found, but node without sudo is ok
This should help:
sudo ln -s /usr/local/bin/node /usr/bin/node
sudo ln -s /usr/local/lib/node /usr/lib/node
sudo ln -s /usr/local/bin/npm /usr/bin/npm
sudo ln -s /usr/local/bin/node-waf /usr/bin/node-waf

Categories

Resources