我想在我的 docker 容器中使用本地檔案夾,例如資料集。我還有一個 python 請求模塊,客戶端在其中向 API 服務器發送一個目錄路徑。我想在 docker compose 中使用此路徑作為卷在 docker 容器中使用它。
如何將此路徑發送到 docker compose。API請求是:
def train():
url = 'http://127.0.0.1:8000/train' # server url
data = {'label': ['fire'],
'image_path': '/path/to/directory',
'label_path': '/path/to/directory',
'image_size': 640,
'validation_split': 0.2,
'save_dir': 'results/'}
r = requests.post(url, json=data)
print(r.text)
如何發送本地路徑'image_path'
和'label_path'
docker compose 并將其用作卷。我的碼頭工人撰寫:
version: "3.8"
services:
inference:
container_name: "inference"
build: "./backbone"
ports:
- '5000:5000'
volumes:
- ./backbone:/code
# - ./volumes/weights:/weights
command: "python3 app.py"
environment:
- PORT=5000
- MODEL_PATH=/code/weights/best.pt
ipc: host
shm_size: 1024M
train:
container_name: "train"
build: "./train"
shm_size: '2gb'
command: "python3 main_train.py"
environment:
- RESPONSE_URL=http://127.0.0.1:8000/response
- LOGGER_URL=http://127.0.0.1:8000/logger
- PORT=8000
- IS_LOGGER_ON=False
ports:
- '8000:8000'
volumes:
- ./train:/code
ipc: host
uj5u.com熱心網友回復:
您需要進行一些更改才能成功完成這項作業。
在服務器代碼中,您不需要接受完整路徑。這樣做有許多安全問題(您可以檢索應用程式代碼嗎?資料庫憑據檔案?系統檔案,例如/etc/passwd
?)。相反,將其設定為具有可配置的資料路徑,并且只接受該路徑中的檔案。
DATA_PATH = os.environ.get('DATA_PATH', 'data') # default relative to current directory
def handle(data):
if '/' in data['image_file']:
raise InvalidInputError()
image_path = os.path.join(DATA_PATH, data['image_file'])
...
在 Docker 映像中進行設定時,可以為該資料路徑指定一個固定路徑。在容器檔案系統根目錄的子目錄中使用簡單路徑就可以了。
# train/Dockerfile
FROM python
...
ENV DATA_DIR=/data # set this variable only because the application wants it
RUN mkdir -p "$DATA_DIR" # create an empty directory by default
CMD ["./main_train.py"]
現在,當您在 Compose 中啟動它時,您知道將保存資料目錄的(固定)容器端路徑,因此您可以在那里掛載內容。
services:
train:
build: ./train
ports:
- '8000:8000'
volumes:
- ./dataset_3:/data
通常情況下,影像中的檔案系統布局、注入資料或配置的路徑以及容器內的服務使用的埠等細節都是影像的固定屬性。因此,指定“資料將始終在/data
”并將其用作掛載點是安全的;您無需在 Docker 設定中的任何位置將其指定為變數。同樣,您不需要$PORT
在 Compose 設定中設定環境變數,因為容器端埠號通常是影像的固定屬性。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/496553.html
標籤:Python 码头工人 烧瓶 码头工人撰写 码头工人卷
上一篇:正確使用SQLAlchemy會話