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 useenvironment
Keywords to set environment variables.
For example:
Explanation:
- Here is a name called
web
service, it usesmy - web - app:v1.0
Mirror. -
environment
Each line below defines an environment variable. -
DEBUG = false
Set a name calledDEBUG
The Boolean environment variable isfalse
,API_KEY = "abcdef123456"
Set a name calledAPI_KEY
The 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 as
DEBUG = false
Turn 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.env
document:
existdocker -
Create a file in the directory where it is located.env
document.
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:
-
.env
Files 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. - exist
docker -
In the file, through${variable name}
Form to quote.env
Environment variables in the file. - For example,
DB_USER = ${DB_USER}
Will put the container inDB_USER
The value of the environment variable is set to.env
In the fileDB_USER
The value of (here ismyuser
)。 - In this way, when it is necessary to modify the value of the environment variable, only modify
.env
Files, 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 - file
Options):
docker - compose --env - file up -d docker - compose --env - file up -d
This method is started via the command linedocker - compose
When 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 can
DEBUG
Set astrue
For easy debugging, set it tofalse
for 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.