[英]node js docker-compose volume empty
我编写了一个应用程序,可以将一些图像下载到src/storage
路径中名为storage
的文件夹中。 在开发模式(无容器)和生产模式(容器)下一切正常,但卷将为空。
docker-compose:
version: "3.2"
services:
app:
build: .
volumes:
- ./storage-data:/src/storage
depends_on:
- db
env_file:
- prod.env
networks:
- app_network
networks:
app_network:
Dockerfile:
FROM node:14
WORKDIR /usr/src/app
COPY package.json yarn.lock ./
RUN npm install
COPY ./src .
CMD [ "node", "app.js" ]
这是我下载的 function:
const { createWriteStream } = require("fs");
const { pipeline } = require("stream");
const { promisify } = require("util");
const fetch = require("node-fetch");
const download = async ({ url, path }) => {
const streamPipeline = promisify(pipeline);
const response = await fetch(url);
if (!response.ok) {
throw new Error(`unexpected response ${response.statusText}`);
}
await streamPipeline(response.body, createWriteStream(path));
};
module.exports = download;
最后是代码:
for (const [i, product] of products.entries()) {
const { thumbnail, name } = product;
const ext = thumbnail.slice(thumbnail.lastIndexOf("."));
const filePath = `${path.join(__dirname, "../storage")}/${
brand.text
}-${slugify(name)}-thumbnail${ext}`;
try {
await download({
url: thumbnail,
path: filePath,
});
products[i].thumbnail = filePath.slice(filePath.indexOf("src"));
logger.info(`"${name}" thumbnail saved to ${filePath}`);
} catch (error) {
logger.error(error);
}
}
正如我所说,它在容器中下载文件,但卷文件夹为空且没有错误。
如果我用 docker exec 检查 docker 容器,我可以看到下载的文件。
您需要将卷挂载到从图像目录中容器内的终端执行pwd
时看到的绝对路径。
您需要更改 docker-compose 文件的volumes
,如下所示:
volumes:
- ./storage-data:/usr/src/app/src/storage
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.