SoFunction
Updated on 2025-03-09

How to configure environment variables for containers in files

1. Set environment variables directly

version: "3"
services:
  web:
    image: my - web - app:v1.0
    environment:
      - DEBUG = false
      - API_KEY = "abcdef123456"

Syntax format

In the service definition section, you can useenvironmentKeywords to set environment variables.

For example:

Explanation

  • Here is a name calledwebservice, it usesmy - web - app:v1.0Mirror.
  • environmentEach line below defines an environment variable.
  • DEBUG = falseSet a name calledDEBUGThe Boolean environment variable isfalseAPI_KEY = "abcdef123456"Set a name calledAPI_KEYThe string environment variable whose value isabcdef123456
  • After the container is started, these environment variables will be read and used by the application in the container. The application can adjust its behavior based on the values ​​of these variables, such asDEBUG = falseTurn off debug mode when .

2. Read environment variables (.env file) from the file

DB_USER = myuser
DB_PASSWORD = mypassword
DB_HOST = database - service
DB_PORT = 3306
DB_NAME = mydatabase

create.envdocument

existdocker - Create a file in the directory where it is located.envdocument.

For example:

existdocker - References in the file

version: "3"
services:
  web:
    image: my - web - app:v1.0
    environment:
      - DB_USER = ${DB_USER}
      - DB_PASSWORD = ${DB_PASSWORD}
      - DB_HOST = ${DB_HOST}
      - DB_PORT = ${DB_PORT}
      - DB_NAME = ${DB_NAME}

Explanation

  • .envFiles are used to store key-value pairs of environment variables, which makes the management of environment variables more convenient, especially when there are multiple environment variables or the values ​​of environment variables may change frequently.
  • existdocker - In the file, through${variable name}Form to quote.envEnvironment variables in the file.
  • For example,DB_USER = ${DB_USER}Will put the container inDB_USERThe value of the environment variable is set to.envIn the fileDB_USERThe value of (here ismyuser)。
  • In this way, when it is necessary to modify the value of the environment variable, only modify.envFiles, not required indocker - Modify one by one in the file.

3. Use multiple environment files (for different deployment environments)

# 
DEBUG = true
APP_PORT = 3000
# 
DEBUG = false
APP_PORT = 80

Create multiple environment files

For example, createUsed in the development environment,Used in production environment.

existdocker - Specify the environment file in the file (using--env - fileOptions)

docker - compose --env - file  up -d
docker - compose --env - file  up -d

This method is started via the command linedocker - composeWhen used.

For example, start in a development environment:

Or start in production:

Explanation

  • By specifying different environment files, you can easily switch between different deployment environments (such as development, testing, production).
  • Each environment file can contain environment variable settings specific to that environment, allowing the application to adjust its behavior according to the environment it is in.
  • For example, in a development environment, you canDEBUGSet astrueFor easy debugging, set it tofalsefor improved performance and security.

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.