2

So I am creating a docker image with packer. The template defines the provisioners and builders etc. The builder section looks like this:

{
    "builders": [
        {
            "type": "docker",
            "image": "ubuntu:latest",
            "export_path": "image.tar",
            "changes": [
                "USER test",
                "WORKDIR /tmp",
                "VOLUME /data",
                "EXPOSE 21 22",
                "CMD sudo myprogram"
            ]
        }
    ]
}

When running packer against the template, the output is an image.tar file. I can then import it: docker import image.tar. And then I start like this docker run -p 21:21 -it --rm 52c0b0362362 bash.

I want whenever the image is started that automatically sudo myprogram is executed. However it does not seem to work, even tho the template validated successfully. I also tried instead of specifying CMD sudo myprogram to set it as entrypoint like so: ENTRYPOINT sudo myprogram. Neither worked. How can I make sure whenever my image is started, this command is automatically executed? It must be run as root/with sudo, that's important.

  • What does the log indicate? Why would you like to run sudo? Could you add a sample program so people could reproduce the issue? Please add the complete packer.json file and describe the exact commands that you issued, e.g. packer build file.json – 030 2 days ago
  • Instead of CMD you could try ENTRYPOINT. There is a StackOverflow post that describes the difference. – 030 2 days ago
  • 1
    The problem has nothing to do with Packer. You should try to run docker run -it -p 21:21 --rm myimage without the bash command. You should see some output. – Hedi Nasr yesterday
  • I'm pretty sure the docker image doesn't have sudo permissions. When i've pulled down the image, running anything with sudo produces an error that doesn't recognize sudo. try removing sudo and and see if that corrects the issue. also you might be able to get sudo back by installing it. context here – xtreampb yesterday
  • Could you add the relevant logging to the Q&A? – 030 3 hours ago
1

According to @earcam one can run a docker inspect <id> to check whether an image contains a CMD. If one builds a docker image using the provided packer snippet in the Q&A, imports it and subsequently runs a docker inspect 1127c20077ef | grep -i cmd, the CMD has not been set:

"Cmd": null,

According to @Greendrake when has to set the context when importing an image by defining --change 'CMD ["somecommand"]'

docker import --change 'CMD ["bash"]' image.tar

and subsequently issue an:

docker inspect b604fa244510 | grep -A3 -i cmd
        "Cmd": [
            "bash"
        ],

Once the image has been imported and docker run is issued a shell is opened:

docker run -it b604fa244510
root@69df4b3f48a2:/#

instead of

docker run -p 21:21 -it --rm 02163a32f3b7
docker: Error response from daemon: No command specified.
See 'docker run --help'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.