{"id":1317,"date":"2024-07-23T12:20:28","date_gmt":"2024-07-23T12:20:28","guid":{"rendered":"https:\/\/dockerpros.com\/?post_type=glossary&#038;p=1317"},"modified":"2024-07-23T12:23:18","modified_gmt":"2024-07-23T12:23:18","slug":"dockerfile-usuario","status":"publish","type":"glossary","link":"https:\/\/dockerpros.com\/es\/wiki\/dockerfile-user\/","title":{"rendered":"USER\n\nEl comando USER se utiliza para establecer el usuario (o UID) que ejecutar\u00e1 los comandos especificados en las instrucciones RUN, CMD y ENTRYPOINT que siguen en el Dockerfile.\n\nUSER daemon\n\nSi se especifica un nombre de usuario, debe existir en la imagen."},"content":{"rendered":"<h2>Understanding the <code>USER<\/code> Instruction in Dockerfile: A Comprehensive Guide<\/h2>\n<p>The <code>USER<\/code> instruction in a <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> defines the user name (or UID) and optionally the group name (or GID) to use when running commands in the <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. This instruction plays a critical role in securing applications, managing permissions, and ensuring that Docker containers <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> with the appropriate privileges. While it may seem simple at first glance, its implications are profound, especially in production environments where security and best practices are paramount.<\/p>\n<h3>The Importance of User Privileges in Docker<\/h3>\n<p>In traditional computing environments, running applications with elevated privileges can lead to significant security vulnerabilities. Docker containers, which encapsulate applications and their dependencies, are no exception. The default user for a Docker <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> is the root user, which has unrestricted access to the entire file system and all processes running within the <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. If a <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> is compromised, an attacker could potentially gain root access to the host system, leading to broader security incidents. <\/p>\n<p>To mitigate these risks, it is vital to <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> containers as non-root users whenever possible. By using the <code>USER<\/code> instruction, developers can specify the user under which the application should <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>, thereby adhering to the principle of least privilege and enhancing the overall security posture of the application.<\/p>\n<h3>How to Use the <code>USER<\/code> Instruction<\/h3>\n<p>The <code>USER<\/code> instruction can be used in several ways in a <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. Below are the basic syntaxes:<\/p>\n<ol>\n<li>\n<p><strong>Specifying a User by Name<\/strong>:<\/p>\n<pre><code class=\"language-dockerfile\">USER username<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Specifying a User by UID<\/strong>:<\/p>\n<pre><code class=\"language-dockerfile\">USER 1001<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Specifying a User and Group<\/strong>:<\/p>\n<pre><code class=\"language-dockerfile\">USER username:groupname<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Specifying a UID and GID<\/strong>:<\/p>\n<pre><code class=\"language-dockerfile\">USER 1001:1002<\/code><\/pre>\n<\/li>\n<\/ol>\n<h3>Order of Instructions Matters<\/h3>\n<p>The placement of the <code>USER<\/code> instruction within a <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> is significant. When this instruction is used, it applies to all subsequent instructions in the <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. Therefore, the point at which you declare your user can greatly affect how your application is built and <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>.<\/p>\n<p>For instance, consider the following <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> snippet:<\/p>\n<pre><code class=\"language-dockerfile\">FROM ubuntu:20.04\n\n# Creating a user\n<span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> useradd -ms \/bin\/bash myuser\n\n# Switch to the new user\nUSER myuser\n\n# This command runs as myuser\n<span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> whoami<\/code><\/pre>\n<p>In this case, the <code>whoami<\/code> command will be executed as <code>myuser<\/code>. If we placed the <code>USER<\/code> instruction after the <code><span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> whoami<\/code>, it would execute as the root user instead.<\/p>\n<h3>Best Practices for Using <code>USER<\/code><\/h3>\n<ol>\n<li>\n<p><strong>Create a Non-Root User<\/strong>: Always create a specific non-root user for your application. This ensures that if the application is compromised, the attacker will have limited access.<\/p>\n<pre><code class=\"language-dockerfile\"><span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> useradd -ms \/bin\/bash appuser\nUSER appuser<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Set Appropriate Permissions<\/strong>: When creating files or directories that need to be accessible by your application, ensure the correct ownership and permission settings are applied.<\/p>\n<pre><code class=\"language-dockerfile\"><span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> mkdir \/app &amp;&amp; chown appuser:appuser \/app<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Use Multi-Stage Builds<\/strong>: In complex applications, utilize multi-stage builds to keep your final <span class=\"glossaryai-tooltip glossary-term-651\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/image\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> lightweight and secure. This method allows you to build your application as a root user and switch to a non-root user in the final stage.<\/p>\n<pre><code class=\"language-dockerfile\">FROM golang:1.16 AS builder\nWORKDIR \/app\nCOPY . .\nRUN go build -o myapp\n\nFROM alpine:latest\nRUN adduser -D appuser\nWORKDIR \/app\nCOPY --from=builder \/app\/myapp .\nUSER appuser\nCMD [\".\/myapp\"]<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Environment Variables<\/strong>: Be mindful of environment variables that may affect user permissions. Some applications may require specific environment variables to function correctly, which might not be set when running as a non-root user.<\/p>\n<\/li>\n<\/ol>\n<h3>Common Pitfalls and Troubleshooting<\/h3>\n<ol>\n<li>\n<p><strong>Permission Denied Errors<\/strong>: Often, you might encounter permission denied errors when switching to a non-root user. This can occur if the application attempts to access files or directories that require elevated privileges. Always ensure that the necessary permissions are granted.<\/p>\n<\/li>\n<li>\n<p><strong>Missing Dependencies<\/strong>: If your application depends on certain system-level packages or libraries that are only available to the root user during build time, ensure they are installed prior to switching users.<\/p>\n<pre><code class=\"language-dockerfile\"><span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> apt-get update &amp;&amp; apt-get install -y some-package\nUSER appuser<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Running Interactive Applications<\/strong>: When building containers for applications that require interactive sessions, consider how the user context will affect accessibility. Non-root users may not have permission to access certain system resources or interact with the system in ways that a root user can.<\/p>\n<\/li>\n<\/ol>\n<h3>User and Group Management in Docker<\/h3>\n<p>The management of users and groups in Docker can become complex, especially when dealing with multiple services or applications within a <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. Here are some advanced considerations:<\/p>\n<ul>\n<li>\n<p><strong>User Namespaces<\/strong>: Enable user namespaces in Docker to map <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> users to non-root users on the host. This provides an additional layer of security.<\/p>\n<pre><code class=\"language-bash\">{\n\"userns-remap\": \"default\"\n}<\/code><\/pre>\n<\/li>\n<li>\n<p><strong>Group IDs<\/strong>: When specifying group IDs in Dockerfiles, it\u2019s essential to ensure those groups exist within the <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. If a group doesn&#8217;t exist, the <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> might fail to start due to issues with user permissions.<\/p>\n<\/li>\n<\/ul>\n<h3>Real-World Examples<\/h3>\n<p>Consider a web application running in a Docker <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. By default, if the application runs as root, it poses a significant security risk. Here\u2019s how you could structure the <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>:<\/p>\n<pre><code class=\"language-dockerfile\"># Use a lightweight base image\nFROM node:14-alpine\n\n# Create a non-root user and switch to it\nRUN addgroup -S appgroup &amp;&amp; adduser -S appuser -G appgroup\nWORKDIR \/app\n\n# Copy source code and install dependencies\nCOPY package*.json .\/\nRUN npm install\nCOPY . .\n\n# Change ownership of the app directory\nRUN chown -R appuser:appgroup \/app\n\n# Switch to non-root user\nUSER appuser\n\n# Expose the application port\nEXPOSE 3000\n\n# Start the application\nCMD [\"npm\", \"start\"]<\/code><\/pre>\n<p>In this example, a non-root user <code>appuser<\/code> is created, and all operations are conducted under this user, minimizing security risks while maintaining application functionality.<\/p>\n<h3>Conclusion<\/h3>\n<p>The <code>USER<\/code> instruction in a <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> is an essential element in creating secure, manageable, and efficient Docker images. By adhering to best practices like running applications as non-root users, setting the right permissions, and managing dependencies thoughtfully, developers can significantly reduce the attack surface of their applications. As the landscape of application security continues to evolve, mastering the nuances of <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> instructions, including <code>USER<\/code>, will empower developers to build robust and secure containerized applications. The importance of understanding and effectively implementing user management in Docker cannot be overstated, particularly in today&#8217;s security-conscious environment where breaches can have devastating consequences.<\/p>\n<p>By ensuring that containers <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> with the least privileges necessary, developers not only protect their applications but also contribute to a safer ecosystem for all users and services interacting with their applications. The journey to secure containerization begins with understanding and properly utilizing the <code>USER<\/code> instruction in Dockerfiles.<\/p>","protected":false},"excerpt":{"rendered":"<p>In a <span class=\"glossaryai-tooltip glossary-term-652\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\">Un Dockerfile es un script que contiene una serie de instrucciones para automatizar la creaci\u00f3n de im\u00e1genes Docker. Especifica la imagen base, las dependencias de la aplicaci\u00f3n y la configuraci\u00f3n, facilitando el despliegue consistente en diferentes entornos.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/es\/wiki\/dockerfile\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>, la instrucci\u00f3n `USER` especifica la cuenta de usuario bajo la cual el <span class=\"glossaryai-tooltip glossary-term-650\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/wiki\/container\/\" target=\"_blank\">contenedor<\/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\/es\/wiki\/container\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> voluntad <span class=\"glossaryai-tooltip glossary-term-672\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/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\/es\/wiki\/run\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>. Esto mejora la seguridad al reducir los permisos y aislar los procesos, haciendo que tus aplicaciones sean m\u00e1s seguras.<\/p>","protected":false},"author":1,"featured_media":1949,"parent":0,"template":"","glossary-cat":[],"class_list":["post-1317","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 USER - 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\/es\/wiki\/dockerfile-usuario\/\" \/>\n<meta property=\"og:locale\" content=\"es_ES\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Dockerfile USER - Dockerpros\" \/>\n<meta property=\"og:description\" content=\"In a Dockerfile, the `USER` instruction specifies the user account under which the container will run. This enhances security by reducing permissions and isolating processes, making your applications safer.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dockerpros.com\/es\/wiki\/dockerfile-usuario\/\" \/>\n<meta property=\"og:site_name\" content=\"Dockerpros\" \/>\n<meta property=\"article:modified_time\" content=\"2024-07-23T12:23:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-user_1317.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=\"Tiempo de lectura\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/\",\"url\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/\",\"name\":\"Dockerfile USER - Dockerpros\",\"isPartOf\":{\"@id\":\"https:\/\/dockerpros.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-user_1317.jpg\",\"datePublished\":\"2024-07-23T12:20:28+00:00\",\"dateModified\":\"2024-07-23T12:23:18+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#breadcrumb\"},\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#primaryimage\",\"url\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-user_1317.jpg\",\"contentUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-user_1317.jpg\",\"width\":800,\"height\":600,\"caption\":\"dockerfile-user-2\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#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 USER\"}]},{\"@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\":\"es\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/dockerpros.com\/#organization\",\"name\":\"Dockerpros\",\"url\":\"https:\/\/dockerpros.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@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":"USER - Dockerpros\n\nEl comando USER en un Dockerfile se utiliza para establecer el nombre de usuario o UID que se utilizar\u00e1 para ejecutar los comandos RUN, CMD y ENTRYPOINT que siguen en el Dockerfile. Tambi\u00e9n establece el nombre de usuario para cualquier comando RUN que utilice la opci\u00f3n --user.\n\nSi el nombre de usuario no existe, se crea una cuenta de usuario con ese nombre y se le asigna una contrase\u00f1a aleatoria.\n\nEl comando USER solo afecta al usuario para los comandos RUN, CMD y ENTRYPOINT que siguen en el Dockerfile. No afecta al usuario para el comando docker run.\n\nSi no se especifica un usuario en el Dockerfile, los comandos se ejecutar\u00e1n como root.\n\nEjemplo:\n\n```\nFROM ubuntu\nRUN useradd -ms \/bin\/bash user1\nUSER user1\nRUN echo \"Hello from user1\"\nUSER root\nRUN echo \"Hello from root\"\n```\n\nEn este ejemplo, el primer comando RUN se ejecutar\u00e1 como el usuario user1, y el segundo comando RUN se ejecutar\u00e1 como el usuario root.\n\nEl comando USER tambi\u00e9n se puede utilizar para cambiar el usuario para un comando RUN espec\u00edfico utilizando la opci\u00f3n --user:\n\n```\nFROM ubuntu\nRUN useradd -ms \/bin\/bash user1\nRUN --user user1 echo \"Hello from user1\"\nRUN echo \"Hello from root\"\n```\n\nEn este ejemplo, el primer comando RUN se ejecutar\u00e1 como el usuario user1, y el segundo comando RUN se ejecutar\u00e1 como el usuario root.\n\nEs importante tener en cuenta que el comando USER solo afecta al usuario para los comandos RUN, CMD y ENTRYPOINT que siguen en el Dockerfile. No afecta al usuario para el comando docker run.\n\nSi necesitas ejecutar un contenedor como un usuario espec\u00edfico, puedes utilizar la opci\u00f3n -u o --user con el comando docker run:\n\n```\ndocker run -u user1 myimage\n```\n\nEn este ejemplo, el contenedor se ejecutar\u00e1 como el usuario user1.","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\/es\/wiki\/dockerfile-usuario\/","og_locale":"es_ES","og_type":"article","og_title":"Dockerfile USER - Dockerpros","og_description":"In a Dockerfile, the `USER` instruction specifies the user account under which the container will run. This enhances security by reducing permissions and isolating processes, making your applications safer.","og_url":"https:\/\/dockerpros.com\/es\/wiki\/dockerfile-usuario\/","og_site_name":"Dockerpros","article_modified_time":"2024-07-23T12:23:18+00:00","og_image":[{"width":800,"height":600,"url":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-user_1317.jpg","type":"image\/jpeg"}],"twitter_card":"summary_large_image","twitter_misc":{"Tiempo de lectura":"5 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/","url":"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/","name":"USER - Dockerpros\n\nEl comando USER en un Dockerfile se utiliza para establecer el nombre de usuario o UID que se utilizar\u00e1 para ejecutar los comandos RUN, CMD y ENTRYPOINT que siguen en el Dockerfile. Tambi\u00e9n establece el nombre de usuario para cualquier comando RUN que utilice la opci\u00f3n --user.\n\nSi el nombre de usuario no existe, se crea una cuenta de usuario con ese nombre y se le asigna una contrase\u00f1a aleatoria.\n\nEl comando USER solo afecta al usuario para los comandos RUN, CMD y ENTRYPOINT que siguen en el Dockerfile. No afecta al usuario para el comando docker run.\n\nSi no se especifica un usuario en el Dockerfile, los comandos se ejecutar\u00e1n como root.\n\nEjemplo:\n\n```\nFROM ubuntu\nRUN useradd -ms \/bin\/bash user1\nUSER user1\nRUN echo \"Hello from user1\"\nUSER root\nRUN echo \"Hello from root\"\n```\n\nEn este ejemplo, el primer comando RUN se ejecutar\u00e1 como el usuario user1, y el segundo comando RUN se ejecutar\u00e1 como el usuario root.\n\nEl comando USER tambi\u00e9n se puede utilizar para cambiar el usuario para un comando RUN espec\u00edfico utilizando la opci\u00f3n --user:\n\n```\nFROM ubuntu\nRUN useradd -ms \/bin\/bash user1\nRUN --user user1 echo \"Hello from user1\"\nRUN echo \"Hello from root\"\n```\n\nEn este ejemplo, el primer comando RUN se ejecutar\u00e1 como el usuario user1, y el segundo comando RUN se ejecutar\u00e1 como el usuario root.\n\nEs importante tener en cuenta que el comando USER solo afecta al usuario para los comandos RUN, CMD y ENTRYPOINT que siguen en el Dockerfile. No afecta al usuario para el comando docker run.\n\nSi necesitas ejecutar un contenedor como un usuario espec\u00edfico, puedes utilizar la opci\u00f3n -u o --user con el comando docker run:\n\n```\ndocker run -u user1 myimage\n```\n\nEn este ejemplo, el contenedor se ejecutar\u00e1 como el usuario user1.","isPartOf":{"@id":"https:\/\/dockerpros.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#primaryimage"},"image":{"@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#primaryimage"},"thumbnailUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-user_1317.jpg","datePublished":"2024-07-23T12:20:28+00:00","dateModified":"2024-07-23T12:23:18+00:00","breadcrumb":{"@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#breadcrumb"},"inLanguage":"es","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dockerpros.com\/wiki\/dockerfile-user\/"]}]},{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#primaryimage","url":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-user_1317.jpg","contentUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/dockerfile-user_1317.jpg","width":800,"height":600,"caption":"dockerfile-user-2"},{"@type":"BreadcrumbList","@id":"https:\/\/dockerpros.com\/wiki\/dockerfile-user\/#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 USER"}]},{"@type":"WebSite","@id":"https:\/\/dockerpros.com\/#website","url":"https:\/\/dockerpros.com\/","name":"Profesionales de Docker","description":"DockerPros \u2013 Tu centro definitivo de recursos Docker","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":"es"},{"@type":"Organization","@id":"https:\/\/dockerpros.com\/#organization","name":"Profesionales de Docker","url":"https:\/\/dockerpros.com\/","logo":{"@type":"ImageObject","inLanguage":"es","@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\/es\/wp-json\/wp\/v2\/glossary\/1317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/glossary"}],"about":[{"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/types\/glossary"}],"author":[{"embeddable":true,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":0,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/glossary\/1317\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/media\/1949"}],"wp:attachment":[{"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/media?parent=1317"}],"wp:term":[{"taxonomy":"glossary-cat","embeddable":true,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/glossary-cat?post=1317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}