{"id":1321,"date":"2024-07-23T12:20:37","date_gmt":"2024-07-23T12:20:37","guid":{"rendered":"https:\/\/dockerpros.com\/?post_type=glossary&#038;p=1321"},"modified":"2024-07-23T12:23:26","modified_gmt":"2024-07-23T12:23:26","slug":"dockerfile-stopsignal","status":"publish","type":"glossary","link":"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile-stopsignal\/","title":{"rendered":"Dockerfile STOPSIGNAL"},"content":{"rendered":"<h1>Understanding the Dockerfile STOPSIGNAL Instruction<\/h1>\n<p>The <code>STOPSIGNAL<\/code> instruction in a <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\" target=\"_blank\">Dockerfile<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A Dockerfile is a script containing a series of instructions to automate the creation of Docker images. It specifies the base image, application dependencies, and configuration, facilitating consistent deployment across environments.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> specifies the system signal that will be sent to the main process in the <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\" target=\"_blank\">container<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Containers are lightweight, portable units that encapsulate software and its dependencies, enabling consistent execution across different environments. They leverage OS-level virtualization for efficiency.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> when a <code>docker stop<\/code> command is issued. This instruction is crucial for defining how a <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\" target=\"_blank\">container<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Containers are lightweight, portable units that encapsulate software and its dependencies, enabling consistent execution across different environments. They leverage OS-level virtualization for efficiency.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> gracefully shuts down, allowing for proper cleanup of resources, data flushing, and termination of child processes. Understanding the <code>STOPSIGNAL<\/code> directive enhances the management of Docker containers, especially in production environments where stability and data integrity are paramount.<\/p>\n<h2>The Importance of Signal Handling in Containers<\/h2>\n<p>Signals in Unix-like operating systems are an essential mechanism for inter-process communication. They allow processes to receive notifications to perform specific actions, such as terminating gracefully or reloading configuration files. In the context of Docker, understanding how to handle signals effectively can determine the stability and reliability of applications running within containers.<\/p>\n<p>When a Docker <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\" target=\"_blank\">container<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Containers are lightweight, portable units that encapsulate software and its dependencies, enabling consistent execution across different environments. They leverage OS-level virtualization for efficiency.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> is stopped, Docker uses a default signal (<code>SIGTERM<\/code>) to signal the main process within the <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\" target=\"_blank\">container<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Containers are lightweight, portable units that encapsulate software and its dependencies, enabling consistent execution across different environments. They leverage OS-level virtualization for efficiency.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. However, the way an application responds to this signal can vary widely. By default, many applications may terminate immediately upon receiving <code>SIGTERM<\/code>, leading to potential data loss or corruption. The <code>STOPSIGNAL<\/code> instruction allows developers to customize this behavior, ensuring that applications have the opportunity to complete ongoing tasks before shutting down.<\/p>\n<h2>Syntax and Usage<\/h2>\n<p>The syntax for the <code>STOPSIGNAL<\/code> instruction in a <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\" target=\"_blank\">Dockerfile<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A Dockerfile is a script containing a series of instructions to automate the creation of Docker images. It specifies the base image, application dependencies, and configuration, facilitating consistent deployment across environments.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> is straightforward:<\/p>\n<pre><code class=\"language-dockerfile\">STOPSIGNAL <\/code><\/pre>\n<p>Where <code>` can be any valid signal name such as<\/code>SIGTERM<code>,<\/code>SIGINT<code>, or a generic number representing a signal. For example, to set<\/code>SIGQUIT` as the stop signal, you would write:<\/p>\n<pre><code class=\"language-dockerfile\">STOPSIGNAL SIGQUIT<\/code><\/pre>\n<p>It is worth noting that the signal can be specified in either its symbolic name (like <code>SIGTERM<\/code>) or its numeric representation (like <code>15<\/code>, the operating system default for <code>SIGTERM<\/code>). The following example illustrates both usages:<\/p>\n<pre><code class=\"language-dockerfile\">STOPSIGNAL SIGTERM\n# or\nSTOPSIGNAL 15<\/code><\/pre>\n<h2>Default Behavior Without STOPSIGNAL<\/h2>\n<p>If the <code>STOPSIGNAL<\/code> instruction is omitted from a <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\" target=\"_blank\">Dockerfile<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A Dockerfile is a script containing a series of instructions to automate the creation of Docker images. It specifies the base image, application dependencies, and configuration, facilitating consistent deployment across environments.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>, Docker defaults to sending <code>SIGTERM<\/code> to the PID 1 process in the <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\" target=\"_blank\">container<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Containers are lightweight, portable units that encapsulate software and its dependencies, enabling consistent execution across different environments. They leverage OS-level virtualization for efficiency.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> when the <code>docker stop<\/code> command is executed. This can be problematic for some applications that are not designed to handle <code>SIGTERM<\/code> properly. Consequently, they may not terminate gracefully, leading to resource leaks, incomplete transactions, or corrupted state.<\/p>\n<h3>Example of Default Behavior<\/h3>\n<p>Consider a simple application that does not handle <code>SIGTERM<\/code>. When the <code>docker stop<\/code> command is issued:<\/p>\n<pre><code class=\"language-bash\">docker stop my_container<\/code><\/pre>\n<p>Docker sends <code>SIGTERM<\/code> to the main process. If the application does not implement any signal handling logic, it terminates immediately, potentially leading to data loss.<\/p>\n<h2>Implementing STOPSIGNAL for Graceful Shutdown<\/h2>\n<p>To implement the <code>STOPSIGNAL<\/code> instruction effectively, the application within the <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\" target=\"_blank\">container<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Containers are lightweight, portable units that encapsulate software and its dependencies, enabling consistent execution across different environments. They leverage OS-level virtualization for efficiency.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> should have signal handling mechanisms to manage shutdown procedures. Below is a step-by-step guide on how to implement <code>STOPSIGNAL<\/code> for a <span class=\"glossaryai-tooltip glossary-term-684\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/node\/\" target=\"_blank\">Node<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Node, or Node.js, is a JavaScript runtime built on Chrome's V8 engine, enabling server-side scripting. It allows developers to build scalable network applications using asynchronous, event-driven architecture.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/node\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>.js application.<\/p>\n<h3>Step 1: Create a Simple Node.js Application<\/h3>\n<p>Let&#8217;s create a basic Express application that listens for HTTP requests and gracefully handles shutdown:<\/p>\n<pre><code class=\"language-javascript\">\/\/ app.js\nconst express = require('express');\nconst app = express();\nconst <span class=\"glossaryai-tooltip glossary-term-677\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/port\/\" target=\"_blank\">PORT<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A PORT is a communication endpoint in a computer network, defined by a numerical identifier. It facilitates the routing of data to specific applications, enhancing system functionality and security.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/port\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> = process.env.PORT || 3000;\n\napp.get('\/', (req, res) =&gt; {\n    res.send('Hello, World!');\n});\n\n\/\/ Signal handling\nconst server = app.listen(<span class=\"glossaryai-tooltip glossary-term-677\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/port\/\" target=\"_blank\">PORT<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A PORT is a communication endpoint in a computer network, defined by a numerical identifier. It facilitates the routing of data to specific applications, enhancing system functionality and security.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/port\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>, () =&gt; {\n    console.log(`Server is running on <span class=\"glossaryai-tooltip glossary-term-677\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/port\/\" target=\"_blank\">port<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A PORT is a communication endpoint in a computer network, defined by a numerical identifier. It facilitates the routing of data to specific applications, enhancing system functionality and security.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/port\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> ${PORT}`);\n});\n\n\/\/ Graceful shutdown\nconst shutdown = () =&gt; {\n    console.log('Received shutdown signal, closing server...');\n    server.close(() =&gt; {\n        console.log('Server closed');\n        process.exit(0);\n    });\n};\n\nprocess.on('SIGTERM', shutdown);\nprocess.on('SIGINT', shutdown);<\/code><\/pre>\n<h3>Step 2: Write the Dockerfile<\/h3>\n<p>Next, create a <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\" target=\"_blank\">Dockerfile<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A Dockerfile is a script containing a series of instructions to automate the creation of Docker images. It specifies the base image, application dependencies, and configuration, facilitating consistent deployment across environments.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> that includes the <code>STOPSIGNAL<\/code> instruction:<\/p>\n<pre><code class=\"language-dockerfile\"># Use a Node.js base image\nFROM node:14\n\n# Set the working directory\nWORKDIR \/usr\/src\/app\n\n# Copy application files\nCOPY package*.json .\/\nRUN npm install\nCOPY . .\n\n# Specify the stop signal\nSTOPSIGNAL SIGTERM\n\n# Expose the port\nEXPOSE 3000\n\n# Command to run the application\nCMD [\"node\", \"app.js\"]<\/code><\/pre>\n<h3>Step 3: Build and Run the Container<\/h3>\n<p>Build and <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/run\/\" target=\"_blank\">run<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">\"RUN\" refers to a command in various programming languages and operating systems to execute a specified program or script. It initiates processes, providing a controlled environment for task execution.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> the Docker <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\" target=\"_blank\">container<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Containers are lightweight, portable units that encapsulate software and its dependencies, enabling consistent execution across different environments. They leverage OS-level virtualization for efficiency.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> using the following commands:<\/p>\n<pre><code class=\"language-bash\">docker build -t my-node-app .\ndocker <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/run\/\" target=\"_blank\">run<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">\"RUN\" refers to a command in various programming languages and operating systems to execute a specified program or script. It initiates processes, providing a controlled environment for task execution.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> --name my-node-app -p 3000:3000 my-node-app<\/code><\/pre>\n<h3>Step 4: Test the Graceful Shutdown<\/h3>\n<p>Now test the graceful shutdown by executing:<\/p>\n<pre><code class=\"language-bash\">docker stop my-node-app<\/code><\/pre>\n<p>The output should indicate that the server is closing gracefully, thanks to the signal handling implemented in the application.<\/p>\n<h2>Customizing the Stop Signal<\/h2>\n<p>While <code>SIGTERM<\/code> is the default and often the most appropriate choice, there are scenarios where you might want to use different signals depending on your application&#8217;s specific needs.<\/p>\n<h3>Using SIGKILL for Immediate Termination<\/h3>\n<p>In some cases, you may wish to use <code>SIGKILL<\/code> as the stop signal. This would be useful for applications that do not need to perform any cleanup:<\/p>\n<pre><code class=\"language-dockerfile\">STOPSIGNAL SIGKILL<\/code><\/pre>\n<p>However, using <code>SIGKILL<\/code> prevents the application from executing any shutdown logic, which can lead to data inconsistency or other issues. It should be used with caution and only when absolutely necessary.<\/p>\n<h3>Choosing Between Signals<\/h3>\n<p>The choice of stop signal should be based on how the application is designed to handle shutdowns:<\/p>\n<ul>\n<li><strong>SIGTERM<\/strong>: The default signal for graceful shutdowns. Ideal for most applications.<\/li>\n<li><strong>SIGINT<\/strong>: Often used for interrupting processes that manage user-facing applications.<\/li>\n<li><strong>SIGQUIT<\/strong>: Similar to <code>SIGINT<\/code>, but allows for core dumps, useful for debugging.<\/li>\n<li><strong>SIGKILL<\/strong>: Forces termination without any cleanup; use sparingly.<\/li>\n<\/ul>\n<h2>Combining STOPSIGNAL with Health Checks<\/h2>\n<p>When designing Docker containers, you may want to combine the <code>STOPSIGNAL<\/code> instruction with health checks to ensure that your application can respond to signals only when it is in a healthy state. <\/p>\n<h3>Implementing Health Checks<\/h3>\n<p>A <span class=\"glossaryai-tooltip glossary-term-698\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/health-check\/\" target=\"_blank\">health check<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A health check is a systematic evaluation of an individual's physical and mental well-being, often involving assessments of vital signs, medical history, and lifestyle factors to identify potential health risks.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/health-check\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> can be added to your <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\" target=\"_blank\">Dockerfile<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A Dockerfile is a script containing a series of instructions to automate the creation of Docker images. It specifies the base image, application dependencies, and configuration, facilitating consistent deployment across environments.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> as follows:<\/p>\n<pre><code class=\"language-dockerfile\"><span class=\"glossaryai-tooltip glossary-term-680\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/healthcheck\/\" target=\"_blank\">HEALTHCHECK<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">HEALTHCHECK is a Docker directive used to monitor container health by executing specified commands at defined intervals. It enhances reliability by enabling automatic restarts for failing services.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/healthcheck\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> <span class=\"glossaryai-tooltip glossary-term-670\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/cmd\/\" target=\"_blank\">CMD<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">CMD, or Command Prompt, is a command-line interpreter in Windows operating systems. It allows users to execute commands, automate tasks, and manage system files through a text-based interface.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/cmd\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> curl --fail http:\/\/localhost:3000\/ || exit 1<\/code><\/pre>\n<p>This command checks if the application is running and responding on <span class=\"glossaryai-tooltip glossary-term-677\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/port\/\" target=\"_blank\">port<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A PORT is a communication endpoint in a computer network, defined by a numerical identifier. It facilitates the routing of data to specific applications, enhancing system functionality and security.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/port\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> 3000. If the application is unhealthy, Docker will not attempt to send the stop signal, preventing potential data corruption or inconsistent state.<\/p>\n<h3>Example Dockerfile with Health Check<\/h3>\n<p>Here is an updated version of the <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\" target=\"_blank\">Dockerfile<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A Dockerfile is a script containing a series of instructions to automate the creation of Docker images. It specifies the base image, application dependencies, and configuration, facilitating consistent deployment across environments.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> that includes a <span class=\"glossaryai-tooltip glossary-term-698\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/health-check\/\" target=\"_blank\">health check<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A health check is a systematic evaluation of an individual's physical and mental well-being, often involving assessments of vital signs, medical history, and lifestyle factors to identify potential health risks.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/health-check\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>:<\/p>\n<pre><code class=\"language-dockerfile\">FROM node:14\nWORKDIR \/usr\/src\/app\nCOPY package*.json .\/\nRUN npm install\nCOPY . .\n\nSTOPSIGNAL SIGTERM\nHEALTHCHECK CMD curl --fail http:\/\/localhost:3000\/ || exit 1\n\nEXPOSE 3000\nCMD [\"node\", \"app.js\"]<\/code><\/pre>\n<h2>Real-World Scenarios and Best Practices<\/h2>\n<p>Using the <code>STOPSIGNAL<\/code> instruction effectively can significantly improve the management of Docker containers in production environments. Here are some best practices and real-world scenarios:<\/p>\n<h3>1. Ensure Application-Level Signal Handling<\/h3>\n<p>Always implement application-level signal handling in your containers. This allows the application to perform essential cleanup operations and ensures data integrity.<\/p>\n<h3>2. Use Health Checks for Robustness<\/h3>\n<p>Incorporate health checks to validate the state of your application before it receives stop signals. This prevents Docker from sending stop signals to unhealthy containers, thereby avoiding unnecessary crashes.<\/p>\n<h3>3. Document Signal Behavior<\/h3>\n<p>Document how your application handles various signals, especially in complex systems. This aids in debugging and enhances team collaboration.<\/p>\n<h3>4. Test Shutdown Procedures<\/h3>\n<p>Regularly test the shutdown procedures by using <code>docker stop<\/code>, particularly after deploying new versions of your application. This ensures that changes do not inadvertently affect signal handling.<\/p>\n<h3>5. Monitor Resource Cleanup<\/h3>\n<p>After implementing the <code>STOPSIGNAL<\/code> instruction, monitor resource cleanup during shutdowns. Use logs and monitoring tools to ensure that processes terminate as expected.<\/p>\n<h2>Conclusion<\/h2>\n<p>The <code>STOPSIGNAL<\/code> instruction in <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\" target=\"_blank\">Dockerfile<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A Dockerfile is a script containing a series of instructions to automate the creation of Docker images. It specifies the base image, application dependencies, and configuration, facilitating consistent deployment across environments.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> is a powerful but often underutilized feature that can make a significant difference in how applications running in containers handle shutdowns. By specifying the appropriate signal, you enable your applications to gracefully terminate, ensuring that they can complete ongoing processes and clean up resources effectively.<\/p>\n<p>Implementing signal handling, combining it with health checks, and following best practices can lead to robust and reliable <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\" target=\"_blank\">container<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Containers are lightweight, portable units that encapsulate software and its dependencies, enabling consistent execution across different environments. They leverage OS-level virtualization for efficiency.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> deployments. As containerized applications become increasingly central to modern software architecture, understanding and utilizing the <code>STOPSIGNAL<\/code> instruction becomes vital for developers aiming to create resilient applications.<\/p>\n<p>By investing time in mastering this feature, you equip yourself with the knowledge necessary to build better Docker containers and maintain high levels of application performance and reliability, regardless of the environment in which they operate.<\/p>","protected":false},"excerpt":{"rendered":"<p>L'instruction `STOPSIGNAL` dans un <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\" target=\"_blank\">Dockerfile<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A Dockerfile is a script containing a series of instructions to automate the creation of Docker images. It specifies the base image, application dependencies, and configuration, facilitating consistent deployment across environments.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> specifies the signal that should be sent to the container\u2019s main process to stop it gracefully. By default, Docker uses SIGTERM, but custom signals can enhance process management.<\/p>","protected":false},"author":1,"featured_media":1957,"parent":0,"template":"","glossary-cat":[],"class_list":["post-1321","glossary","type-glossary","status-publish","has-post-thumbnail","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Dockerfile STOPSIGNAL - Dockerpros<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile-stopsignal\/\" \/>\n<meta property=\"og:locale\" content=\"fr_FR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dockerfile STOPSIGNAL - Dockerpros\" \/>\n<meta property=\"og:description\" content=\"The `STOPSIGNAL` instruction in a Dockerfile specifies the signal that should be sent to the container\u2019s main process to stop it gracefully. By default, Docker uses SIGTERM, but custom signals can enhance process management.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile-stopsignal\/\" \/>\n<meta property=\"og:site_name\" content=\"Dockerpros\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-23T12:23:26+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-stopsignal_1321.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"800\" \/>\n\t<meta property=\"og:image:height\" content=\"600\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Dur\u00e9e de lecture estim\u00e9e\" \/>\n\t<meta name=\"twitter:data1\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/\",\"url\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/\",\"name\":\"Dockerfile STOPSIGNAL - Dockerpros\",\"isPartOf\":{\"@id\":\"https:\/\/dockerpros.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-stopsignal_1321.jpg\",\"datePublished\":\"2024-07-23T12:20:37+00:00\",\"dateModified\":\"2024-07-23T12:23:26+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#breadcrumb\"},\"inLanguage\":\"fr-FR\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#primaryimage\",\"url\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-stopsignal_1321.jpg\",\"contentUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-stopsignal_1321.jpg\",\"width\":800,\"height\":600,\"caption\":\"dockerfile-stopsignal-2\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/dockerpros.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Glossary\",\"item\":\"https:\/\/dockerpros.com\/fr\/wiki\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Dockerfile STOPSIGNAL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/dockerpros.com\/#website\",\"url\":\"https:\/\/dockerpros.com\/\",\"name\":\"Dockerpros\",\"description\":\"DockerPros \u2013 Your Ultimate Docker Resource Hub\",\"publisher\":{\"@id\":\"https:\/\/dockerpros.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/dockerpros.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"fr-FR\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/dockerpros.com\/#organization\",\"name\":\"Dockerpros\",\"url\":\"https:\/\/dockerpros.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"fr-FR\",\"@id\":\"https:\/\/dockerpros.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/Dockerpros_logo_blanco.png\",\"contentUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/Dockerpros_logo_blanco.png\",\"width\":532,\"height\":114,\"caption\":\"Dockerpros\"},\"image\":{\"@id\":\"https:\/\/dockerpros.com\/#\/schema\/logo\/image\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Dockerfile STOPSIGNAL - Dockerpros","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile-stopsignal\/","og_locale":"fr_FR","og_type":"article","og_title":"Dockerfile STOPSIGNAL - Dockerpros","og_description":"The `STOPSIGNAL` instruction in a Dockerfile specifies the signal that should be sent to the container\u2019s main process to stop it gracefully. By default, Docker uses SIGTERM, but custom signals can enhance process management.","og_url":"https:\/\/dockerpros.com\/fr\/wiki\/dockerfile-stopsignal\/","og_site_name":"Dockerpros","article_modified_time":"2024-07-23T12:23:26+00:00","og_image":[{"width":800,"height":600,"url":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-stopsignal_1321.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Dur\u00e9e de lecture estim\u00e9e":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/","url":"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/","name":"Dockerfile STOPSIGNAL - Dockerpros","isPartOf":{"@id":"https:\/\/dockerpros.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#primaryimage"},"image":{"@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#primaryimage"},"thumbnailUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-stopsignal_1321.jpg","datePublished":"2024-07-23T12:20:37+00:00","dateModified":"2024-07-23T12:23:26+00:00","breadcrumb":{"@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#breadcrumb"},"inLanguage":"fr-FR","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/"]}]},{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#primaryimage","url":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-stopsignal_1321.jpg","contentUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-stopsignal_1321.jpg","width":800,"height":600,"caption":"dockerfile-stopsignal-2"},{"@type":"BreadcrumbList","@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-stopsignal\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dockerpros.com\/"},{"@type":"ListItem","position":2,"name":"Glossary","item":"https:\/\/dockerpros.com\/fr\/wiki\/"},{"@type":"ListItem","position":3,"name":"Dockerfile STOPSIGNAL"}]},{"@type":"WebSite","@id":"https:\/\/dockerpros.com\/#website","url":"https:\/\/dockerpros.com\/","name":"Dockerpros","description":"DockerPros \u2013 Votre centre de ressources Docker incontournable","publisher":{"@id":"https:\/\/dockerpros.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dockerpros.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"fr-FR"},{"@type":"Organization","@id":"https:\/\/dockerpros.com\/#organization","name":"Dockerpros","url":"https:\/\/dockerpros.com\/","logo":{"@type":"ImageObject","inLanguage":"fr-FR","@id":"https:\/\/dockerpros.com\/#\/schema\/logo\/image\/","url":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/Dockerpros_logo_blanco.png","contentUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/Dockerpros_logo_blanco.png","width":532,"height":114,"caption":"Dockerpros"},"image":{"@id":"https:\/\/dockerpros.com\/#\/schema\/logo\/image\/"}}]}},"_links":{"self":[{"href":"https:\/\/dockerpros.com\/fr\/wp-json\/wp\/v2\/glossary\/1321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dockerpros.com\/fr\/wp-json\/wp\/v2\/glossary"}],"about":[{"href":"https:\/\/dockerpros.com\/fr\/wp-json\/wp\/v2\/types\/glossary"}],"author":[{"embeddable":true,"href":"https:\/\/dockerpros.com\/fr\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":0,"href":"https:\/\/dockerpros.com\/fr\/wp-json\/wp\/v2\/glossary\/1321\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dockerpros.com\/fr\/wp-json\/wp\/v2\/media\/1957"}],"wp:attachment":[{"href":"https:\/\/dockerpros.com\/fr\/wp-json\/wp\/v2\/media?parent=1321"}],"wp:term":[{"taxonomy":"glossary-cat","embeddable":true,"href":"https:\/\/dockerpros.com\/fr\/wp-json\/wp\/v2\/glossary-cat?post=1321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}