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
    Dec 5, 2019 at 15:48
  • Instead of CMD you could try ENTRYPOINT. There is a StackOverflow post that describes the difference.
    – 030
    Dec 5, 2019 at 15:58
  • 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
    Dec 6, 2019 at 8:58
  • 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
    Dec 6, 2019 at 19:28
  • Could you add the relevant logging to the Q&A?
    – 030
    Dec 7, 2019 at 19:36

2 Answers 2


And then I start like this docker run -p 21:21 -it --rm 52c0b0362362 bash

This would not work by design. When you run the image with the command above you are instructing Docker to overwrite the CMD instruction from your Dockerfile and therefore telling it to execute bash instead of sudo myprogram. It doesn't matter what you have set in the CMD instruction - it would never be executed.

One way you could go around this would be to use ENTRYPOINT as others have suggested. This will make sudo myprogram the default command and then CMD can act as "additional arguments" to the default command (sudo myprogram).

Here's a working example:

# build.json
    "builders": [
            "type": "docker",
            "image": "ubuntu:latest",
            "commit": true,
            "changes": [
                "ONBUILD RUN apt-get -q update",
                "ONBUILD RUN apt-get install -y -q htop",
                "ENTRYPOINT [\"top\"]"

# /packer build ./build.json

# run top with delay 3 sec
# docker run --rm -it sha256:hash

# run top with delay 1 sec
# docker run --rm -it sha256:hash -d 1

The other solution would be to keep the CMD [\"sudo myprogram\"] instruction but to run the image without adding a command at the end: docker run -it --rm HASH_ID.


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": [

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

docker run -it b604fa244510

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 and acknowledge you have read our privacy policy.

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