4

I just started with Packer, and want to build AMI and Docker images.

Problem is - that Docker's Ubuntu image have no sudo, thus - build failed with an error:

docker: /tmp/script_2085.sh: 3: /tmp/script_2085.sh: sudo: not found

Is there any way to add some "conditions" for a shell provisioner to run cmds with or without sudo, depending on builder?

Currently - my template have:

  ...
  "provisioners": [{
    "type": "shell",
    "inline": [
      "sleep 30",
      "sudo apt-get update",
      "sudo apt-get install -y nginx"
    ]
  }]
  ...

Add something like:

if which sudo; then sudo apt-get update && sudo apt-get install -y nginx; else apt-get update && apt-get install -y nginx; fi

Looks too weird solution... I'm sure - Packer has facility to do it in a better way.

setevoy
  • 4,374
  • 11
  • 50
  • 87

1 Answers1

3

Why don't you just install sudo in the docker box. You could do this one of two ways:


Running a script to install sudo, only on your docker image

You can use the only parameter in the provisioner to install sudo on an image:

...
"provisioners": [{
  "type": "shell",
  "inline": [
    "apt-get install sudo"
  ],
  "only": ["docker"]
},
  ... other provisioners here ...
]
...

More info in the packer docs, here, under "Run On Specific Builds".


Installing sudo at the beginning of your provision script

At the beginning of your first provision script, add the command to install sudo if it is not already installed. Use the following (taken from here):

dpkg -l | grep -qw sudo || apt-get install sudo

to check the list of installed packages and install if sudo is not present.


Personally, I'd be inclined to go for the first option, since it explicitly states that something should be done only on certain boxes. No room for confusion.

Parting notes

  • Bear in mind that your provisioners will run in the order you define them in your packer file. So make sure you install sudo in the first one.
  • You may also need to run apt-get update before you do the install.
Community
  • 1
  • 1
Tiz
  • 680
  • 5
  • 17