skip to Main Content

The following is my docker file

FROM node:16-alpine
WORKDIR /app
COPY a.txt /app

I have volume name my-volume which have a folder called dist. Im trying to mount my-volume to /app in the container. I use the below command.

docker run -v my-volume:/app -itd  -e SERVICE_ENV='test01'  8ef983161140

The volume is getting mounted properly, but the issue is that the a.txt file in the container is getting replaced by dist folder from volume. I want to merge the contents of volume and container in /app.

Any idea?

docker run -v my-volume:/app -itd  -e SERVICE_ENV='test01'  8ef983161140

expecting

/app # ls -al
total 0
drwxr-xr-x    3 root     root            18 Oct 18 15:33 .
drwxr-xr-x    1 root     root            18 Oct 18 16:22 ..
drwxr-xr-x    1 root     root            18 Oct 18 16:22 a.txt
drwxrwxr-x    3 node     node            18 Oct 18 14:11 dist

2

Answers


  1. If you can’t place a.txt in my-volume, you could place a.txt in /app_tmp in the image and have a script running on startup (e.g. with ENTRYPOINT) that copies the file from /app_tmp to /app (that is the mounted my-volume).

    Login or Signup to reply.
  2. I don’t believe you can mix contents from your container image with a volume in docker in the same folder. I believe docker will replace everything in the specified folder with the volume.

    You can attach a volume to the dist folder in your app so that your dockerfile still maintains that a.txt file and you have your dist files in a volume.

    docker run -v my-volume:/app/dist -itd  -e SERVICE_ENV='test01'  8ef983161140
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search