Dockerfile -> plan.sh issues

Hello, I am attempting to "habitize" an application that already uses docker. Right now, I am using the Dockerfile as a reference to create the plan but I am running into issues. For some background, the application currently builds an api docker image and an app docker image. Docker-compose is used to marry the two images when running locally.

Attached are the images of the app plan.sh, Dockerfile, and error on build.

I am unsure what extra information is relevant to this issue, so please let me know if I can clarify or share additional info!

@nealajpatel can you copy your plan into a text snippet and post it here as a codeblock? Iā€™d like to see if I cant reproduce your error!

pkg_name=app
pkg_origin=npatel
pkg_version="0.1.0"
pkg_svc_user=app
pkg_deps=(core/node8 core/coreutils)
pkg_build_deps=(
  core/node8
  core/gcc
  core/python2
  core/make
  core/rsync
)
pkg_exports=(
  [port]=http.listen_port
)
pkg_expose=(port)

do_begin() {
  export SSL_CERT_FILE=/src/tls-ca-bundle.pem
}

# build step clears the previous dist dir, installs npm packages, and builds the angular app
do_build() {
  pushd $HAB_CACHE_SRC_PATH/${pkg_dirname} > /dev/app
  rsync -a  $PLAN_CONTEXT/../ --exclude='/node_modules' --exclude='*.env' ./
  pushd ./ > /dev/svr
  npm install -g http-server --silent &&\
  npm install -g node-sass --silent
  fix_node_module_bins
  popd > /dev/svr && popd > /dev/app
}

# install copies generated files / packages to the prefix directory
do_install() {
  useradd -ms /bin/bash $SVC_USER &&\
  mkdir -p "${pkg_prefix}/app"
  cp -vr "$HAB_CACHE_SRC_PATH/${pkg_dirname}" "${pkg_prefix}/app/"
  node-sass --output-style expanded css/sass/main.scss> css/style.css
#  chown -R $SVC_USER .
}

do_setup_environment() {
  set_buildtime_env -f http_proxy "http://10.127.40.152:8000"
  set_buildtime_env -f https_proxy "http://10.127.40.152:8000"
  echo "finished setting up env"
}

fix_node_module_bins() {
    echo "fixing node modules"
  for b in node_modules/.bin/*; do
    fix_interpreter $(readlink -f -n $b) core/coreutils bin/env
  done
}

Itā€™s hard to say, Iā€™m not a pro at node.js there are some other folks around who might have more luck, but from what I can tell it looks like just a permissions issue which is pretty strange. If you remove the pkg_svc_user line in your plan do you get the same permissions failure?

It appears to give the same error with and without the pkg_svc_user. I noticed in the Dockerfile that a USER_NAME is created, and was trying to bring that over to the plan.sh. Is the pkg_svc_user the right approach to take?

Nope, pkg_svc_user doesnā€™t do any user creation itā€™s just determines which user the service will run as at runtime. Iā€™ll see if I canā€™t get someone with some more node.js experience to take a look at this.

Is there a .gitignore in the directory that you rsync across? Because, strangely enough, that can cause problems.

The only files you want to copy over before you do any npm install is the package.json / package-lock.json or yarn files as necessary:

  • Copy package*.json
  • npm install
  • Copy app over

Try that and see how you go.

1 Like

There is no .gitignore in the directory I am rsyncing. I have narrowed down the issue to node-sass. For some reason when I run ā€œnpm install -g --unsafe-perm node-sassā€ and then try to execute a node-sass command it says the command is not found. This issue occurs whether or not node-sass is installed globally. Any ideas where to go from here?

Edit: I posted the updated plan.sh, but I think it needs to be approved.

pkg_name=app
pkg_origin=npatel
pkg_version="0.1.0"
pkg_deps=(core/node8 core/coreutils)
pkg_build_deps=(
  core/node6
  core/sassc
  core/gcc
  core/python2
  core/make
  core/rsync
)
pkg_exports=(
  [port]=http.listen_port
)
pkg_expose=(port)

do_begin() {
  export SSL_CERT_FILE=/src/tls-ca-bundle.pem
}

# build step clears the previous dist dir, installs npm packages, and builds the angular app
do_build() {
  pushd $HAB_CACHE_SRC_PATH/${pkg_dirname} > /dev/app
  rsync -a  $PLAN_CONTEXT/../ --exclude='/node_modules' --exclude='*.env' ./
  pushd ./ > /dev/svr
  npm install --production
  fix_node_module_bins
  node-sass --output-style expanded css/sass/main.scss> css/style.css
  popd > /dev/svr && popd > /dev/app
}

# install copies generated files / packages to the prefix directory
do_install() {
  mkdir -p "${pkg_prefix}/app"
  cp -vr "$HAB_CACHE_SRC_PATH/${pkg_dirname}" "${pkg_prefix}/app/"
}

do_setup_environment() {
  set_buildtime_env -f http_proxy "http://10.127.40.152:8000"
  set_buildtime_env -f https_proxy "http://10.127.40.152:8000"
  echo "finished setting up env"
}

fix_node_module_bins() {
    echo "fixing node modules"
  for b in node_modules/.bin/*; do
    fix_interpreter $(readlink -f -n $b) core/coreutils bin/env
  done
}

One of the things I notice at the beginning of your error output is the error ā€œuser ā€œundefinedā€ d9oes not have permission to access the dev dirā€ - so it definitely looks like the app user is not being created - let me dig into this a bit.

Can you do me a favor and change this line:

useradd -ms /bin/bash user-name &&\

to

useradd -ms /bin/bash app &&\

Iā€™d like to see what it says if you do that

1 Like

The error I am getting now is "invalid user: ā€˜appā€™ " on the chown.

Alright, well at least the user is being created, so that might be progress (I often consider a different error message to be progress). Taking another lookā€¦

Can you next try changing this:

chown -R user-name "${pkg_prefix}/app"

To:

chown -R app "${pkg_prefix}/app"

Thank you for the suggestion, but I already have that change in my current plan.

I forgot to mention that I commented out the ā€œnode-sassā€ command in do_build() to test the switch from ā€œuser-nameā€ to ā€œappā€.

When it is uncommented, the node-sass command in do_build() cannot be executed due to a command not found error.

I would think that a global install of node-sass would resolve this issue, but it does not seem to be that way.
I have tried running fix_node_modules before and after the node-sass command with no luck.
I have also attempted not globally installing the package, and the same error is produced.

EDIT: I have updated the post with my plan.sh to reflect my most recent changes.

Thank your for the update! I will try to reproduce this locally today and see what I can find :slight_smile:

Coming in late to the party here. There are a few things I see off the bat that this plan should change:

  • You shouldnā€™t have npm install -g, anything you have in there should be in your package.json
  • Youā€™ll probably want to include core/sassc
  • The chown and useradd shouldnā€™t be there. If you want to run your app as a user, set: pkg_svc_user and pkg_svc_group

Thanks for the input! I went ahead and removed any ownership changes, and removed the unecessary npm installs.
I added core/sassc to the dependencies, but I am still seeing a ā€œnode-sass: command not foundā€ during the build.

I have updated my plan.sh in the post above to reflect my most recent changes.

Do you have a line like this in your package.json?

Yes, below is a copy of the package.json
{
  "name": "bermuda-automative",
  "version": "1.0.0",
  "description": "",
  "main": "index.html",
  "dependencies": {
    "body-parser": "^1.17.2",
    "d3": "^4.9.1",
    "express": "^4.15.3",
    "http-server": "^0.11.1",
    "jquery": "^3.1.1",
    "lodash": "^4.17.4",
    "moment": "^2.18.1",
    "node-sass": "^4.5.3",
    "node-static": "^0.7.9",
    "pdfmake": "^0.1.32",
    "requirejs": "^2.3.2",
    "save-svg-as-png": "^1.2.0"
  },
  "engines": {
    "node": "6.9."
  },
  "scripts": {
    "start": "node index.js",
    "sass": "sass --style=expanded css/sass/main.scss:./css/style.css",
    "scss:dev": "sass --watch --style=expanded css/sass/main.scss:./css/style.css"
  },
  "repository": {},
  "keywords": [],
  "author": "",
  "license": "ISC"
}