Search code examples
node.jswindowsdockerdocker-composedockerfile

Update periodically a file inside a docker image from a windows access application


I am new to docker.

I have a microsoft Access application that write datas on a windows box in a file named datas.json.

This file datas.json is periodically read by a node.json application that process it.

For differents reasons, i need to dockerize the node.js application.

I have wrote a Dockerfile and that' part is ok.

Next i need to make the access file wrote datas inside the docker image asactually the datas.json file is inside the docker image.

How can i do this ? Is maintain the datas.json file inside the image a good way ? Is it better (and possible) to have the datas.json outside the docker image and read it from docker image ?


Solution

  • You can mount a file or directory from your host filesystem in Docker. For example, suppose data.json contains {"msg": "hi"}. You can create a container where it will be shared with the host filesystem with:

    docker run -it --mount type=bind,source=$PWD/data.json,target=/root/data.json node:slim /bin/bash
    

    This should work the same on Windows, but the source option must be an absolute path, so replace $PWD with the path to the directory where data.json lives.

    This puts data.json under /root and opens up a bash shell in the container. Use it to try looking at data.json:

    root@129e0d705a44:/# cat /root/data.json
    {
      "msg": "hi"
    }
    

    Now modify the contents of the file in your local text editor and you'll see the contents of data.json in the container have immediately changed:

    root@129e0d705a44:/# cat /root/data.json
    {
      "msg": "bye"
    }
    

    There are other options for mounts, see the documentation on bind mounts for more info.

    Note that you cannot add a bind mount to your Dockerfile, you have to specify it when you start the container. Dockerfiles are used to create a Docker image, which is machine-independent. It would be a bit scary security-wise if you started a container and it could decide to read random parts of your filesystem.