{"id":214,"date":"2024-07-21T11:46:24","date_gmt":"2024-07-21T11:46:24","guid":{"rendered":"https:\/\/dockerpros.com\/?p=214"},"modified":"2024-07-21T11:46:24","modified_gmt":"2024-07-21T11:46:24","slug":"como-gestiono-las-variables-de-entorno-en-docker","status":"publish","type":"post","link":"https:\/\/dockerpros.com\/es\/security\/how-do-i-manage-environment-variables-in-docker\/","title":{"rendered":"How do I manage environment variables in Docker?"},"content":{"rendered":"<h1>Managing Environment Variables in Docker: An Advanced Guide<\/h1>\n<p>Docker has revolutionized the way we deploy applications, offering a lightweight and portable alternative to traditional VM-based environments. One of the core concepts in Docker is the use of environment variables, which play a vital role in configuring containers at runtime. This article aims to provide an in-depth understanding of how to manage environment variables in Docker, exploring various methods, best practices, and common pitfalls.<\/p>\n<h2>What Are Environment Variables?<\/h2>\n<p>Environment variables are dynamic values that can affect the way running processes will behave on a computer. They are commonly used to configure applications, define settings for services, and pass sensitive information like <span class=\"glossaryai-tooltip glossary-term-1249\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/wiki\/api\/\" target=\"_blank\">API<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An API, or Application Programming Interface, enables software applications to communicate and interact with each other. It defines protocols and tools for building software and facilitating integration.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/es\/wiki\/api\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> keys and passwords. In the context of Docker, environment variables allow you to customize the behavior of your containerized applications without hardcoding configuration values into your images.<\/p>\n<h2>Why Use Environment Variables in Docker?<\/h2>\n<ol>\n<li>\n<p><strong>Separation of Concerns<\/strong>: By using environment variables, you can separate your application code from its configuration. This promotes better maintainability and scalability.<\/p>\n<\/li>\n<li>\n<p><strong>Security<\/strong>: Sensitive information can be passed to containers as environment variables, reducing the risk of exposing them in your codebase.<\/p>\n<\/li>\n<li>\n<p><strong>Flexibility<\/strong>: Environment variables allow you to easily adjust the configuration of containers at runtime. For instance, you can <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> the same <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> in different environments (development, testing, production) with varying settings.<\/p>\n<\/li>\n<li>\n<p><strong>Portability<\/strong>: Since environment variables are defined at runtime, the same Docker <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> can be used across different systems without modification.<\/p>\n<\/li>\n<\/ol>\n<h2>How to Set Environment Variables in Docker<\/h2>\n<h3>1. Dockerfile<\/h3>\n<p>You can define environment variables directly in your <code><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><\/code> using the <code><span class=\"glossaryai-tooltip glossary-term-671\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/wiki\/env\/\" target=\"_blank\">ENV<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">ENV, or Environmental Variables, are crucial in software development and system configuration. They store dynamic values that affect the execution environment, enabling flexible application behavior across different platforms.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/es\/wiki\/env\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span><\/code> instruction. This is particularly useful for setting default values.<\/p>\n<pre><code class=\"language-Dockerfile\"># Sample Dockerfile\nFROM python:3.8-slim\n\n# Set environment variables\nENV APP_ENV=production\nENV DB_HOST=db.example.com\n\n# Copy application code\nCOPY . \/app\n\n# Set the working directory\nWORKDIR \/app\n\n# Install dependencies\nRUN pip install -r requirements.txt\n\n# Command to run your application\nCMD [\"python\", \"app.py\"]<\/code><\/pre>\n<p>In this example, <code>APP_ENV<\/code> and <code>DB_HOST<\/code> are set as environment variables that can be accessed by your application during runtime.<\/p>\n<h3>2. Docker Run Command<\/h3>\n<p>You can also pass environment variables at the time of <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> creation using the <code>-e<\/code> or <code>--env<\/code> flag with the <code>docker <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><\/code> command.<\/p>\n<pre><code class=\"language-bash\">docker <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> -e APP_ENV=development -e DB_HOST=db.local my_image<\/code><\/pre>\n<p>This approach allows you to override the values set in the <code><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><\/code>.<\/p>\n<h3>3. Environment File<\/h3>\n<p>For managing multiple environment variables, you can create an <code>.env<\/code> file and use the <code>--env-file<\/code> option with <code>docker <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><\/code>.<\/p>\n<pre><code class=\"language-plaintext\"># .env file\nAPP_ENV=production\nDB_HOST=db.example.com\nAPI_KEY=mysecretapikey<\/code><\/pre>\n<p>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> 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> with this environment file:<\/p>\n<pre><code class=\"language-bash\">docker <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> --env-file .env my_image<\/code><\/pre>\n<h3>4. Docker Compose<\/h3>\n<p><span class=\"glossaryai-tooltip glossary-term-654\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/wiki\/docker-compose\/\" target=\"_blank\">Docker Compose<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Docker Compose is a tool for defining and running multi-container Docker applications using a YAML file. It simplifies deployment, configuration, and orchestration of services, enhancing development efficiency.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/es\/wiki\/docker-compose\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> simplifies the process of defining and running multi-container Docker applications. You can specify environment variables directly in the <code>docker-compose.yml<\/code> file.<\/p>\n<pre><code class=\"language-yaml\">version: '3.8'\n\nservices:\n  web:\n    image: my_image\n    environment:\n      APP_ENV: production\n      DB_HOST: db.example.com\n    ports:\n      - \"5000:5000\"<\/code><\/pre>\n<p>Alternatively, you can reference an external <code>.env<\/code> file:<\/p>\n<pre><code class=\"language-yaml\">version: '3.8'\n\nservices:\n  web:\n    image: my_image\n    env_file:\n      - .env\n    ports:\n      - \"5000:5000\"<\/code><\/pre>\n<h2>Best Practices for Managing Environment Variables<\/h2>\n<p>While environment variables are powerful, it&#8217;s essential to manage them correctly to ensure security, maintainability, and ease of use. Here are some best practices:<\/p>\n<h3>1. Use Default Values<\/h3>\n<p>When defining environment variables in your <code><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><\/code>, consider providing default values. This ensures that your application has a fallback in case no value is provided at runtime.<\/p>\n<h3>2. Keep Sensitive Data Secure<\/h3>\n<p>Avoid hardcoding sensitive data like passwords or <span class=\"glossaryai-tooltip glossary-term-1249\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/wiki\/api\/\" target=\"_blank\">API<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">An API, or Application Programming Interface, enables software applications to communicate and interact with each other. It defines protocols and tools for building software and facilitating integration.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/es\/wiki\/api\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> keys in your <code><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><\/code> or codebase. Instead, use environment variables passed at runtime or leverage Docker secrets for sensitive information in <span class=\"glossaryai-tooltip glossary-term-657\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/wiki\/orchestration\/\" target=\"_blank\">orchestration<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Orchestration refers to the automated management and coordination of complex systems and services. It optimizes processes by integrating various components, ensuring efficient operation and resource utilization.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/es\/wiki\/orchestration\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> platforms like <span class=\"glossaryai-tooltip glossary-term-655\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/wiki\/docker-swarm\/\" target=\"_blank\">Docker Swarm<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Docker Swarm is a container orchestration tool that enables the management of a cluster of Docker engines. It simplifies scaling and deployment, ensuring high availability and load balancing across services.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/es\/wiki\/docker-swarm\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span>.<\/p>\n<h3>3. Document Your Environment Variables<\/h3>\n<p>Keep a well-maintained documentation file outlining each environment variable, its purpose, and its possible values. This is especially important for team projects.<\/p>\n<h3>4. Use Meaningful Names<\/h3>\n<p>Naming conventions are important. Use clear and descriptive names for your environment variables. This helps developers understand the configuration without having to dig into the code.<\/p>\n<h3>5. Limit Variable Scope<\/h3>\n<p>Whenever possible, limit the scope of environment variables to the services that require them. This practice helps to minimize potential security risks associated with exposing sensitive configuration details to unnecessary services.<\/p>\n<h2>Common Pitfalls<\/h2>\n<ol>\n<li>\n<p><strong>Overwriting Variables<\/strong>: Be cautious when passing environment variables at runtime. If you set a variable in both your <code><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><\/code> and your <code>docker <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><\/code> command, the latter will take precedence, which could lead to unexpected behavior if not managed carefully.<\/p>\n<\/li>\n<li>\n<p><strong>Using Non-String Values<\/strong>: Docker environment variables are always treated as strings. If your application expects a different type (like a boolean or integer), ensure proper conversion in your application code.<\/p>\n<\/li>\n<li>\n<p><strong>Ignoring Local Development<\/strong>: Local development and testing should mimic production as closely as possible. To achieve this, use the same environment variable configuration (e.g., through <code>.env<\/code> files) to ensure consistency.<\/p>\n<\/li>\n<li>\n<p><strong>Not Utilizing Docker Secrets<\/strong>: For sensitive data, consider using Docker secrets instead, especially if you&#8217;re deploying in a swarm mode. Secrets are encrypted and only available to services that explicitly need them.<\/p>\n<\/li>\n<\/ol>\n<h2>Accessing Environment Variables in Application Code<\/h2>\n<p>Once your environment variables are set up, accessing them from your application code is straightforward. Most programming languages provide built-in libraries for reading environment variables.<\/p>\n<h3>Accessing in Python<\/h3>\n<pre><code class=\"language-python\">import os\n\napp_env = os.getenv('APP_ENV', 'development')\ndb_host = os.getenv('DB_HOST', 'localhost')\n\nprint(f'App Environment: {app_env}')\nprint(f'Database Host: {db_host}')<\/code><\/pre>\n<h3>Accessing in Node.js<\/h3>\n<pre><code class=\"language-javascript\">const appEnv = process.env.APP_ENV || 'development';\nconst dbHost = process.env.DB_HOST || 'localhost';\n\nconsole.log(`App Environment: ${appEnv}`);\nconsole.log(`Database Host: ${dbHost}`);<\/code><\/pre>\n<h3>Accessing in Java<\/h3>\n<pre><code class=\"language-java\">public class Main {\n    public static void main(String[] args) {\n        String appEnv = System.getenv(\"APP_ENV\");\n        String dbHost = System.getenv(\"DB_HOST\");\n\n        System.out.println(\"App Environment: \" + (appEnv != null ? appEnv : \"development\"));\n        System.out.println(\"Database Host: \" + (dbHost != null ? dbHost : \"localhost\"));\n    }\n}<\/code><\/pre>\n<h2>Conclusion<\/h2>\n<p>Environment variables are a powerful feature in Docker that facilitates flexible, secure, and maintainable application configuration. By mastering the various methods for managing environment variables and adhering to best practices, you can enhance your development workflow and ensure that your applications are robust and adaptable.<\/p>\n<p>As with any tool, understanding the strengths and limitations of environment variables will empower you to build better containerized applications. Whether you are deploying a simple web <span class=\"glossaryai-tooltip glossary-term-681\"><span class=\"glossaryai-link\"><a href=\"https:\/\/dockerpros.com\/es\/wiki\/service\/\" target=\"_blank\">service<\/a><\/span><span class=\"gai-content-hidden glossaryai-tooltip-content\"><span class=\"gai-tooltip-body\"><span class=\"glossaryai-tooltip-text\">Service refers to the act of providing assistance or support to fulfill specific needs or requirements. In various domains, it encompasses customer service, technical support, and professional services, emphasizing efficiency and user satisfaction.<span class=\"glossaryai-more-link\"> <a href=\"https:\/\/dockerpros.com\/es\/wiki\/service\/\">More \u00bb<\/a><\/span><\/span><\/span><\/span><\/span> or a complex microservices architecture, effective management of environment variables will be a key component of your success in the Docker ecosystem.<\/p>","protected":false},"excerpt":{"rendered":"<p>La gesti\u00f3n de variables de entorno en Docker se puede realizar utilizando la opci\u00f3n `-e` en `docker run`, o defini\u00e9ndolas en un archivo `.env` o en `docker-compose.yml`, asegurando una configuraci\u00f3n segura.<\/p>","protected":false},"author":1,"featured_media":308,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-214","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-security"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How do I manage environment variables in Docker? - 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\/seguridad\/como-gestiono-las-variables-de-entorno-en-docker\/\" \/>\n<meta property=\"og:locale\" content=\"es_ES\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How do I manage environment variables in Docker? - Dockerpros\" \/>\n<meta property=\"og:description\" content=\"Managing environment variables in Docker can be done using the `-e` flag in `docker run`, or by defining them in a `.env` file or `docker-compose.yml`, ensuring secure configuration.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dockerpros.com\/es\/seguridad\/como-gestiono-las-variables-de-entorno-en-docker\/\" \/>\n<meta property=\"og:site_name\" content=\"Dockerpros\" \/>\n<meta property=\"article:published_time\" content=\"2024-07-21T11:46:24+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.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=\"author\" content=\"dockerpros\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Escrito por\" \/>\n\t<meta name=\"twitter:data1\" content=\"dockerpros\" \/>\n\t<meta name=\"twitter:label2\" content=\"Tiempo de lectura\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutos\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/\"},\"author\":{\"name\":\"dockerpros\",\"@id\":\"https:\/\/dockerpros.com\/#\/schema\/person\/a9b4c3d7f7a8e2b072e77d47b382a3a4\"},\"headline\":\"How do I manage environment variables in Docker?\",\"datePublished\":\"2024-07-21T11:46:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/\"},\"wordCount\":832,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/dockerpros.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.jpg\",\"articleSection\":[\"Security\"],\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/\",\"url\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/\",\"name\":\"How do I manage environment variables in Docker? - Dockerpros\",\"isPartOf\":{\"@id\":\"https:\/\/dockerpros.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.jpg\",\"datePublished\":\"2024-07-21T11:46:24+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#breadcrumb\"},\"inLanguage\":\"es\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#primaryimage\",\"url\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.jpg\",\"contentUrl\":\"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.jpg\",\"width\":800,\"height\":600,\"caption\":\"how-do-i-manage-environment-variables-in-docker-2\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/dockerpros.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How do I manage environment variables in Docker?\"}]},{\"@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\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/dockerpros.com\/#\/schema\/person\/a9b4c3d7f7a8e2b072e77d47b382a3a4\",\"name\":\"dockerpros\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"es\",\"@id\":\"https:\/\/dockerpros.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/dockerpros.com\/wp-content\/litespeed\/avatar\/d13b9d4f101de1a7535b404e0c59affd.jpg?ver=1780577253\",\"contentUrl\":\"https:\/\/dockerpros.com\/wp-content\/litespeed\/avatar\/d13b9d4f101de1a7535b404e0c59affd.jpg?ver=1780577253\",\"caption\":\"dockerpros\"},\"sameAs\":[\"https:\/\/dockerpros.com\/\"],\"url\":\"https:\/\/dockerpros.com\/es\/author\/dockerpros\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u00bfC\u00f3mo gestiono las variables de entorno en Docker? - 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\/es\/seguridad\/como-gestiono-las-variables-de-entorno-en-docker\/","og_locale":"es_ES","og_type":"article","og_title":"How do I manage environment variables in Docker? - Dockerpros","og_description":"Managing environment variables in Docker can be done using the `-e` flag in `docker run`, or by defining them in a `.env` file or `docker-compose.yml`, ensuring secure configuration.","og_url":"https:\/\/dockerpros.com\/es\/seguridad\/como-gestiono-las-variables-de-entorno-en-docker\/","og_site_name":"Dockerpros","article_published_time":"2024-07-21T11:46:24+00:00","og_image":[{"width":800,"height":600,"url":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.jpg","type":"image\/jpeg"}],"author":"dockerpros","twitter_card":"summary_large_image","twitter_misc":{"Escrito por":"dockerpros","Tiempo de lectura":"5 minutos"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#article","isPartOf":{"@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/"},"author":{"name":"dockerpros","@id":"https:\/\/dockerpros.com\/#\/schema\/person\/a9b4c3d7f7a8e2b072e77d47b382a3a4"},"headline":"How do I manage environment variables in Docker?","datePublished":"2024-07-21T11:46:24+00:00","mainEntityOfPage":{"@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/"},"wordCount":832,"commentCount":0,"publisher":{"@id":"https:\/\/dockerpros.com\/#organization"},"image":{"@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#primaryimage"},"thumbnailUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.jpg","articleSection":["Security"],"inLanguage":"es","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/","url":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/","name":"\u00bfC\u00f3mo gestiono las variables de entorno en Docker? - Dockerpros","isPartOf":{"@id":"https:\/\/dockerpros.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#primaryimage"},"image":{"@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#primaryimage"},"thumbnailUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.jpg","datePublished":"2024-07-21T11:46:24+00:00","breadcrumb":{"@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#breadcrumb"},"inLanguage":"es","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/"]}]},{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#primaryimage","url":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.jpg","contentUrl":"https:\/\/dockerpros.com\/wp-content\/uploads\/2024\/07\/how-do-i-manage-environment-variables-in-docker_214.jpg","width":800,"height":600,"caption":"how-do-i-manage-environment-variables-in-docker-2"},{"@type":"BreadcrumbList","@id":"https:\/\/dockerpros.com\/security\/how-do-i-manage-environment-variables-in-docker\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dockerpros.com\/"},{"@type":"ListItem","position":2,"name":"How do I manage environment variables in Docker?"}]},{"@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\/"}},{"@type":"Person","@id":"https:\/\/dockerpros.com\/#\/schema\/person\/a9b4c3d7f7a8e2b072e77d47b382a3a4","name":"profesionales de Docker","image":{"@type":"ImageObject","inLanguage":"es","@id":"https:\/\/dockerpros.com\/#\/schema\/person\/image\/","url":"https:\/\/dockerpros.com\/wp-content\/litespeed\/avatar\/d13b9d4f101de1a7535b404e0c59affd.jpg?ver=1780577253","contentUrl":"https:\/\/dockerpros.com\/wp-content\/litespeed\/avatar\/d13b9d4f101de1a7535b404e0c59affd.jpg?ver=1780577253","caption":"dockerpros"},"sameAs":["https:\/\/dockerpros.com\/"],"url":"https:\/\/dockerpros.com\/es\/author\/dockerpros\/"}]}},"_links":{"self":[{"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/posts\/214","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/comments?post=214"}],"version-history":[{"count":0,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/posts\/214\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/media\/308"}],"wp:attachment":[{"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/media?parent=214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/categories?post=214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dockerpros.com\/es\/wp-json\/wp\/v2\/tags?post=214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}