简体   繁体   English

节点js docker-compose 卷为空

[英]node js docker-compose volume empty

I've written an app that download some images to a folder called storage in src/storage path.我编写了一个应用程序,可以将一些图像下载到src/storage路径中名为storage的文件夹中。 everything works great in development mode (without container) and in production mode (container) it works but the volume will be empty.在开发模式(无容器)和生产模式(容器)下一切正常,但卷将为空。

docker-compose: 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: Dockerfile:

FROM node:14
WORKDIR /usr/src/app
COPY package.json yarn.lock ./

RUN npm install

COPY ./src .

CMD [ "node", "app.js" ]

and this is my download function:这是我下载的 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;

and finally the code:最后是代码:

    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);
      }
    }

as I said, it downloads files in container but volume folder is empty and there is no error.正如我所说,它在容器中下载文件,但卷文件夹为空且没有错误。

if I check docker container with docker exec i can see downloaded files.如果我用 docker exec 检查 docker 容器,我可以看到下载的文件。

Update:更新:

You need to mount the volume to the absolute path you see when you execute pwd from a terminal within the container in the images directory.您需要将卷挂载到从图像目录中容器内的终端执行pwd时看到的绝对路径。


You need to change your docker-compose file's volumes like below:您需要更改 docker-compose 文件的volumes ,如下所示:

volumes:
      - ./storage-data:/usr/src/app/src/storage

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM