如何在 docker 容器和本地文件系统之间交换文件?
How to exchange files between docker container and local filesystem?
我有一个 TypeScript 代码,它读取一个目录的内容,并且必须每隔一段时间一个一个地删除它们。
在本地一切正常。我为我的代码制作了一个 docker 容器并希望实现相同的目的,但是,我意识到目录内容与构建容器时存在的目录内容相同。
据我了解,docker容器与本地文件系统之间的连接缺失。
我一直在徘徊 bind
和 volume
选项,我遇到了以下简单教程:
How To Share Data Between the Docker Container and the Host
按照之前的教程,理论上我是可以达到目的的:
If you make any changes to the ~/nginxlogs folder, you’ll be able to see them from inside the Docker container in real-time as well.
然而,我按照完全相同的步骤进行操作,但仍然看不到本地所做的更改反映在 docker 容器中,反之亦然。
我的问题是:如何从 docker 容器访问我的本地文件系统到 read/write/delete 文件?
更新
这是我的 dockerfile
FROM ampervue/ffmpeg
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get update -qq && apt-get install -y --force-yes \
nodejs; \
apt-get clean
RUN npm install -g fluent-ffmpeg
RUN rm -rf /usr/local/src
RUN apt-get autoremove -y; apt-get clean -y
WORKDIR /work
COPY package.json .
COPY . .
CMD ["node", "sizeCalculator.js"]
在 docker 运行 命令上挂载卷的简单方法
docker run -it -v /<Source Dir>/:/<Destination Dir> <container_name> bash
另一种方法是使用 docker-compose
。
让我们尝试使用 docker-compose
将您的 docker 文件和 docker-compose 放在同一位置或目录
主要焦点
volumes:
- E:\dirToMap:/work
docker-compose.yaml
version: "3"
services:
ampervue:
build:
context: ./
image: <Image Name>
container_name: ampervueservice
volumes:
- E:\dirToMap:/vol1
ports:
- 8080:8080
并在docker文件中添加音量
FROM ampervue/ffmpeg
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get update -qq && apt-get install -y --force-yes \
nodejs; \
apt-get clean
RUN npm install -g fluent-ffmpeg
RUN rm -rf /usr/local/src
RUN apt-get autoremove -y; apt-get clean -y
WORKDIR /work
VOLUME /vol1
COPY package.json .
COPY . .
CMD ["node", "sizeCalculator.js"]
和运行按照命令启动容器
docker-compose -f "docker-compose-sr.yml" up -d --build
下面的例子直接来自docs:
The --mount
and -v
examples below produce the same result. You can't run them both unless you remove the devtest
container after running the first one.
和-v
:
docker run -d -it --name devtest -v "$(pwd)"/target:/app nginx:latest
和--mount
:
docker run -d -it --name devtest --mount type=bind,source="$(pwd)"/target,target=/app nginx:latest
这是您必须输入 2 个不同路径的地方:
-v /path/from/your/host:/path/inside/the/container
<-------host------->:<--------container------->
--mount type=bind,source=/path/from/your/host,target=/path/inside/the/container
<-------host-------> <--------container------->
我有一个 TypeScript 代码,它读取一个目录的内容,并且必须每隔一段时间一个一个地删除它们。
在本地一切正常。我为我的代码制作了一个 docker 容器并希望实现相同的目的,但是,我意识到目录内容与构建容器时存在的目录内容相同。
据我了解,docker容器与本地文件系统之间的连接缺失。
我一直在徘徊 bind
和 volume
选项,我遇到了以下简单教程:
How To Share Data Between the Docker Container and the Host
按照之前的教程,理论上我是可以达到目的的:
If you make any changes to the ~/nginxlogs folder, you’ll be able to see them from inside the Docker container in real-time as well.
然而,我按照完全相同的步骤进行操作,但仍然看不到本地所做的更改反映在 docker 容器中,反之亦然。
我的问题是:如何从 docker 容器访问我的本地文件系统到 read/write/delete 文件?
更新
这是我的 dockerfile
FROM ampervue/ffmpeg
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get update -qq && apt-get install -y --force-yes \
nodejs; \
apt-get clean
RUN npm install -g fluent-ffmpeg
RUN rm -rf /usr/local/src
RUN apt-get autoremove -y; apt-get clean -y
WORKDIR /work
COPY package.json .
COPY . .
CMD ["node", "sizeCalculator.js"]
在 docker 运行 命令上挂载卷的简单方法
docker run -it -v /<Source Dir>/:/<Destination Dir> <container_name> bash
另一种方法是使用 docker-compose
。
让我们尝试使用 docker-compose
将您的 docker 文件和 docker-compose 放在同一位置或目录
主要焦点
volumes:
- E:\dirToMap:/work
docker-compose.yaml
version: "3"
services:
ampervue:
build:
context: ./
image: <Image Name>
container_name: ampervueservice
volumes:
- E:\dirToMap:/vol1
ports:
- 8080:8080
并在docker文件中添加音量
FROM ampervue/ffmpeg
RUN curl -sL https://deb.nodesource.com/setup_10.x | sudo -E bash -
RUN apt-get update -qq && apt-get install -y --force-yes \
nodejs; \
apt-get clean
RUN npm install -g fluent-ffmpeg
RUN rm -rf /usr/local/src
RUN apt-get autoremove -y; apt-get clean -y
WORKDIR /work
VOLUME /vol1
COPY package.json .
COPY . .
CMD ["node", "sizeCalculator.js"]
和运行按照命令启动容器
docker-compose -f "docker-compose-sr.yml" up -d --build
下面的例子直接来自docs:
The
--mount
and-v
examples below produce the same result. You can't run them both unless you remove thedevtest
container after running the first one.
和
-v
:docker run -d -it --name devtest -v "$(pwd)"/target:/app nginx:latest
和
--mount
:docker run -d -it --name devtest --mount type=bind,source="$(pwd)"/target,target=/app nginx:latest
这是您必须输入 2 个不同路径的地方:
-v /path/from/your/host:/path/inside/the/container
<-------host------->:<--------container------->
--mount type=bind,source=/path/from/your/host,target=/path/inside/the/container
<-------host-------> <--------container------->