[英]How to mount Docker container volumes
My requirement is like this:我的要求是这样的:
Q) I have a little python
app running a small flask
server on a docker container.问)我有一个小的
python
应用程序在 docker 容器上运行一个小型flask
服务器。 I need to pass a value to this container that will return from the python server.我需要向这个容器传递一个值,该值将从 python 服务器返回。 but I need to change this passing value occasionally and I DON'T WANT TO build the image every time I do a change to this passing value.
但我需要偶尔更改此传递值,并且我不想每次更改此传递值时都构建图像。
For this what I have done this set .env
file and set the environment variables I need to pass like below:为此,我已经完成了这个设置
.env
文件并设置了我需要传递的环境变量,如下所示:
this is my .env
:这是我的
.env
:
MY_NAME=John McBeth
This is my server.py
:这是我的
server.py
:
from flask import Flask
app = Flask(__name__)
import os
PORT = 8083
return_value = os.environ(['MY_NAME']) # value I am getting from .env
@app.route('/')
def hello_world():
return str(return_value)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=PORT)
And I am building this from a docker-compose.yaml
as below:我正在从
docker-compose.yaml
构建它,如下所示:
version: "3.2"
services:
backend:
build: ./backends/banuka
environment:
CHOKIDAR_USEPOLLING: "true"
MY_NAME: ${MY_NAME} // this is where I am passing the value in `.env`
ports:
- 8080:8083
PROBLEM: How can I set a volume to this container, that every time I change this name in .env
it will reflect right after without building the image again and running?问题:我如何为这个容器设置一个卷,每次我在
.env
中更改这个名称时,它会立即反映,而无需再次构建图像并运行?
Change your docker-compose.yml
to:将您的
docker-compose.yml
更改为:
version: "3.2"
services:
backend:
build: ./backends/banuka
volumes:
- .:/path/to/project
environment:
CHOKIDAR_USEPOLLING: "true"
env_file:
- .env
ports:
- 8080:8083
You can set /path/to/project
to whatever you prefer, this will be the path inside your container, the .
您可以将
/path/to/project
设置为您喜欢的任何内容,这将是您容器内的路径, .
before only means current directory, which I suppose is the root directory of your project, you can change this too to any path in your machine you'd like to mount. before 仅表示当前目录,我想它是您项目的根目录,您也可以将其更改为您想要挂载的机器中的任何路径。
Also, you can set the .env
file directly in compose with env_file
option, which is what I actually recommend此外,您可以使用
env_file
选项直接在 compose 中设置.env
文件,这是我真正推荐的
https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option https://docs.docker.com/compose/environment-variables/#the-env_file-configuration-option
Create a file along with docker-compose.yml与 docker-compose.yml 一起创建一个文件
echo "John McBeth" > file_with_env_content
Mount file in container.在容器中挂载文件。
version: "3.2"
services:
backend:
build: ./backends/banuka
volumes:
- ./file_with_env_content:/opt/file_with_env_content
environment:
CHOKIDAR_USEPOLLING: "true"
ports:
- 8080:8083
And Change you python script to read content from temp file.并更改您的 python 脚本以从临时文件中读取内容。
from flask import Flask
app = Flask(__name__)
import os
PORT = 8083
#return_value = os.environ(['MY_NAME']) # value I am getting from .env
f = open("/opt/file_with_env_content", "r")
return_value = f.readline() # Write your logic to select any specific line or key value pair etc.
f.close()
@app.route('/')
def hello_world():
return str(return_value)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=PORT)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.