Answers: <p>When you replace pid 1 with a new executable, rather than forking it as a child pid, you reduce overhead (slightly). More importantly, you get signals directly to your application instead of having them passed to the shell running as pid 1. By default, the shell running in pid 1 will ignore the SIGTERM sent by docker to gracefully stop your application. After 10 seconds of no response, docker will send a SIGKILL which will immediately terminate the shell and child processes without giving them a chance to drain network connections or finish writing to any files. This can result in things like database corruption depending on what the app is inside the container.</p>
  
  <p>Even when this doesn't apply to your application (if there's no state), you can save up to 10 seconds every time you stop the container by switching to the exec syntax (assuming your application exits when it receives a SIGTERM). Therefore the exec syntax is the one I prefer for both data safety and speed.</p>
  
Answers: CC BY-SA 4.0
Answers: 2019-01-21T16:22:15.920Z
Answers: 2019-01-21T16:22:15.920Z
Answers: BMitch
Body: <p>In Docker, when chaining several startup commands (CMD) in shell form, is there any advantage in using the shell's <a href="http://man7.org/linux/man-pages/man1/exec.1p.html" rel="nofollow noreferrer">exec</a> feature to replace it with the last command of the chain instead of just running it as a child process of the startup shell?</p>
  
  <p>Here is an example:</p>
  
  <pre><code>CMD npm install &amp;&amp; npm start
  </code></pre>
  
  <p>versus:</p>
  
  <pre><code>CMD npm install &amp;&amp; exec npm start
  </code></pre>
  
  <p>I think in the latter example, the <code>npm start</code> command will replace the shell as the PID 1 process. But I do not fully understand which advantages or disadvantages this would have.</p>
  
Tags: <docker>
Title: In Docker, is there any advantage or disadvantage in using 'CMD cmd1 && cmd2' versus 'CMD cmd1 && exec cmd2'?
UserDisplayName: Sylvain Leroux
ThreadText: # In Docker, is there any advantage or disadvantage in using 'CMD cmd1 && cmd2' versus 'CMD cmd1 && exec cmd2'?
  
  (Asked by: username_0 on 2019-01-21)
  
  In Docker, when chaining several startup commands (CMD) in shell form, is there any advantage in using the shell's [exec](http://man7.org/linux/man-pages/man1/exec.1p.html) feature to replace it with the last command of the chain instead of just running it as a child process of the startup shell?
  
  Here is an example:
  
  ```
  CMD npm install && npm start
  
  ```
  
  versus:
  
  ```
  CMD npm install && exec npm start
  
  ```
  
  I think in the latter example, the `npm start` command will replace the shell as the PID 1 process. But I do not fully understand which advantages or disadvantages this would have.
  
  **Tags:** docker
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2019-01-21. Score: 6 | Accepted answer
  
  When you replace pid 1 with a new executable, rather than forking it as a child pid, you reduce overhead (slightly). More importantly, you get signals directly to your application instead of having them passed to the shell running as pid 1. By default, the shell running in pid 1 will ignore the SIGTERM sent by docker to gracefully stop your application. After 10 seconds of no response, docker will send a SIGKILL which will immediately terminate the shell and child processes without giving them a chance to drain network connections or finish writing to any files. This can result in things like database corruption depending on what the app is inside the container.
  
  Even when this doesn't apply to your application (if there's no state), you can save up to 10 seconds every time you stop the container by switching to the exec syntax (assuming your application exits when it receives a SIGTERM). Therefore the exec syntax is the one I prefer for both data safety and speed.
  
  
  ---
  
  