How to run a CMD in a docker container that was created using Packer?
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.