Set Docker environment variables WITHOUT using "ENV"

368 views
Skip to first unread message

Ivan Ribakov

unread,
Mar 8, 2018, 12:02:37 PM3/8/18
to Packer
I'm building a docker image and using Ansible for provisioning. I would like to be able to update image's PATH variable from inside the container (I don't need these changes to take effect immediately, as long as they are commited to an image and are available later). Is there a way to do it?

To illustrate my point:

$ docker run -itd --name test centos:6 
$ docker exec -it test /bin/bash
[root@006a9c3195b6 /]# echo 'echo SUCCESS' > /root/test.sh
[root@006a9c3195b6 /]# chmod +x /root/test.sh
[root@006a9c3195b6 /]# echo 'export PATH=/root:$PATH' > /etc/profile.d/my_settings.sh
[root@006a9c3195b6 /]# echo 'PATH=/root:$PATH' > /etc/environment
[root@006a9c3195b6 /]# echo 'export PATH=/root:$PATH' > ~/.bashrc
[root@006a9c3195b6 /]# exit
$ docker commit test test-image:1
$ docker exec -it test-image:1 test.sh
OCI runtime exec failed: exec failed: container_linux.go:296: starting container process caused "exec: \"test.sh\": executable file not found in $PATH": unknown

However, following works:

$ docker run -it test-image:1 /bin/bash
[root@8f821c7b9b82 /]# test.sh 
SUCCESS

Any ideas as to what I'm doing wrong?

Alvaro Miranda Aguilera

unread,
Mar 8, 2018, 1:00:02 PM3/8/18
to packe...@googlegroups.com
where packer fits here ?

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/e4cb8df6-fe4e-48bc-9064-64ce4cabae79%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Alvaro

Rickard von Essen

unread,
Mar 8, 2018, 1:33:20 PM3/8/18
to packe...@googlegroups.com

Ivan Ribakov

unread,
Mar 8, 2018, 4:05:52 PM3/8/18
to Packer
 Alvaro: 

I posted this question here because of the Packer way to build Docker images, i.e. without using Dockerfiles. I'm hoping that this place has higher concentration of people who work with Docker without Dockerfiles and as such can help me with my query.
In case you are wondering, earlier snippet can be replicated using following Packer template
P.S. last line in the snippet should be "docker run" not "docker exec".

Rickard:

Thanks again for taking your time today to help me out. Another answer on my SO question already suggested use of "--changes": it is definitely the winning option for me at the moment.

However, I would like to use this opportunity to ask if after all there is an alternative to "--changes" that can be performed by one of provisioners. Reason I'm looking for an alternative is that I'm using Ansible playbook to provision both Docker and Vagrant images. My only option for now to set PATH correctly in Docker seems to be to duplicate PATH configuration inside Packer template and I don't like the idea of repeating the configuration for obvious reasons.

I appreciate that this is not an issue with Packer at all, but as mentioned earlier, seeing how this forum has a high concentration of people who work with Docker without Dockerfiles I was hoping that someone might know a better answer (and if not, could perhaps at least hint where the limitation lies).

To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool...@googlegroups.com.



--
Alvaro

--
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.

Ivan Ribakov

unread,
Mar 9, 2018, 6:19:00 AM3/9/18
to Packer
I have updated docker builder config with
"changes": ["ENV PATH=...:${PATH}"]

hoping that would solve my problem, so now my docker builder looks like this:

"builders": [{
 
"type": "docker",
 
"image": "{{user `docker_source_repo`}}:{{user `docker_source_tag`}}",
 
"export_path": "{{user `build_dir`}}/docker-{{user `docker_target_repo`}}/{{user `docker_target_tag`}}.tar",
 
"run_command": [
   
"-d",
   
"-i",
   
"-t",
   
"--name",
   
"{{user `container_name`}}",
   
"--privileged",
   
"{{.Image}}",
   
"/bin/bash"
 
],
 
"changes": [
   
"ENV PATH=/usr/local/texlive/2018/bin/x86_64-linux/:${PATH}"
 
]
}],


But when I pull resulting image from Docker Hub, create a container and run 
docker container inspect <ID>

I get following:

...
"Config": {
   
"Hostname": "82e7c913d455",
   
"Domainname": "",
   
"User": "",
   
"AttachStdin": true,
   
"AttachStdout": true,
   
"AttachStderr": true,
   
"Tty": true,
   
"OpenStdin": true,
   
"StdinOnce": true,
   
"Env": null,
   
"Cmd": [
       
"<my_binary_here>",
       
"-p",
       
".",
       
"-o",
       
"/tmp"
   
],
   
"Image": "<my_image_here>",
   
"Volumes": null,
   
"WorkingDir": "",
   
"Entrypoint": null,
   
"OnBuild": null,
   
"Labels": {}
},
...

Env is null. So running 
docker run -it <my_image_here> <my_binary_here>

still fails because binary is not on the PATH.

Any ideas what I might have done wrong?

Rickard von Essen

unread,
Mar 9, 2018, 6:35:38 AM3/9/18
to packe...@googlegroups.com
However, I would like to use this opportunity to ask if after all there is an alternative to "--changes" that can be performed by one of provisioners. Reason I'm looking for an alternative is that I'm using Ansible playbook to provision both Docker and Vagrant images. My only option for now to set PATH correctly in Docker seems to be to duplicate PATH configuration inside Packer template and I don't like the idea of repeating the configuration for obvious reasons.

You have two options:

a) Either you use the same provisioner and configure bashrc or profile but then you have to invoke them the same, i.e. docker exec <container> bash (-l) -c test.sh

