1

I made an app that download images and save them to a directory called storage. everything works great in development but in container I got error:

{"errno":-2,"code":"ENOENT","syscall":"open","path":"/usr/src/app/src/storage/samsung-Galaxy-Note20-Ultra-thumbnail.jpg","level":"error","service":"user-service","timestamp":"2021-05-20T20:27:42.545Z","message":"ENOENT: no such file or directory, open '/usr/src/app/src/storage/samsung-Galaxy-Note20-Ultra-thumbnail.jpg'","stack":"Error: ENOENT: no such file or directory, open '/usr/src/app/src/storage/samsung-Galaxy-Note20-Ultra-thumbnail.jpg'"}

and this is my download code:

    for (const [i, product] of products.entries()) {
      const { thumbnail, name } = product;
      const ext = thumbnail.slice(thumbnail.lastIndexOf("."));
      const filePath = `${path.resolve("./src/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);
      }
    }

this code use path node js module and this is download 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));
};

This is my folder structure:

enter image description here

and my docker volume in docker-comspoe.yml:

   volumes:
      - ./storage:/src/storage

and working dir in my dockerfile:

WORKDIR /usr/src/app

I don't know What causes this error.

Braiam
  • 1
  • 11
  • 47
  • 78
ShaSha
  • 589
  • 3
  • 9
  • 24

1 Answers1

1

You're mounting dir /src/storage but uploading to /usr/src/app/src/storage/.
Change from:

`${path.resolve("./src/storage")}/${brand.text}-${slugify(name)}-thumbnail${ext}`;

to:

`${path.resolve("/src/storage")}

quentino
  • 1,101
  • 1
  • 10
  • 25
  • I did this and it works in docker container well, but in MacOS it fails. I changed to ```path.join(__dirname, '../storage')``` and it works great. but volume doesn't work (and there is no error). – ShaSha May 21 '21 at 11:40