{"id":1327,"date":"2024-07-23T12:20:58","date_gmt":"2024-07-23T12:20:58","guid":{"rendered":"https:\/\/dockerpros.com\/?post_type=glossary&#038;p=1327"},"modified":"2024-07-23T12:23:33","modified_gmt":"2024-07-23T12:23:33","slug":"dockerfile-ziel","status":"publish","type":"glossary","link":"https:\/\/dockerpros.com\/de\/wiki\/dockerfile-target\/","title":{"rendered":"Dockerfile TARGET"},"content":{"rendered":"<h2>Understanding Dockerfile TARGET: Advanced Insights<\/h2>\n<p>In the realm of containerization, the <code><span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span><\/code> serves as the blueprint for building Docker images. Among the myriad features offered by Dockerfiles, the <code>TARGET<\/code> mechanism stands out as a powerful tool for advanced users, allowing for multi-stage builds. This feature enables developers to optimize their Docker images by specifying different build targets within a single <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>, making it possible to streamline the image-building process, reduce size, and enhance security. This article will delve deep into the <code>TARGET<\/code> feature in Dockerfiles, exploring its syntax, practical applications, and best practices, while providing an overview of why it is an essential component of modern DevOps practices.<\/p>\n<h2>The Basics of Multi-Stage Builds<\/h2>\n<p>Before we explore the <code>TARGET<\/code> feature specifically, it&#8217;s important to understand the concept of multi-stage builds in Docker. Introduced in Docker 17.05, multi-stage builds allow developers to use multiple <code>FROM<\/code> statements within a single <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. Each stage can be built independently, pulling in different base images and configurations, which can significantly enhance the efficiency of the image-building process.<\/p>\n<p>Multi-stage builds help in reducing the size of the final <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> by allowing developers to include only the necessary artifacts. For example, you can compile your application in one stage using a full-fledged development environment and then <span class=\"glossaryai-tooltip glossary-term-673\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/copy\/\" target=\"_blank\">copy<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">COPY is a command in computer programming and data management that facilitates the duplication of files or data from one location to another, ensuring data integrity and accessibility.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/copy\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> only the compiled binary to a minimal base <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> in another stage. This practice minimizes the attack surface and reduces resource consumption when deploying containers.<\/p>\n<h2>Syntax of the TARGET Instruction<\/h2>\n<p>The <code>TARGET<\/code> feature is utilized when invoking the <code>docker build<\/code> command. The primary syntax is as follows:<\/p>\n<pre><code class=\"language-sh\">docker build --target  -t : .<\/code><\/pre>\n<p>The &#8220; refers to the name of the stage defined in the <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. By specifying this target, the <span class=\"glossaryai-tooltip glossary-term-666\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/docker-engine\/\" target=\"_blank\">Docker engine<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Docker Engine is an open-source containerization technology that enables developers to build, deploy, and manage applications within lightweight, isolated environments called containers.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/docker-engine\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> will only build up to that specific stage, allowing you to skip subsequent stages that may not be necessary for your current requirements.<\/p>\n<h3>Example of a Multi-Stage Dockerfile<\/h3>\n<p>To illustrate the power of the <code>TARGET<\/code> feature, consider the following example of a multi-stage <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>:<\/p>\n<pre><code class=\"language-Dockerfile\"># Stage 1: Build Stage\nFROM golang:1.18 AS builder\nWORKDIR \/app\nCOPY . .\nRUN go build -o myapp\n\n# Stage 2: Production Stage\nFROM alpine:3.15\nWORKDIR \/app\nCOPY --from=builder \/app\/myapp .\nCMD [\".\/myapp\"]<\/code><\/pre>\n<p>In this example, we have two stages: the first stage (<code>builder<\/code>) compiles a Go application, while the second stage creates a minimal production <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> based on Alpine Linux. <\/p>\n<h3>Building to a Specific Target<\/h3>\n<p>When you want to build only the <code>builder<\/code> stage and perhaps debug or test the application, you can use the <code>TARGET<\/code> feature as follows:<\/p>\n<pre><code class=\"language-sh\">docker build --target builder -t myapp:builder .<\/code><\/pre>\n<p>This command builds the <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> up to the <code>builder<\/code> stage only, allowing you to <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> the Go application in the development environment without shipping the complete production <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>.<\/p>\n<h2>Advantages of Using TARGET<\/h2>\n<h3>1. Streamlined Development Process<\/h3>\n<p>By utilizing the <code>TARGET<\/code> feature, developers can quickly iterate on specific stages of their application without the need to rebuild the entire Docker <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. This can significantly speed up the development workflow, especially in environments where build time is critical.<\/p>\n<h3>2. Enhanced Security<\/h3>\n<p>Reducing the number of layers and minimizing the contents of your final <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> contributes to improved security. By using <code>TARGET<\/code>, you can ensure that only the necessary components are included in the production <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>, while development and testing tools remain in a separate stage that is not deployed.<\/p>\n<h3>3. Space Efficiency<\/h3>\n<p>Docker images can grow large over time, particularly with dependencies and build tools. By segmenting the build process into stages and using <code>TARGET<\/code>, you can create smaller images by only including the essential artifacts needed to <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> your application.<\/p>\n<h3>4. Customization for Different Environments<\/h3>\n<p>Different environments (development, staging, production) often require different configurations. The <code>TARGET<\/code> feature allows for seamless building of images tailored for each environment by creating dedicated stages for each environment&#8217;s needs.<\/p>\n<h2>Best Practices for Using TARGET<\/h2>\n<h3>1. Naming Stages Clearly<\/h3>\n<p>When leveraging multi-stage builds and the <code>TARGET<\/code> feature, it&#8217;s crucial to provide meaningful names to each stage. Clear and descriptive names make it easier for developers to understand the purpose of each stage and which target they should use during the build process.<\/p>\n<p>For example:<\/p>\n<pre><code class=\"language-Dockerfile\">FROM <span class=\"glossaryai-tooltip glossary-term-684\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/node\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>:14 AS development\n# Development stage instructions\n\nFROM <span class=\"glossaryai-tooltip glossary-term-684\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/node\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>:14 AS testing\n# Testing stage instructions\n\nFROM <span class=\"glossaryai-tooltip glossary-term-684\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/node\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>:14 AS production\n# Production stage instructions<\/code><\/pre>\n<h3>2. Keep Stages Lean<\/h3>\n<p>Each stage should have a well-defined purpose and should include only what is necessary for that stage. This approach not only minimizes <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> size but also enhances performance and security.<\/p>\n<h3>3. Use Caching Wisely<\/h3>\n<p>Docker uses a layer caching mechanism that can speed up builds if the layers have not changed. When making modifications, try to structure your <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> to maximize the efficiency of the cache. Changes in later stages should ideally not invalidate the cache in earlier stages.<\/p>\n<h3>4. Document Your Dockerfile<\/h3>\n<p>Including comments in your <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\/de\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> can provide context and clarify the reasoning behind specific stages and commands. This documentation is invaluable for team members who may work on the project in the future.<\/p>\n<h3>5. Test Each Stage Independently<\/h3>\n<p>When working with multi-stage builds, it may be beneficial to build and test each stage independently to ensure they function as intended. This practice helps to catch issues early in the development process.<\/p>\n<h2>Practical Use Cases for TARGET<\/h2>\n<h3>1. Development vs. Production Builds<\/h3>\n<p>A frequent use case for the <code>TARGET<\/code> feature is the differentiation between development and production builds. By defining separate stages for development (with testing tools and dependencies) and production (with just the necessary runtime), teams can easily switch between environments.<\/p>\n<h3>2. Building Libraries or Dependencies<\/h3>\n<p>In scenarios where libraries are built as separate artifacts, developers can create a <span class=\"glossaryai-tooltip glossary-term-740\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/multi-stage-build\/\" target=\"_blank\">multi-stage build<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">A multi-stage build is a Docker optimization technique that enables the separation of build and runtime environments. By using multiple FROM statements in a single Dockerfile, developers can streamline image size and enhance security by excluding unnecessary build dependencies in the final image.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/multi-stage-build\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> that first compiles the library and then builds the application that depends on it. Using <code>TARGET<\/code>, they can build and test the library independently.<\/p>\n<h3>3. CI\/CD Pipeline Optimization<\/h3>\n<p>In continuous integration and deployment pipelines, utilizing the <code>TARGET<\/code> feature allows for optimized builds. Specific stages can be built and tested based on the context of the pipeline, reducing build times and resource consumption.<\/p>\n<h2>Conclusion<\/h2>\n<p>The <code>TARGET<\/code> feature in Dockerfiles is a powerful tool that enables developers to optimize their containerized applications through multi-stage builds. By allowing for targeted builds, this feature enhances the development workflow, improves security, reduces <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">image<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An image is a visual representation of an object or scene, typically composed of pixels in digital formats. It can convey information, evoke emotions, and facilitate communication across various media.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> size, and accommodates diverse environment configurations. By adhering to best practices and leveraging practical use cases, developers can fully exploit the capabilities of Docker, driving efficiency and effectiveness in their containerization efforts.<\/p>\n<p>As the ecosystem around Docker continues to evolve, the importance of mastering advanced features like <code>TARGET<\/code> cannot be overstated. As applications become more complex and the demand for agile development practices grows, understanding and implementing multi-stage builds will be essential for delivering modern applications in a reliable and efficient manner. Whether you are a seasoned Docker user or just beginning your journey, embracing the <code>TARGET<\/code> feature will undoubtedly enhance your containerization strategy.<\/p>","protected":false},"excerpt":{"rendered":"<p>Die <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/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\">Eine Dockerfile ist ein Skript, das eine Reihe von Anweisungen zur Automatisierung der Erstellung von Docker-Images enth\u00e4lt. Sie gibt das Basis-Image, die Anwendungsabh\u00e4ngigkeiten und die Konfiguration an und erm\u00f6glicht so eine konsistente Bereitstellung \u00fcber verschiedene Umgebungen hinweg.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> TARGET instruction enables multi-stage builds, allowing developers to define specific build stages. This feature optimizes <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\" target=\"_blank\">Bild<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Ein Bild ist eine visuelle Darstellung eines Objekts oder einer Szene, die typischerweise aus Pixeln in digitalen Formaten besteht. Es kann Informationen vermitteln, Emotionen hervorrufen und die Kommunikation \u00fcber verschiedene Medien hinweg erleichtern.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/de\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> size by copying only necessary artifacts, enhancing efficiency.<\/p>","protected":false},"author":1,"featured_media":1967,"parent":0,"template":"","glossary-cat":[],"class_list":["post-1327","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 TARGET - 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\/de\/wiki\/dockerfile-ziel\/\" \/>\n<meta property=\"og:locale\" content=\"de_DE\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dockerfile TARGET - Dockerpros\" \/>\n<meta property=\"og:description\" content=\"The Dockerfile TARGET instruction enables multi-stage builds, allowing developers to define specific build stages. This feature optimizes image size by copying only necessary artifacts, enhancing efficiency.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dockerpros.com\/de\/wiki\/dockerfile-ziel\/\" \/>\n<meta property=\"og:site_name\" content=\"Dockerpros\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-23T12:23:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-target_1327.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=\"Gesch\u00e4tzte Lesezeit\" \/>\n\t<meta name=\"twitter:data1\" content=\"6\u00a0Minuten\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/\",\"url\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/\",\"name\":\"Dockerfile TARGET - Dockerpros\",\"isPartOf\":{\"@id\":\"https:\/\/dockerpros.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-target_1327.jpg\",\"datePublished\":\"2024-07-23T12:20:58+00:00\",\"dateModified\":\"2024-07-23T12:23:33+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#breadcrumb\"},\"inLanguage\":\"de\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#primaryimage\",\"url\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-target_1327.jpg\",\"contentUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-target_1327.jpg\",\"width\":800,\"height\":600,\"caption\":\"dockerfile-target-2\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#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 TARGET\"}]},{\"@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\":\"de\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/dockerpros.com\/#organization\",\"name\":\"Dockerpros\",\"url\":\"https:\/\/dockerpros.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"de\",\"@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 TARGET - Dockerpros\n\nIn diesem Artikel werden wir uns mit dem Konzept des Dockerfile TARGET in Docker besch\u00e4ftigen. Dockerfile TARGET ist eine Funktion, die es erm\u00f6glicht, mehrere Build-Phasen in einem einzigen Dockerfile zu definieren. Dies kann besonders n\u00fctzlich sein, wenn Sie verschiedene Umgebungen oder Konfigurationen f\u00fcr Ihre Anwendung erstellen m\u00f6chten.\n\nWas ist Dockerfile TARGET?\n\nDockerfile TARGET ist eine Anweisung, die in einem Dockerfile verwendet wird, um eine bestimmte Build-Phase als Ziel f\u00fcr den Build-Prozess festzulegen. Mit anderen Worten, es erm\u00f6glicht Ihnen, verschiedene Stufen oder Phasen in Ihrem Dockerfile zu definieren und dann eine bestimmte Phase als Ziel f\u00fcr den Build auszuw\u00e4hlen.\n\nWarum Dockerfile TARGET verwenden?\n\nEs gibt mehrere Gr\u00fcnde, warum Sie Dockerfile TARGET verwenden m\u00f6chten:\n\n1. Mehrere Umgebungen: Wenn Sie verschiedene Umgebungen f\u00fcr Ihre Anwendung haben, z. B. Entwicklung, Test und Produktion, k\u00f6nnen Sie mit Dockerfile TARGET verschiedene Build-Phasen f\u00fcr jede Umgebung definieren.\n\n2. Gr\u00f6\u00dfenoptimierung: Durch die Verwendung von Dockerfile TARGET k\u00f6nnen Sie die Gr\u00f6\u00dfe Ihres endg\u00fcltigen Docker-Images reduzieren, indem Sie nur die notwendigen Dateien und Abh\u00e4ngigkeiten f\u00fcr die Zielphase einbeziehen.\n\n3. Sicherheit: Dockerfile TARGET erm\u00f6glicht es Ihnen, sensible Informationen oder Abh\u00e4ngigkeiten in fr\u00fcheren Phasen zu definieren und sie dann in der Zielphase zu entfernen oder zu ersetzen.\n\nWie verwendet man Dockerfile TARGET?\n\nUm Dockerfile TARGET zu verwenden, m\u00fcssen Sie die Anweisung \"FROM\" mit dem Schl\u00fcsselwort \"AS\" gefolgt von einem Namen f\u00fcr die Phase verwenden. Anschlie\u00dfend k\u00f6nnen Sie in sp\u00e4teren Phasen auf diese Phase verweisen, indem Sie den Namen in der FROM-Anweisung verwenden.\n\nHier ist ein Beispiel f\u00fcr ein Dockerfile mit mehreren Phasen:\n\n```dockerfile\n# Phase 1: Build-Umgebung\nFROM node:14 AS build-env\nWORKDIR \/app\nCOPY package*.json .\/\nRUN npm install\nCOPY . .\nRUN npm run build\n\n# Phase 2: Produktionsumgebung\nFROM nginx:alpine AS production-env\nCOPY --from=build-env \/app\/dist \/usr\/share\/nginx\/html\n```\n\nIn diesem Beispiel haben wir zwei Phasen definiert: \"build-env\" und \"production-env\". In der ersten Phase erstellen wir die Anwendung mit Node.js und f\u00fchren den Build-Prozess durch. In der zweiten Phase kopieren wir die gebauten Dateien aus der ersten Phase in das Nginx-Image f\u00fcr die Produktion.\n\nUm das Dockerfile mit einer bestimmten Phase als Ziel zu bauen, k\u00f6nnen Sie den folgenden Befehl verwenden:\n\n```bash\ndocker build --target production-env -t my-app .\n```\n\nIn diesem Befehl geben wir den Namen der Zielphase (\"production-env\") mit dem Flag \"--target\" an. Dadurch wird nur die angegebene Phase als Ziel f\u00fcr den Build ausgew\u00e4hlt.\n\nFazit\n\nDockerfile TARGET ist eine leistungsstarke Funktion in Docker, die es erm\u00f6glicht, mehrere Build-Phasen in einem einzigen Dockerfile zu definieren. Es bietet Flexibilit\u00e4t bei der Erstellung verschiedener Umgebungen und hilft dabei, die Gr\u00f6\u00dfe und Sicherheit Ihrer Docker-Images zu optimieren. Indem Sie Dockerfile TARGET verwenden, k\u00f6nnen Sie Ihren Build-Prozess effizienter gestalten und die Verwaltung Ihrer Docker-Images vereinfachen.","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\/de\/wiki\/dockerfile-ziel\/","og_locale":"de_DE","og_type":"article","og_title":"Dockerfile TARGET - Dockerpros","og_description":"The Dockerfile TARGET instruction enables multi-stage builds, allowing developers to define specific build stages. This feature optimizes image size by copying only necessary artifacts, enhancing efficiency.","og_url":"https:\/\/dockerpros.com\/de\/wiki\/dockerfile-ziel\/","og_site_name":"Dockerpros","article_modified_time":"2024-07-23T12:23:33+00:00","og_image":[{"width":800,"height":600,"url":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-target_1327.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Gesch\u00e4tzte Lesezeit":"6\u00a0Minuten"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/","url":"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/","name":"Dockerfile TARGET - Dockerpros\n\nIn diesem Artikel werden wir uns mit dem Konzept des Dockerfile TARGET in Docker besch\u00e4ftigen. Dockerfile TARGET ist eine Funktion, die es erm\u00f6glicht, mehrere Build-Phasen in einem einzigen Dockerfile zu definieren. Dies kann besonders n\u00fctzlich sein, wenn Sie verschiedene Umgebungen oder Konfigurationen f\u00fcr Ihre Anwendung erstellen m\u00f6chten.\n\nWas ist Dockerfile TARGET?\n\nDockerfile TARGET ist eine Anweisung, die in einem Dockerfile verwendet wird, um eine bestimmte Build-Phase als Ziel f\u00fcr den Build-Prozess festzulegen. Mit anderen Worten, es erm\u00f6glicht Ihnen, verschiedene Stufen oder Phasen in Ihrem Dockerfile zu definieren und dann eine bestimmte Phase als Ziel f\u00fcr den Build auszuw\u00e4hlen.\n\nWarum Dockerfile TARGET verwenden?\n\nEs gibt mehrere Gr\u00fcnde, warum Sie Dockerfile TARGET verwenden m\u00f6chten:\n\n1. Mehrere Umgebungen: Wenn Sie verschiedene Umgebungen f\u00fcr Ihre Anwendung haben, z. B. Entwicklung, Test und Produktion, k\u00f6nnen Sie mit Dockerfile TARGET verschiedene Build-Phasen f\u00fcr jede Umgebung definieren.\n\n2. Gr\u00f6\u00dfenoptimierung: Durch die Verwendung von Dockerfile TARGET k\u00f6nnen Sie die Gr\u00f6\u00dfe Ihres endg\u00fcltigen Docker-Images reduzieren, indem Sie nur die notwendigen Dateien und Abh\u00e4ngigkeiten f\u00fcr die Zielphase einbeziehen.\n\n3. Sicherheit: Dockerfile TARGET erm\u00f6glicht es Ihnen, sensible Informationen oder Abh\u00e4ngigkeiten in fr\u00fcheren Phasen zu definieren und sie dann in der Zielphase zu entfernen oder zu ersetzen.\n\nWie verwendet man Dockerfile TARGET?\n\nUm Dockerfile TARGET zu verwenden, m\u00fcssen Sie die Anweisung \"FROM\" mit dem Schl\u00fcsselwort \"AS\" gefolgt von einem Namen f\u00fcr die Phase verwenden. Anschlie\u00dfend k\u00f6nnen Sie in sp\u00e4teren Phasen auf diese Phase verweisen, indem Sie den Namen in der FROM-Anweisung verwenden.\n\nHier ist ein Beispiel f\u00fcr ein Dockerfile mit mehreren Phasen:\n\n```dockerfile\n# Phase 1: Build-Umgebung\nFROM node:14 AS build-env\nWORKDIR \/app\nCOPY package*.json .\/\nRUN npm install\nCOPY . .\nRUN npm run build\n\n# Phase 2: Produktionsumgebung\nFROM nginx:alpine AS production-env\nCOPY --from=build-env \/app\/dist \/usr\/share\/nginx\/html\n```\n\nIn diesem Beispiel haben wir zwei Phasen definiert: \"build-env\" und \"production-env\". In der ersten Phase erstellen wir die Anwendung mit Node.js und f\u00fchren den Build-Prozess durch. In der zweiten Phase kopieren wir die gebauten Dateien aus der ersten Phase in das Nginx-Image f\u00fcr die Produktion.\n\nUm das Dockerfile mit einer bestimmten Phase als Ziel zu bauen, k\u00f6nnen Sie den folgenden Befehl verwenden:\n\n```bash\ndocker build --target production-env -t my-app .\n```\n\nIn diesem Befehl geben wir den Namen der Zielphase (\"production-env\") mit dem Flag \"--target\" an. Dadurch wird nur die angegebene Phase als Ziel f\u00fcr den Build ausgew\u00e4hlt.\n\nFazit\n\nDockerfile TARGET ist eine leistungsstarke Funktion in Docker, die es erm\u00f6glicht, mehrere Build-Phasen in einem einzigen Dockerfile zu definieren. Es bietet Flexibilit\u00e4t bei der Erstellung verschiedener Umgebungen und hilft dabei, die Gr\u00f6\u00dfe und Sicherheit Ihrer Docker-Images zu optimieren. Indem Sie Dockerfile TARGET verwenden, k\u00f6nnen Sie Ihren Build-Prozess effizienter gestalten und die Verwaltung Ihrer Docker-Images vereinfachen.","isPartOf":{"@id":"https:\/\/dockerpros.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#primaryimage"},"image":{"@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#primaryimage"},"thumbnailUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-target_1327.jpg","datePublished":"2024-07-23T12:20:58+00:00","dateModified":"2024-07-23T12:23:33+00:00","breadcrumb":{"@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#breadcrumb"},"inLanguage":"de","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dockerpros.com\/wiki\/dockerfile-target\/"]}]},{"@type":"ImageObject","inLanguage":"de","@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#primaryimage","url":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-target_1327.jpg","contentUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-target_1327.jpg","width":800,"height":600,"caption":"dockerfile-target-2"},{"@type":"BreadcrumbList","@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-target\/#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 TARGET"}]},{"@type":"WebSite","@id":"https:\/\/dockerpros.com\/#website","url":"https:\/\/dockerpros.com\/","name":"Docker-Profis","description":"DockerPros \u2013 Ihr umfassender Docker-Ressourcen-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":"de"},{"@type":"Organization","@id":"https:\/\/dockerpros.com\/#organization","name":"Docker-Profis","url":"https:\/\/dockerpros.com\/","logo":{"@type":"ImageObject","inLanguage":"de","@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\/de\/wp-json\/wp\/v2\/glossary\/1327","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dockerpros.com\/de\/wp-json\/wp\/v2\/glossary"}],"about":[{"href":"https:\/\/dockerpros.com\/de\/wp-json\/wp\/v2\/types\/glossary"}],"author":[{"embeddable":true,"href":"https:\/\/dockerpros.com\/de\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":0,"href":"https:\/\/dockerpros.com\/de\/wp-json\/wp\/v2\/glossary\/1327\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dockerpros.com\/de\/wp-json\/wp\/v2\/media\/1967"}],"wp:attachment":[{"href":"https:\/\/dockerpros.com\/de\/wp-json\/wp\/v2\/media?parent=1327"}],"wp:term":[{"taxonomy":"glossary-cat","embeddable":true,"href":"https:\/\/dockerpros.com\/de\/wp-json\/wp\/v2\/glossary-cat?post=1327"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}