在Docker容器中使用外部配置

280 阅读1分钟
  1. 挂载外部配置文件:可以将外部配置文件直接挂载到Docker容器中,通过 -v 参数指定挂载的外部文件路径和容器内的目标路径。例如:
docker run -v /path/to/configfile:/path/in/container image_name
# example
docker run -v /www/wwwroot/docker/.env:/app/.env my-frontend:latest
  1. 使用Docker环境变量:可以在Docker容器中使用环境变量来传递配置信息,可以在Dockerfile中指定环境变量,或者在运行容器时通过 -e 参数传递环境变量。例如:
docker run -e CONFIG_VAR=value image_name
# example
docker run -e SERVER_API=https://api.com my-frontend:latest
  1. 使用Docker配置服务:Docker提供了配置服务(Config Service)来管理配置文件,可以使用 docker config create 命令创建配置文件,然后在运行容器时通过 --config 参数指定配置文件。例如:
docker config create my_config /path/to/configfile
docker run --config source=my_config,target=/path/in/container image_name
# example
docker config create my_config /www/wwwroot/docker/.env
docker run --config source=my_config,target=/app/.env my-frontend:latest

docker如何使用外部配置文件