How to mount json data file in docker and use it as data source?

88 Views Asked by At

A very simple thing which is not working.

I have a json file ('data.json') which I have placed in my c# project in this path '\var\data\data.json' relative to my program.cs file.

When I debug my project it's working fine (see my program.cs code snippet below).

I want the 'data.json' file to work as a my data source and as a volume in docker, so my c# program can load/mount the json file.

My docker.compose.yml:

version: '2'
networks:
  default:
    ipam:
      driver: default
services:
  myapp:
    image: myapp-image:latest
    ports:
      - '7113:8080'
    networks:
      - default
    volumes:
      - type: bind
        source: myapp/var/data/ #(my host path)
        target: /var/data/ #(my docker path inside container)
volumes:
  myapp:
    driver: local

But it doesn't mount my data.json, when I compose-up my container.

I can do a manual copy from my host into my container: docker cp myapp/var/data/data.json myapp-app-1:/var/data/

I can even see it being copy into my container folder: enter image description here

But my program is not loading it from the mount (even when I restart my container) - rather it is perhaps loading it from the compiled program?

Here is my code from my c# 'program.cs' file

var filePath = @"var/data/data.json";
var json = JsonDocument.Parse(File.ReadAllText(filePath));
var deserialized = json.Deserialize<List<Member>>();

Can anyone please help me with how I can mount my 'data.json' file and use it in my program.cs. I am using docker-desktop on windows.

1

There are 1 best solutions below

0
codingjoe On

I was having my data.json file placed in two different places, and I was using the wrong path.

My application was using the following path: '/app/var/data/data.json'

...and I was updating and copying my file in '/var/data/data.json'.

Now I need to figure out how I can restart my container when the volume (data.json) has changed.

I would love any suggestion for how a change in the data.json file can restart or refresh my container.