HiveBrain v1.2.0
Get Started
← Back to all entries
snippetdockerMinor

How to run a CMD in a docker container that was created using Packer?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
containerpackerdockercreatedthatusinghowwascmdrun

Problem

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.

Solution

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.

Code Snippets

# 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

Context

StackExchange DevOps Q#9991, answer score: 5

Revisions (0)

No revisions yet.