Answers: <p>According to <a href="https://serverfault.com/questions/757210/no-command-specified-from-re-imported-docker-image-container#comment1247323_797619">@earcam</a> one can run a <code>docker inspect &lt;id&gt;</code> to check whether an image contains a CMD. If one builds a docker image using the provided packer snippet in the Q&amp;A, imports it and subsequently runs a <code>docker inspect 1127c20077ef | grep -i cmd</code>, the CMD has not been set:</p>
  
  <pre><code>"Cmd": null,
  </code></pre>
  
  <p>According to <a href="https://serverfault.com/a/797619/215599">@Greendrake</a> when has to set the context when importing an image by defining <code>--change 'CMD ["somecommand"]'</code></p>
  
  <pre><code>docker import --change 'CMD ["bash"]' image.tar
  </code></pre>
  
  <p>and subsequently issue an:</p>
  
  <pre><code>docker inspect b604fa244510 | grep -A3 -i cmd
          "Cmd": [
              "bash"
          ],
  </code></pre>
  
  <p>Once the image has been imported and docker run is issued a shell is opened:</p>
  
  <pre><code>docker run -it b604fa244510
  root@69df4b3f48a2:/#
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>docker run -p 21:21 -it --rm 02163a32f3b7
  docker: Error response from daemon: No command specified.
  See 'docker run --help'.
  </code></pre>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-07T20:51:39.123Z
Answers: 2019-12-07T20:51:39.123Z
Answers: 030
Answers: <blockquote>
    <p>And then I start like this <code>docker run -p 21:21 -it --rm 52c0b0362362 bash</code></p>
  </blockquote>
  
  <p>This would not work by design. When you run the image with the command above you are instructing Docker to overwrite the <code>CMD</code> instruction from your Dockerfile and therefore telling it to execute <code>bash</code> instead of <code>sudo myprogram</code>. It doesn't matter what you have set in the <code>CMD</code> instruction - it would never be executed.</p>
  
  <p>One way you could go around this would be to use <a href="https://docs.docker.com/engine/reference/builder/#entrypoint" rel="noreferrer"><code>ENTRYPOINT</code></a> as others have suggested. This will make <code>sudo myprogram</code> the default command and then <code>CMD</code> can act as "additional arguments" to the default command (<code>sudo myprogram</code>). </p>
  
  <p>Here's a working example:</p>
  
  <pre><code># 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
  </code></pre>
  
  <p>The other solution would be to keep the <code>CMD [\"sudo myprogram\"]</code> instruction but to run the image without adding a command at the end: <code>docker run -it --rm HASH_ID</code>. </p>
  
Answers: CC BY-SA 4.0
Answers: 2019-12-08T20:57:17.317Z
Answers: 2019-12-08T20:57:17.317Z
Answers: tftd
Body: <p>So I am creating a docker image with packer.
  The template defines the provisioners and builders etc. 
  The builder section looks like this:</p>
  
  <pre><code>{
      "builders": [
          {
              "type": "docker",
              "image": "ubuntu:latest",
              "export_path": "image.tar",
              "changes": [
                  "USER test",
                  "WORKDIR /tmp",
                  "VOLUME /data",
                  "EXPOSE 21 22",
                  "CMD sudo myprogram"
              ]
          }
      ]
  }
  </code></pre>
  
  <p>When running packer against the template, the output is an <code>image.tar</code> file. 
  I can then import it: <code>docker import image.tar</code>.
  And then I start like this <code>docker run -p 21:21 -it --rm 52c0b0362362 bash</code>.</p>
  
  <p>I want whenever the image is started that automatically <code>sudo myprogram</code> is executed. However it does not seem to work, even tho the template validated successfully. I also tried instead of specifying <code>CMD sudo myprogram</code> to set it as entrypoint like so: <code>ENTRYPOINT sudo myprogram</code>.
  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.</p>
  
Comments: CC BY-SA 4.0
Comments: 2019-12-05T15:48:35.677Z
Comments: 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`
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2019-12-05T15:58:04.970Z
Comments: Instead of `CMD` you could try `ENTRYPOINT`. There is a [StackOverflow post that describes the difference](https://stackoverflow.com/a/21564990/2777965).
Comments: 030
Comments: CC BY-SA 4.0
Comments: 2019-12-06T08:58:24.340Z
Comments: 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.
Comments: Hedi Nasr
Comments: CC BY-SA 4.0
Comments: 2019-12-06T19:28:17.017Z
Comments: 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](https://github.com/tianon/docker-brew-ubuntu-core/issues/48#issuecomment-215522746)
Comments: xtreampb
Comments: CC BY-SA 4.0
Comments: 2019-12-07T19:36:07.113Z
Comments: Could you add the relevant logging to the Q&A?
Comments: 030
Tags: <docker><packer>
Title: How to run a CMD in a docker container that was created using Packer?
UserDisplayName: Kyu96
ThreadText: # How to run a CMD in a docker container that was created using Packer?
  
  (Asked by: username_0 on 2019-12-02)
  
  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.
  
  **Tags:** docker, packer
  
  > **Comments:**
  > **username_1**: 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`  
  > **username_1**: Instead of `CMD` you could try `ENTRYPOINT`. There is a [StackOverflow post that describes the difference](https://stackoverflow.com/a/21564990/2777965).  
  > **username_2**: 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.  
  > **username_3**: 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](https://github.com/tianon/docker-brew-ubuntu-core/issues/48#issuecomment-215522746)  
  > **username_1**: Could you add the relevant logging to the Q&A?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_4 on 2019-12-08. Score: 5 | Accepted answer
  
  > 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`](https://docs.docker.com/engine/reference/builder/#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`.
  
  
  ---
  
  ### Answer by username_1 on 2019-12-07. Score: 1
  
  According to [@earcam](https://serverfault.com/questions/757210/no-command-specified-from-re-imported-docker-image-container#comment1247323_797619) 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](https://serverfault.com/a/797619/215599) 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'.
  
  ```
  
  
  ---
  
  