Search code examples
dockerdocker-composedockerfiledocker-volume

Docker create volume with file host to override container file


I am new to docker and learning from the online resources.Basically I have an angularjs project that contains a conf file in folder (src/assets/conf/appConfig.json) and basically what I am trying to achieve is when I run my docker (version - 19.03.8) command the fileappConfig.json should be copied from /var/lib/docker/volumes/volumetest/_data/appConfig.json to src/assets/conf/appConfig.json.

I have created a volume(volumetest) as shown below:

enter image description here

I have two questions:

What command do I need in order to create file appConfig.json in the path /var/lib/docker/volumes/volumetest/_data and add the following line in the file:

{ baseUrl:http://localhost:8080/ }

Once the above is created I will add the following line in my Dockerfile for angularjs project:

COPY src/assets/conf/appConfig.json /var/lib/docker/volumes/volumetest/_data

Will the file src/assets/conf/appConfig.json be overridden when i do docker run command (docker run -p 8000:8080 -d --name cm testApp:1.0)?

Thanks in advance for any help


Solution

  • You should completely ignore the /var/lib/docker tree. Instead, you can use a Docker bind mount to replace the file at startup time. Create the file on your host; let's say you've created an alternate appConfig.json in the current directory. When you run your container, mount that file in:

    docker run \
      -v $PWD/appConfig.json:/app/src/assets/conf/appConfig.json \
      ... \
      testApp:1.0
    

    The contents of /var/lib/docker are installation-specific and aren't intended to be directly edited. Also, since containers and images have isolated filesystem spaces, you can't write directly into a host directory in a Dockerfile. If you used a named volume, you'd only be able to edit the file from inside a container, and you'd have to replace the entire conf directory with the volume contents.