Dockerfile to run nodejs static-content in docker container - node.js

Need an advice to dockerize and run a node JS static-content app on K8s cluster.
I have a static web-content which I run "npm run build” into the terminal which generates /build and direct my IIS webserver to /build/Index.html.
Now, I started creating a Docker file, how do I point my nodeJS image to invoke /build/Index.html file
FROM node:carbon
WORKDIR /app
COPY /Core/* ./app
npm run build
EXPOSE 8080
CMD [ "node", ".app/build/index.html" ]
Please how can I run this app only on node v8.9.3 and
npm 5.6.0 ?
Any inputs please ?

You can specify the version of node specifically:
FROM node:8.9.3

Assumptions:
package.json is under Code directory.
npm run build will be running outside of the container and a build directory will be created in Code directory.
We will copy the whole Code/build directory under /app directory of the container.
We will copy package.json to /app folder and will run the website through scripts available in package.json file.
Solution:
I would say add a script named start in the package.json and call that script from Dockerfile's CMD command. The script would look like:
"scripts": {
"start": "node ./index.html",
},
And the Dockerfile would look like:
FROM node:8.9.3
# Make app directory in the container.
RUN MKDIR /app
# Copy whole code to app directory.
COPY Code/build/ /app
# Copy package.json app directory.
COPY package.json /app
# make app directory as the working directory.
WORKDIR /app
# Install dependencies.
RUN npm install -only=production
# Expose the port
EXPOSE 8080
# Start the process
CMD ["npm", "start"]

Related

Hw do i build a docker image with socket.io

I am working on a chat application, using create-react-app framework for the frontend and nodejs for the backend, am also using socket.io for the connection. when testing locally, everything worked fine. am build a docker image to deploy on heroku, this is my DockerFile code below:
FROM node:lts-alpine
WORKDIR /app
COPY package*.json ./
COPY client/package*.json client/
RUN npm run install-client --only=production
COPY socket/package*.json socket/
RUN npm run install-socket --only=production
COPY server/package*.json server/
RUN npm run install-server --only=production
COPY client/ client/
RUN npm run build --prefix client
COPY server/ server/
COPY socket/ socket/
USER node
CMD [ "npm", "start", "--prefix", "server", "npm", "start", "--prefix", "socket"]
EXPOSE 5000
EXPOSE 8080
i dont know if am doing anything wrong, but when i deploy it , the build was successful, but when i link on the heroku link is give this error:
This chat-app-docker-demo.herokuapp.com page can’t be foundNo web page was found for the web address: https://chat-app-docker-demo.herokuapp.com/
HTTP ERROR 404

How to hit HTTP endpoint from Docker image?

I have NodeJS/TypeScript application (github repo) which is working fine when I run the script defined in package.json. i.e., npm run start will start my local host and I can hit endpoint via POSTMAN.
I have created docker image (I am new to Docker and this is my first image). Here, I am getting Error: connect ECONNREFUSED 127.0.0.1:7001 error in POSTMAN.
I noticed that I do not see Listening on port 7001 message in terminal when I run docker file. This tells me that I am making some mistake in .Dockerfile.
Steps:
I created docker image using docker build -t <IMAGE-NAME> . I can see successfully created image.
I launched container using docker run --name <CONTAINER-NAME> <IMAGE-NAME>
I've also disabled Use the system proxy setting in POSTMAN but no luck.
Details:
Package.json file
"scripts": {
"dev": "ts-node-dev --respawn --pretty --transpile-only src/server.ts",
"compile": "tsc -p .",
"start": "npm run compile && npm run dev"
}
Response from terminal when I run npm run start (This is successful)
Dockerfile
#FROM is the base image for which we will run our application
FROM node:12.0.0
# Copy source code
COPY . /app
# Change working directory
WORKDIR /app
# Install dependencies
RUN npm install
RUN npm install -g typescript
# Expose API port to the outside
EXPOSE 7001
# Launch application
CMD ["npm", "start"]
Response after running docker command
GitHub repo structure
By any chance did you forget to map your container port to the host one?
docker run --name <CONTAINER-NAME> -p 7001:7001 <IMAGE-NAME>
the -p does the trick of exposing the port to your network. The number on the left side is the container port (7001 as exposed on the Dockerfile) and the second one is the target port on the host machine. You can set this up to other available ports as well. Eg.: -p 7001:3000to expose on http://localhost:3000
Check out Docker documentation about networking
Finally, I was able to make this work with two things:
Using #Dante's suggestion (mentioned above).
Updating my .Dockerfile with following:
FROM node:12.0.0
# Change working directory
WORKDIR /user/app
# Copy package.json into the container at /app
COPY package*.json ./
# Install dependencies
RUN npm install
RUN npm install -g typescript
# Copy the current directory contents into the container at root level (in this case, /app directory)
COPY . ./
# Expose API port to the outside
EXPOSE 7001
# Launch application
CMD ["npm", "run", "start"]

Docker typescript node.js can't start app?

I have an index.ts, then this is my script
"scripts": {
"test": "jest",
"start": "ts-node-dev ./index.ts"
},
I tried to dockerize it, what should I do? do I need to add another command for npm build? or generate a .js file?
my dockerfile like this
FROM node:10-alpine
WORKDIR /
# copy configs to /app folder
COPY package*.json ./
COPY tsconfig.json ./
COPY . .
# check files list
RUN ls -a
RUN npm install
EXPOSE 3001
CMD [ "npm", "start"]
I can't access localhost:3001 in my browser after I run
docker build -t testApp .
then
docker run -p 80:3001 testApp

I'm trying to dockerize my node app using docker file.But it is throwing an error: Cannot find module '/app/app.js'

Docker File For MY Node app:
FROM node:10-alpine
# WORKDIR specifies the application directory
WORKDIR /app
# Copying package.json file to the app directory
COPY node-express/package.json /app
# Installing npm for DOCKER
RUN npm install
# Copying rest of the application to app directory
COPY . /app
EXPOSE 3000
# Starting the application using npm start
CMD ["npm","start"]
MY LOG FILE:
> hello-world#1.0.0 start /app
> node app.js
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module '/app/app.js'
MY Application Root Directory Structure:
app.js node_modules npm-debug.log package.json package-lock.json test

On server startup NPM find start script

I'm working with docker and I'm wondering how I can get the command npm start to locate the app.js file without me doing it via the command line.
my package.json (located: /srv/www) looks as so:
{
"name": "dist",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"start": "forever start -c \"nodemon --harmony\" app.js --exitcrash"
},
"author": "",
"license": "ISC"
}
I'm currently invoking my docker image as so:
docker run -d -v /srv/docker/instantynode/srv:/srv -p 80:8080 myimg ???
I am hoping to replace the ??? with a command which will startup node and invoke npm start in the correct directory, any ideas?
I was thinking maybe of making a little startup script to fix this however I was wondering if npm can fix this on it's own?
Regarding node application, you can start with ONBUILD in Dockerfile, so you can try as below:
$ cat Dockerfile
FROM node:0.10-onbuild (or version 0.12, depend your request)
PORT 8000
the image node:0.10-onbuild has codes already as below:
WORKDIR /usr/src/app
ONBUILD COPY package.json /usr/src/app/
ONBUILD RUN npm install
ONBUILD COPY . /usr/src/app
CMD ["npm" "start"]
With ONBUILD, during docker build, your node.js (or angularjs) codes will be not copied to image and npm install doesn't run.
But when you run the container, it starts to copy the files to /usr/src/app and install npm package. Then start npm service.
So in your case, you should be fine to run your application directly without mount the volume every time.
# I guess file package.json is under /srv/docker/instantynode/srv
$ cd /srv/docker/instantynode/srv
$ cat Dockerfile
FROM node:0.10-onbuild
PORT 8000
$ docker build -t myimg .
$ docker run -d -p 8000:8000 myimg
That's all, you should be fine to access your application via port 8000 now.

Resources