问题
I'm trying to write a Dockerfile for use as a standardized development environment.
Dokerfile:
FROM node:9.5.0
WORKDIR /home/project
# install some stuff...
# clone the repo
RUN git clone https://github.com/... .
# expose
VOLUME /home/project
# fetch latest changes from git, install them and fire up a shell.
CMD git pull && npm install && /bin/bash
After building the image (say with tag dev),
Running docker run -it dev gives me a shell with the project installed and up-to-date.
Of course changes I make in the container are ephemeral so I want to mount /home/project on the host OS, I go to an empty directory project on the host and run:
docker run -it -v ${pwd}:/home/project dev
But my project folder gets overwritten and is empty inside the container.
How can mount the volume such that the container writes to the host folder and not the opposite?
OS: Windows 10 Pro.
回答1:
When you mount a volume, read/write both are bi-directional by default.
That means, any thing you write in container will show up in host directory and vice versa.
But something wired is happening in your case, right?
I will try to tell you whats happening here.
In your build process, you are cloning git repository. During build process, volume do not mount. Git data directly are attached in your docker.
Now, when you are running docker container, you are mounting volume. Mount path in your container will be synced with your source path. That means container directory will be over-written with host directory.
Thats why your git data has been lost.
Possible Solution:
Run a script as CMD. That script will clone git repo and will do other things.
file run.sh
#!/bin/bash
# clone the repo
RUN git clone https://github.com/... .
In Dockerfile
RUN ADD run.sh /bin
# run run.sh, install them and fire up a shell.
CMD run.sh && npm install && /bin/bash
来源:https://stackoverflow.com/questions/48589595/docker-mounting-a-volume-overwrites-container