Home > other >  docker volume mounted after files were deleted
docker volume mounted after files were deleted

Time:11-28

When I mount local build directory to container /app/build, then excute docker-compose up, it will be generated a /app/build directory, expecting it exists on local build directory, but nothing on there.

If not mount, container /app/build directory has expected files

enter image description here

CodePudding user response:

What do you want to achieve? During the build step of docker compose the built package should be copied to the image. Afterwards it should be enough to start the image, without any need for mounting the volume with the built artifacts to the running container, as the files are already in the image.

Example: screenshot

docker compose build only modifies the content inside the build container, which is then written as layer to the built image.

As you see, the COPY ./package . from the Dockerfile does what's expected, but only inside the container. Also the following

RUN bash -c 'source ~/.nvm/nvm.sh \
    && nvm install 17.1.0 \
    && npm install'

Does what's expected, as the node_modules folder is present inside the container. You can verify this also by looking at the file timestamps.

To geht your desired behavior you could do the following:

docker run --rm -it -v ${pwd}:/app ubuntu:20.04 bash
apt update
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
cd /app
cp -Rv package/* .
source ~/.nvm/nvm.sh \
    && nvm install 17.1.0 \
    && npm install
touch build/x
exit
dir .\build\

As you see in the screenshot the filesystem outside the container is modified using this approach:

screenshot 2

Summary:

  • use docker compose build to build a project inside a container, without modifying the outside filesystem
  • use docker run -v -v ${pwd}:/app if you want the build to modify the outside filesystem.
  • Related