无法将Docker容器的工作目录挂载到主机

时间:2020-09-28 08:38:12

标签: python docker dockerfile docker-volume docker-build

我正在尝试为我的python应用程序构建一个docker容器,该容器允许在该应用程序的工作目录中对配置文件进行编辑,因此我不需要每次更改后都重新构建映像。

Dockerfile:

FROM python:3

VOLUME /app
COPY . /app
WORKDIR /app

RUN pip install requirements.txt

CMD ["python", "main.py"]

建立映像后,我将使用

部署容器
docker run -d --name app -v /path/on/host/app:/app app:latest

我在容器中收到一个错误,python: can't open file 'main.py': [Errno 2] No such file or directory/path/on/host/app保持为空。

我无法弄清楚我需要在Dockerfile中进行什么工作。

更新-找到了解决方案

由于我只需要访问配置文件,因此,为了使此功能生效,我做了一些更改

Dockerfile:

FROM python:3
COPY . /app
WORKDIR /app
RUN pip install requirements.txt
VOlUME /app/config
CMD ["python","main.py"]

现在,当我运行容器时,我将配置复制到/path/on/host/app

docker run -d --name app --mount type=bind,source=/path/on/host/app,target=/app/config

这解决了我的问题,现在我可以更新配置并重新启动容器以应用更改。

0 个答案:

没有答案