-1

I have node/express application running in docker using below docker-compose file,

docker-compose

version: '3.8'
services:
  serverapp:
    build:
      context: ../
      dockerfile: Dockerfile
    image: backend:5.1.5
    container_name: server_container
    ports:
      - "5000:5000"

Dockerfile

FROM node:14-alpine as backend-build
WORKDIR /app
COPY package*.json ./
RUN npm install --no-package-lock
COPY . ./
RUN npm prune --production
EXPOSE 5000
    
# stage-2

FROM node:14-alpine
COPY --from=backend-build /app /app
WORKDIR /app

CMD ["npm", "run", "start:docker"]

after running docker-compose file, I create an image and a container successfully.

I'm able to access all APIs using POSTMAN. Now I have to read data from D:/files (windows machine local directory) in one of the APIs.

When I hit that particular api through POSTMAN, it throws one exception which I'm capturing in node app through exception handling mechanism and I get response as below,

{
    "message": "Exception occured : Error: ENOENT: no such file or directory, scandir 'D:/files'"
}

Basically, inside such directory, I have to read different files so I need to access different files as well.

How can I access windows directory and different files within it in docker container ?


Also,

This directory is configurable by my users.

eg.

config.js

export const CONFIG={
   path: "D:/files"               
}

My users can change it to, let's say, D:/data/files or something else like C:/data/abc/files or anything.

SO, How can I make it dynamic when I create container ?


FYI, in local, everything works perfectly fine. It is just docker container in which it doesn't work.

micronyks
  • 54,797
  • 15
  • 112
  • 146

1 Answers1

0

Those who look for solution can do following,

  1. Set docker volume in your docker-compose file as shown below,

    version: '3.8'
    services:
      serverapp:
        build:
          context: ../
          dockerfile: Dockerfile
        image: backend:5.1.5
    
        volumes:
          - D:/files:/app/files                       <#### Mapping of your local directory to container directory
    
        container_name: server_container
        ports:
          - "5000:5000"
    

The above code will map your local directory to container directory but as you can see D:/files path is static path and not dynamic path.

  1. If you want to make path or local directory dynamic, you can do following

Create .env file in the same directory in which your docker-compose file is and define one new variable as shown below,

.env

MY_LOCAL_DIRECTORY_PATH=D:/files               <#### you can set any path you want...

docker.compose

volumes:
      - ${MY_LOCAL_DIRECTORY_PATH}:/app/files  <#### Mapping of your local directory to container directory
micronyks
  • 54,797
  • 15
  • 112
  • 146