Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Azure: Container did not start within expected time (WebApp)

I'm getting the following error when trying to deploy a webapp:

ERROR - Container XXX_0 for site XXX did not start within expected time limit. Elapsed time = 1800.4463925 sec

I'm trying to deploy a node app. Using automatic deployment with a .deployment file. The .deployment file looks as follows:

# 1. KuduSync
if [[ "$IN_PLACE_DEPLOYMENT" -ne "1" ]]; then
  "$KUDU_SYNC_CMD" -v 50 -f "$DEPLOYMENT_SOURCE" -t "$DEPLOYMENT_TARGET" -n "$NEXT_MANIFEST_PATH" -p "$PREVIOUS_MANIFEST_PATH" -i ".git;.hg;
.deployment;deploy.sh"
  exitWithMessageOnError "Kudu Sync failed"
fi

# 2. Select node version
selectNodeVersion

# 3. Install npm packages for root directory
if [ -e "$DEPLOYMENT_TARGET/package.json" ]; then
  cd "$DEPLOYMENT_TARGET"
  # echo "Running $NPM_CMD install --production for root directory"
  # eval $NPM_CMD install --production
  echo "Running $NPM_CMD install --production for root directory"
  eval $NPM_CMD install
  exitWithMessageOnError "npm failed"
  ##################
  echo Building App...
  eval $NPM_CMD run build
  ##################
  echo Starting App...
  # eval $NPM_CMD run start
  # cd - > /dev/null
fi

#####################################################################################
echo "Finished successfully."

The package.json file has the following script:

  "scripts": {
    "dev": "node server.js",
    "build": "next build",
    "start": "echo 'work!!' && NODE_ENV=production && node server.js"
  }

And server.js is as follows:

const {
  createServer
} = require('http')
const next = require('next')
const app = next({
  dev: process.env.NODE_ENV !== 'production'
})
const routes = require('./routes')
const handler = routes.getRequestHandler(app)
console.log("HEYYYY");
// Without express
app.prepare()
  .then(() => {
    console.log("Ready on Localhost:80!!!");
    createServer(handler)
      .listen(80, (err) => {
        if (err) throw err;
        console.log("Ready on Localhost:80");
      });
  })

What I've gathered from research is that:

  1. There is not enough time for the app to start
  2. The port isn't open/ doesn't respond to a ping at start up

To solve (1) I set WEBSITES_CONTAINER_START_TIME_LIMIT to 1800 (which is the max)

To solve (2) I set WEBSITES_PORT (in app setting) with a value of "80" to expose that port. (As per documentation)

Any others things I should try ?

PS the default docker log file outputs the following:

2018-10-15T14:32:59.946431939Z > [email protected] start /home/site/wwwroot
2018-10-15T14:32:59.946455839Z > echo 'work!!' && NODE_ENV=production && node server.js
2018-10-15T14:32:59.946462839Z 
2018-10-15T14:33:00.249554126Z work!!
2018-10-15T14:34:41.634101502Z HEYYYY
2018-10-15T14:35:38.838555689Z  DONE  Compiled successfully in 48099ms14:35:38
2018-10-15T14:35:38.838868291Z 
2018-10-15T14:35:39.406086808Z Ready on Localhost:80!!!
2018-10-15T14:35:39.460029162Z Ready on Localhost:80
like image 423
39fredy Avatar asked Oct 15 '18 18:10

39fredy


2 Answers

  • The recommended way to do this is to have node listen on env.PORT. This way the PORT is not hardcoded in the app and the system can pass it on via the Environment Variable.

  • If a hardcoded port has to be used, and when using App Service in the "Code" scenario, use the App Setting PORT to indicate the value in code.

  • If a hardcoded port has to be used, and when using App Service in the "Container" scenario, use the App Setting WEBSITES_PORT to indicate the value in code.

  • If the port value matches what is in this log entry: "did not start within the expected time limit. Elapsed time ", to what is in code, check the Listen IP address for the code. If the code is listening on localhost or 127.0.0.1, then it will not be accessible outside that specific container, hence, the app will fail to start. To get past this, make the app code listen on 0.0.0.0

like image 182
Jenny Lawrance Avatar answered Oct 19 '22 06:10

Jenny Lawrance


For me setting both -Port 80 and -WEBSITES_PORT 80 in Azure App Service Deploy task, App Settings section in azure devops helped. It made docker to start with port 80 instead of 8000. It is in example of app setting usage for that task.

like image 25
user999723 Avatar answered Oct 19 '22 06:10

user999723