or

b) Use bashrc or profile in the vagrant image since that is invoking bash when you ssh into it and use changes ENV PATH=... to add test.sh to the path in the docker image where bash is not automatically invoked by docker exec.
  

To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/60a0484d-9850-4046-82b0-0b09152b6447%40googlegroups.com.

Rickard von Essen

unread,
Mar 9, 2018, 6:51:27 AM3/9/18
to packe...@googlegroups.com
This works for me:

{
  "builders": [{
  "type": "docker",
  "image": "centos:6",
  "commit": true,
  "run_command": [
    "-d",
    "-i",
    "-t",
    "--privileged",
    "{{.Image}}",
    "/bin/bash"
  ],
  "changes": [
    "ENV PATH=/usr/local/texlive/2018/bin/x86_64-linux/:${PATH}"
  ]
}]
}

$ packer build docker.json
[...]
==> Builds finished. The artifacts of successful builds are:
--> docker: Imported Docker image: sha256:134ccd664a094ef9eea184650fb24dbdad4dc59b44e81ce131d2f126e8638772

$ docker inspect sha256:134ccd664a094ef9eea184650fb24dbdad4dc59b44e81ce131d2f126e8638772 -f "{{.Config.Env}}"
[PATH=/usr/local/texlive/2018/bin/x86_64-linux/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin]


Ivan Ribakov

unread,
Mar 9, 2018, 8:50:55 AM3/9/18
to Packer
Thanks Rickard, I understand now why Env was null before - I was exporting the container instead of committing it by builder.

I have changed my builder to the following:

"builders": [{
 
"type": "docker",
 
"image": "{{user `docker_source_repo`}}:{{user `docker_source_tag`}}",

 
"commit": true,
   
"run_command": [
   
"-d",
   
"-i",
   
"-t",

   
"--name",
   
"{{user `container_name`}}",

   
"--privileged",
   
"{{.Image}}",
   
"/bin/bash"
 
],
 
"changes": [
   
"ENV PATH=/usr/local/texlive/2018/bin/x86_64-linux/:${PATH}"
 
]
}],


However, now, "docker inspect" shows following:

"Env": [
   
"PATH=/usr/local/texlive/2018/bin/x86_64-linux/:"
],

I took stripped down template from above:
{
 
"builders": [{
   
"type": "docker",
   
"image": "centos:6",
   
"commit": true,
   
"run_command": [
     
"-d",
     
"-i",
     
"-t",
     
"--privileged",
     
"{{.Image}}",
     
"/bin/bash"
   
],
   
"changes": [
     
"ENV PATH=/usr/local/texlive/2018/bin/x86_64-linux/:${PATH}"
   
]
 
}]
}

and ran:

$ packer build docker.json
[...]
==> Builds finished. The artifacts of successful builds are:
--> docker: Imported Docker image: sha256:665e385658c14cf0f19fdd584fcbbd18df0f50e281896cc6cae470ec2eee085a


$ docker inspect sha256
:665e385658c14cf0f19fdd584fcbbd18df0f50e281896cc6cae470ec2eee085a -f "{{.Config.Env}}"
[PATH=/usr/local/texlive/2018/bin/x86_64-linux/:]

But as you can see ${PATH} from "changes" is not being resolved (or it is actually empty for some reason).

I guess I'm missing something extra this time but I'm not sure where to start looking. Any ideas?

Rickard von Essen

unread,
Mar 10, 2018, 1:48:40 AM3/10/18
to packe...@googlegroups.com
 I was exporting the container instead of committing it by builder.

Exporting only dumps the filesystem into a tar, no metadata is included. 

But as you can see ${PATH} from "changes" is not being resolved (or it is actually empty for some reason).

Seems to work for me:

$ docker run -it sha256:134ccd664a094ef9eea184650fb24dbdad4dc59b44e81ce131d2f126e8638772 bash -c 'echo $PATH'
/usr/local/texlive/2018/bin/x86_64-linux/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/1a1ec019-349e-42ef-bef8-719e588f4349%40googlegroups.com.

Ivan Ribakov

unread,
Mar 10, 2018, 9:03:35 AM3/10/18
to Packer
What OS, Docker and Packer versions are you running on? 

Rickard von Essen

unread,
Mar 10, 2018, 2:40:25 PM3/10/18
to packe...@googlegroups.com
Packer v1.1.3-dev (567b566c2+CHANGES)
macOS High Sierra 10.13.4 (17E160e)

Docker Engine: 18.03.0-ce-rc1

Docker for Mac Version 18.03.0-ce-rc1-mac54 (23022)

Which gives: Linux 3a126b636a9a 4.9.75-linuxkit-aufs #1 SMP Tue Jan 9 10:58:17 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux



To unsubscribe from this group and stop receiving emails from it, send an email to packer-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/packer-tool/67c3c116-3480-40f7-a018-ace8c10c5e9c%40googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages