Dockerfile -> plan.sh issues


#1

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!


#2

@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!


#3
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
}

#4

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?


#5

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?


#6

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.


#7

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.


#8

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.


#10
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
}

#11

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.


#12

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


#13

The error I am getting now is "invalid user: ‘app’ " on the chown.


#14

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…


#15

Can you next try changing this:

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

To:

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

#16

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.


#17

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


#18

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

#19

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.


#20

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


#21
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"
}