Chand
Chand

Reputation: 51

Container volume not mounting to local path

Python Code:

with open("/var/lib/TestingVolume.txt", "r") as outFile:
    data = outFile.read()
with open("/var/lib/TestingVolume.txt", "w") as outFile:
    outFile.write("Hi, Hello")

Dockerfile

# For more information, please refer to https://aka.ms/vscode-docker-python
FROM python:3.8-slim
# Keeps Python from generating .pyc files in the container
ENV PYTHONDONTWRITEBYTECODE=1
# Turns off buffering for easier container logging
ENV PYTHONUNBUFFERED=1
WORKDIR /app
COPY . /app
# Creates a non-root user with an explicit UID and adds permission to access the /app folder
# For more info, please refer to https://aka.ms/vscode-docker-python-configure-containers
RUN adduser -u 5678 --disabled-password --gecos "" appuser && chown -R appuser /app /var
USER appuser
# During debugging, this entry point will be overridden. For more information, please refer to https://aka.ms/vscode-docker-python-debug
CMD ["python", "WriteData.py"]

docker-compose.yml

version: '3.4'
services:
    newfoldercopy:
        image: newfoldercopy
        build:
          context: .
          dockerfile: ./Dockerfile
        volumes:
          - D:/PythonService:/var/lib/:cached
          - ~:/host-home-folder:cached
          - ./data-subfolder:/data:cached

I am using VS code. Added all the docker files to my workspace. Trying to mount the local path to the container volume. The above code is not writing, as the container is writing data on a container virtual file system. https://code.visualstudio.com/remote/advancedcontainers/add-local-file-mount documentation says that we have to cache. still, it is not working.

Upvotes: 0

Views: 1024

Answers (1)

Hans Kilian
Hans Kilian

Reputation: 25449

You mount to /var/lib/data but write to /var/lib so the path you write to is not at or below the mounted path.

The easiest way to fix it is probably to change your code so you write to /var/lib/data like this

with open("/var/lib/data/TestingVolume.txt", "r") as outFile:
    data = outFile.read()
with open("/var/lib/data/TestingVolume.txt", "w") as outFile:
    outFile.write("Hi, Hello")

Upvotes: 1

Related Questions