title: Dockerfile CMD instruction will exit the container just after running it
body: <p>I want to setup some configuration when my container starts, for this I am using shell scripts. But my container will exits as soon as my scripts ends, I have tried with -d flag / detached mode but It will never run in detached mode.</p>
  
  <p>Below is my Dockerfile</p>
  
  <pre><code>FROM ubuntu:14.04
  
  ADD shell.sh /usr/local/bin/shell.sh
  
  RUN chmod 777 /usr/local/bin/shell.sh
  
  CMD /usr/local/bin/shell.sh
  </code></pre>
  
  <p>Below is my shell script</p>
  
  <pre><code>#!/bin/bash
  echo Hello-docker
  </code></pre>
  
  <ol>
  <li><p>Run without any flag</p>
  
  <pre><code>docker run hello-docker
  </code></pre>
  
  <p>This will print 'Hello-docker' on my console and exits</p></li>
  <li><p>Run with -itd flags</p>
  
  <pre><code>docker run -itd hello-docker
  </code></pre>
  
  <p>and as below my console output, This time also will exits soon.  <strong>:(</strong>
  <a href="https://i.stack.imgur.com/gXaRd.png" rel="noreferrer"><img src="https://i.stack.imgur.com/gXaRd.png" alt="enter image description here"></a></p>
  
  <ul>
  <li><p>The difference I saw is in COMMAND section when I run other images command section will shows "/bin/bash" and will continue in detached mode.</p></li>
  <li><p>And when I run my image in container with shell script COMMAND section will show "/bin/sh -c /usr/loca", and Exit.</p></li>
  <li><p>I want to run container till I not stop it manually.</p></li>
  </ul></li>
  </ol>
  
  <p>EDIT:</p>
  
  <p>After adding ENTRYPOINT instruction in Dockerfile, this will not execute my shell script :(</p>
  
  <pre><code>FROM ubuntu:14.04
  
  ADD shell.sh /usr/local/bin/shell.sh
  
  RUN chmod 777 /usr/local/bin/shell.sh
  
  CMD /usr/local/bin/shell.sh
  
  ENTRYPOINT /bin/bash
  </code></pre>
  
  <p>As per docker documentation <a href="https://docs.docker.com/engine/reference/builder/#understand-how-cmd-and-entrypoint-interact" rel="noreferrer">here</a></p>
  
  <p>CMD will be overridden when running the container with alternative arguments, so If I run docker image with some arguments as below, will not execute CMD instructions. :(</p>
  
  <pre><code>sudo docker run -it --entrypoint=/bin/bash &lt;imagename&gt;
  </code></pre>