Dockerfile to run nodejs static-content in docker container

11/6/2018

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 ?

-- user2153844
docker
kubernetes
node.js
npm

2 Answers

11/6/2018

Assumptions:

  1. package.json is under Code directory.
  2. npm run build will be running outside of the container and a build directory will be created in Code directory.
  3. We will copy the whole Code/build directory under /app directory of the container.
  4. 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"]
-- S.Mishra
Source: StackOverflow

11/6/2018

You can specify the version of node specifically:

FROM node:8.9.3
-- Jim B.
Source: StackOverflow