简体   繁体   English

如何挂载 Docker 容器卷

[英]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.

相关问题 MacOS:如何使用 docker-compose.yml 和 Dockerfile 挂载卷? - MacOS: How to mount volumes using docker-compose.yml and Dockerfile? 将本地目录挂载到Docker容器中 - Mount local directory into Docker Container python docker如何将目录从主机挂载到容器 - python docker how to mount the directory from host to container 如何挂载 docker 容器以便我可以运行存储在容器内部的 python 脚本 - How to mount a docker container so that I can run python scripts, which are stored in the inside of the container Docker:除非重新启动容器,否则绑定安装不反映 - Docker: Bind mount not reflecting unless container is restarted 无法将本地目录挂载到 docker 容器 - Cannot Mount local dir to docker container 无法将主机目录挂载到 docker 容器 - Unable to mount host directory to docker container 在 docker 容器中将主机目录挂载为只写 - mount host directory as write only in the docker container 如何从使用 KubernetesPodOperator 触发它的 Airflow 主机将卷挂载到运行 docker 容器的 Kubernetes Pod - How to mount a volume to a Kubernetes Pod running a docker container from the Airflow host that triggers it using the KubernetesPodOperator Py-Docker create_container使用远程卷 - Py-Docker create_container using remote volumes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM