Docker Error - "jq: error: Cannot iterate over null"
Asked Answered
L

5

45

So I'm trying to deploy a dockerfile on Elastic Beanstalk, but I can't get past this error - "jq: error: Cannot iterate over null".

Successfully built [myContainerId]
Successfully built aws_beanstalk/staging-app
[2015-01-29T10:35:59.494Z] INFO  [16343] - [CMD-AppDeploy/AppDeployStage0/AppDeployPreHook/04run.sh] : Starting activity...
[2015-01-29T10:36:05.507Z] INFO  [16343] - [CMD-AppDeploy/AppDeployStage0/AppDeployPreHook/04run.sh] : Activity execution failed, because: command failed with error code 1: /opt/elasticbeanstalk/hooks/appdeploy/pre/04run.sh
jq: error: Cannot iterate over null
Docker container quit unexpectedly after launch: Docker container quit unexpectedly on Thu Jan 29 10:36:05 UTC 2015:. Check snapshot logs for details. (Executor::NonZeroExitStatus)
at /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/executor-1.0/lib/executor/exec.rb:81:in `sh'
from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/executor-1.0/lib/executor.rb:15:in `sh'
from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/executable.rb:63:in `execute!'
from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/hook-directory-executor.rb:29:in `block (2 levels) in run!'
from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:169:in `call'
from /opt/elasticbeanstalk/lib/ruby/lib/ruby/gems/2.1.0/gems/beanstalk-core-1.1/lib/elasticbeanstalk/activity.rb:169:in `exec'

There aren't any other errors in the logs. My Docker container is successfully built, so it seems unlikely the error is coming from there.

My Dockerrun.aws.json looks like :

   {
  "AWSEBDockerrunVersion": "1",
  "Image": {
    "Name": "blah",
    "Update": "false"
  },
  "Ports": [
    {
      "ContainerPort": "8080"
    }
  ]
}

I'm banging my head against a wall with this one, nothing I change seems to affect it and googling hasn't been of any help.

Any ideas?

Lardy answered 29/1, 2015 at 11:10 Comment(0)
A
79

If others are looking for how to avoid the Cannot iterate over null error in their own jq commands, add a question mark after []. For example

echo '{
  "AWSEBDockerrunVersion": "1",
  "Image": {
    "Name": "blah",
    "Update": "false"
  },
  "Ports": [
    {
      "ContainerPort": "8080"
    }
  ]
}'|jq -c '.Volumes[]?|[.HostDirectory,.ContainerDirectory]'

where [] was replaced with []? does not display the error.

From the manual:

.[]?
    Like .[], but no errors will be output if . is not an array or object.
Aubreyaubrie answered 7/4, 2016 at 19:25 Comment(2)
This is not supported in jq 1.3; unfortunately, ubuntu 14.04 ships 1.3. anyone know a workaround for jq 1.3?Solidify
@Solidify @nobody, See answer here - since jq is a single, self contained binary, you can just overwrite your apt version with one from a newer release, such as version 1.5Hayley
P
37

The problem is that your Dockerrun.aws.json file is missing the Volumes property.

The 04pre.sh script uses the jq tool in order to query the JSON file.

Specifically, it runs the following command on your file, which yields to an error:

$ jq -c '.Volumes[] | [.HostDirectory, .ContainerDirectory]' < Dockerrun.aws.json
jq: error: Cannot iterate over null

Specifying an empty "Volumes" array should resolve the error.

Pintail answered 3/2, 2015 at 15:8 Comment(0)
C
13

Note that an Elastic Beanstalk deployment to the Docker platform can fail with the "jq: error ... Cannot iterate over null..." error message in various phases of an Elastic Beanstalk deployment, for various different reasons. This can include your application (Docker container) quitting or erroring on startup.

Although this particular reported occurrence of the problem may have been specific to a missing "Volumes" property in Dockerrun.aws.json, your problem may not be. If you get this error, then the best way to go about diagnosing the problem is to download your full EB logs and check the diagnostics in the following log file /var/log/eb-activity.log.

If the problem was caused by your application failing to startup then you'll find the error in /var/log/eb-docker/containers/eb-current-app/unexpected-quit.log.

Cashbook answered 9/3, 2016 at 11:9 Comment(2)
To give some idea of alternative causes of this error message: mine was due to running out of memory.Scuttle
This is truly the best answer. Also, for anyone new to this like me, to check logs you do this on the CLI: eb logs.Roshelle
P
1

For me I was already checking if the result was empty or not after running my jq command so I simply added try to the beginning of my query:

Before

$myJson | jq -r '.data[0].tags.version[] | select(startswith(""chart-"")) | sub(""chart-"";"""")'

After

$myJson | jq -r 'try .data[0].tags.version[] | select(startswith(""chart-"")) | sub(""chart-"";"""")'

Then if the result is empty I act accordingly.

Plumcot answered 31/10, 2023 at 12:56 Comment(0)
G
0

In my case issue was caused by ADD in Dockerfile instead of COPY instruction. Changing it fixed the issue.

Georginageorgine answered 11/9, 2017 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.