Docker image using Packer - non-root user

45 views
Skip to first unread message

Alterego

unread,
Jul 11, 2019, 2:45:33 AM7/11/19
to Packer
Hello
I'm new to packer, I've got the following packer script. I would like to create a docker ubuntu image with a non root user (ubuntu lets say). Further I would like add this user into the sudoers group.
I'm trying to adduser using shell provisioner but the build is failing with an error (note: I tried all flags possible but it's still giving me an error). 
How can I achieve this with Packer/Docker?

==> docker: Usage: useradd [options] LOGIN
==> docker:        useradd -D
==> docker:        useradd -D [options]
==> docker:
==> docker: Options:
==> docker:   -b, --base-dir BASE_DIR       base directory for the home directory of the
==> docker:                                 new account
==> docker:   -c, --comment COMMENT         GECOS field of the new account
==> docker:   -d, --home-dir HOME_DIR       home directory of the new account
==> docker:   -D, --defaults                print or change default useradd configuration
==> docker:   -e, --expiredate EXPIRE_DATE  expiration date of the new account
==> docker:   -f, --inactive INACTIVE       password inactivity period of the new account
==> docker:   -g, --gid GROUP               name or ID of the primary group of the new
==> docker:                                 account
==> docker:   -G, --groups GROUPS           list of supplementary groups of the new
==> docker:                                 account
==> docker:   -h, --help                    display this help message and exit
==> docker:   -k, --skel SKEL_DIR           use this alternative skeleton directory
==> docker:   -K, --key KEY=VALUE           override /etc/login.defs defaults
==> docker:   -l, --no-log-init             do not add the user to the lastlog and
==> docker:                                 faillog databases
==> docker:   -m, --create-home             create the user's home directory
==> docker:   -M, --no-create-home          do not create the user'
s home directory
==> docker:   -N, --no-user-group           do not create a group with the same name as
==> docker:                                 the user
==> docker:   -o, --non-unique              allow to create users with duplicate
==> docker:                                 (non-unique) UID
==> docker:   -p, --password PASSWORD       encrypted password of the new account
==> docker:   -r, --system                  create a system account
==> docker:   -R, --root CHROOT_DIR         directory to chroot into
==> docker:   -s, --shell SHELL             login shell of the new account
==> docker:   -u, --uid UID                 user ID of the new account
==> docker:   -U, --user-group              create a group with the same name as the user
==> docker:   -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping
==> docker:       --extrausers              Use the extra users database
==> docker:
==> docker: Killing the container: 2dc2bd524c302eaadb1f5e12ea72a19023ae6c02250138440f5191cf2f76dc43
Build 'docker' errored: Script exited with non-zero exit status: 2.Allowed exit codes are: [0]

Here's my simple json. file.

{
 
"variables": {
   
"repository": "ubuntu",
   
"tag": "1.0",
   
"root_password": null
 
},
 
"builders": [{
   
"type": "docker",
   
"author": "",
   
"image": "ubuntu:latest",
   
"commit": true,
   
"changes": [
     
"USER ubuntu"
   
]


 
}],
 
"provisioners": [
   
{
     
"type": "shell",
     
"inline": [
       
"apt-get update -y && apt-get install -y sudo",
       
"sudo apt-get dist-upgrade -y && sudo apt-get autoremove -y && sudo apt clean -y && sudo apt-get install -y sudo bash-completion curl net-tools iputils-ping git python iptables",
       
"sleep 15",
       
"sudo groupadd -r ubuntu -g 901",
       
"sudo useradd -D -u 901 -r -g ubuntu",
       
"cd /tmp"
     
     
]


}],


 
"post-processors": [
   
{
     
"type": "docker-tag",
     
"repository": "{{user `repository`}}",
     
"tag": "{{user `tag`}}"
   
},
   
{
 
"type": "shell-local",
 
"inline": ["echo foo"]
 
}
 
]
}


Thanks




Rickard von Essen

unread,
Jul 11, 2019, 5:30:43 AM7/11/19
to packe...@googlegroups.com
useradd -D -u 901 -r -g ubuntu

Is incorrect, remove the -D. 

And it's a bit weird to make the ubuntu users system user, and give it a gui/uid < 1000.

Another style thing, it's a bit awkward to use sudo in front of every command when this is running as root. The only real reason to include sudo in a docker image is because there are some legacy scripts/software that uses it. 

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/packer/issues
IRC: #packer-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Packer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/a6c543a5-1a35-40cd-9d15-2f62ffc0a6b3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alterego

unread,
Jul 11, 2019, 8:14:57 AM7/11/19
to Packer
Awesome it works.
{
 "variables": {
   "repository": "ubuntu",
   "tag": "1.0",
   "root_password": null
 },
 "builders": [{
   "type": "docker",
   "author": "",
   "image": "ubuntu:latest",
   "commit": true,
   "changes": [
    "USER ubuntu"
   ]

  }],
 "provisioners": [
  {
    "type": "shell",
    "inline": [
      "apt-get update -y && apt-get install -y sudo",
      "apt-get dist-upgrade -y && apt-get autoremove -y && apt clean -y && apt-get install -y bash-completion curl net-tools iputils-ping git python iptables",
      "sleep 15",
      "groupadd -r ubuntu",
      "useradd -r -g ubuntu ubuntu",
Reply all
Reply to author
Forward
0 new messages