目錄
Defining environment variables in a Dockerfile
Managing environment variables with Docker Compose
首頁 運維 Docker 您如何在Docker容器中指定環境變量?

您如何在Docker容器中指定環境變量?

Jun 28, 2025 am 12:22 AM
docker 環境變數

在Docker容器中設置環境變量有三種常見方式:使用-e標誌、在Dockerfile中定義ENV指令、或通過Docker Compose管理。 1. 使用docker run時添加-e標誌可直接傳入變量,適合臨時測試或CI/CD集成;2. 在Dockerfile中使用ENV設置默認值,適用於不常更改的固定變量,但不適合區分不同環境配置;3. Docker Compose可通過environment塊或.env文件定義變量,後者更利於開發協作和配置分離,並支持變量替換。根據項目需求選擇合適方法或組合使用多種方式。

You can specify environment variables in a Docker container in several practical ways, depending on your setup and needs. The most common methods include using the -e flag when running a container, defining them in a Dockerfile with ENV , or managing them via a .env file when using Docker Compose.

Using the -e flag with docker run

When you start a container manually using docker run , you can pass environment variables directly on the command line with the -e flag.

For example:

 docker run -d -e MY_VAR="hello" my-app

This sets the variable MY_VAR to "hello" inside the container.

  • You can set multiple variables by repeating the -e flag.
  • This is useful for one-off containers or testing different configurations.
  • It's also helpful when integrating with CI/CD systems where secrets or settings are injected dynamically.

If you're setting sensitive data like API keys or passwords, be cautious about exposing them in shell history or logs.

Defining environment variables in a Dockerfile

You can also define default environment variables directly in your Dockerfile using the ENV instruction.

Example:

 ENV MY_VAR hello
ENV LOG_LEVEL debug

These values will be baked into the image and available in every container started from it unless overridden at runtime.

  • This method is good for setting defaults that rarely change.
  • It's not ideal if you need different values across environments (like dev vs prod).
  • Keep in mind these values are visible in the image metadata, so avoid putting sensitive info here.

Managing environment variables with Docker Compose

When working with Docker Compose, you can define environment variables in two main ways:

  1. Directly in the environment block of your docker-compose.yml :

     services:
      app:
        image: my-app
        environment:
          MY_VAR: "hello"
  2. Using an external .env file with the env_file option:

     services:
      app:
        image: my-app
        env_file:
          - .env

The second approach helps keep your configuration clean and portable. The .env file looks like this:

 MY_VAR=hello
LOG_LEVEL=debug
  • This is great for local development and team setups.
  • Make sure to add .env files to .gitignore if they contain sensitive data.
  • Docker Compose supports variable substitution too, so you can reference existing shell variables.

That's how you handle environment variables in Docker — through CLI flags, Dockerfiles, or compose files. Each method has its use case, and often you'll combine them depending on your project's complexity.

以上是您如何在Docker容器中指定環境變量?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

.NET Core快速入門教程 1、開篇:說說.NET Core的那些事兒 .NET Core快速入門教程 1、開篇:說說.NET Core的那些事兒 May 07, 2025 pm 04:54 PM

一、.NETCore的起源談到.NETCore,就不能不提它的前身.NET。當年Java風頭正盛,微軟也對Java青睞有加,Windows平台上的Java虛擬機就是微軟依據JVM標准開發的,據稱是當時性能最佳的Java虛擬機。然而,微軟有自己的小算盤,試圖將Java與Windows平台捆綁,增加一些Windows特有的功能。 Sun公司對此不滿,導致雙方關係破裂,微軟隨後推出了.NET。 .NET從誕生之初就借鑒了Java的許多特性,並在語言特性和窗體開發等方面逐漸超越了Java。 Java在1.6版

Linux上的Docker:Linux系統的容器化 Linux上的Docker:Linux系統的容器化 Apr 22, 2025 am 12:03 AM

Docker在Linux上重要,因為Linux是其原生平台,提供了豐富的工具和社區支持。 1.安裝Docker:使用sudoapt-getupdate和sudoapt-getinstalldocker-cedocker-ce-clicontainerd.io。 2.創建和管理容器:使用dockerrun命令,如dockerrun-d--namemynginx-p80:80nginx。 3.編寫Dockerfile:優化鏡像大小,使用多階段構建。 4.優化和調試:使用dockerlogs和dockerex

Docker vs. Kubernetes:主要差異和協同作用 Docker vs. Kubernetes:主要差異和協同作用 May 01, 2025 am 12:09 AM

Docker和Kubernetes是容器化和編排的領軍者。 Docker專注於容器生命週期管理,適合小型項目;Kubernetes則擅長容器編排,適用於大規模生產環境。兩者結合可提升開發和部署效率。

怎樣開發一個完整的PythonWeb應用程序? 怎樣開發一個完整的PythonWeb應用程序? May 23, 2025 pm 10:39 PM

要開發一個完整的PythonWeb應用程序,應遵循以下步驟:1.選擇合適的框架,如Django或Flask。 2.集成數據庫,使用ORM如SQLAlchemy。 3.設計前端,使用Vue或React。 4.進行測試,使用pytest或unittest。 5.部署應用,使用Docker和平台如Heroku或AWS。通過這些步驟,可以構建出功能強大且高效的Web應用。

C  中的交叉編譯是什麼? C 中的交叉編譯是什麼? Apr 28, 2025 pm 08:21 PM

C 中的交叉編譯是指在一個平台上編譯出可以在另一個平台上運行的可執行文件或庫。 1)交叉編譯需要使用專門的交叉編譯器,如GCC或Clang的變體。 2)設置交叉編譯環境可以使用Docker來管理工具鏈,提高可重複性和可移植性。 3)交叉編譯時需注意代碼優化選項,如-O2、-O3或-Os,以平衡性能和文件大小。

容器化技術(例如Docker)如何影響Java平台獨立性的重要性? 容器化技術(例如Docker)如何影響Java平台獨立性的重要性? Apr 22, 2025 pm 06:49 PM

容器化技術如Docker增強而非替代Java的平台獨立性。 1)確保跨環境的一致性,2)管理依賴性,包括特定JVM版本,3)簡化部署過程,使Java應用更具適應性和易管理性。

查看Docker容器內部進程信息的方法 查看Docker容器內部進程信息的方法 May 19, 2025 pm 09:06 PM

查看Docker容器內部進程信息有三種方法:1.使用dockertop命令,可以列出容器內所有進程,顯示PID、用戶、命令等信息;2.使用dockerexec進入容器內部,再用ps或top命令查看詳細進程信息;3.使用dockerstats命令,實時顯示容器資源使用情況,結合dockertop可全面了解容器性能。

為什麼要使用Docker?解釋的好處和優勢 為什麼要使用Docker?解釋的好處和優勢 Apr 25, 2025 am 12:05 AM

使用Docker的原因是它提供高效、便攜且一致的環境來打包、分發和運行應用程序。 1)Docker是一種容器化平台,允許開發者將應用程序及其依賴項打包到輕量級、可移植的容器中。 2)它基於Linux容器技術和聯合文件系統,確保快速啟動和高效運行。 3)Docker支持多階段構建,優化鏡像大小和部署速度。 4)使用Docker可以簡化開發和部署流程,提高效率並確保跨環境的一致性。

See all articles