Home > Blockchain >  Unable to mount host directory to docker container
Unable to mount host directory to docker container

Time:01-24

I have a python app which runs everyday to download images and saves into specified folder with each day folder creation like this /home/ubuntu/images/yyyymmdd.

I have build docker container of my python app on ubuntu 20. When i try to run app by mounting host directory then log message prints folder created /home/ubuntu/images/20220123 but i can not see any folder.

I have checked the docker folder /var/lib/docker and found that random hash is created inside folder containers and overlay2. So i have tried to mount with both directory as below but no luck.

sudo docker run -t -i -v /home/ubuntu/images:/var/lib/docker/containers --network=host testapp/img-downloader:0.0.1

sudo docker run -t -i -v /home/ubuntu/images:/var/lib/docker/overlay2 --network=host testapp/img-downloader:0.0.1

I can see the date folder created inside images folder and image files got saved like this

/var/lib/docker/overlay2/d52bcf61cae2e563c3c8561bab53b4bb2dd2ea2d633a14d40c96d7992fffae28/diff/home/ubuntu/images/20220123

What i am missing here so that its not saving images to host directory like /home/ubuntu/images/20220123 instead of inside docker container.

My Dockerfile is as below -

FROM alpine:3.14

ENV PYTHONUNBUFFERED=1
RUN apk add --update --no-cache python3-dev mariadb-dev gcc musl-dev g   && ln -sf python3 /usr/bin/python

RUN python3 -m ensurepip
RUN pip3 install --no-cache --upgrade pip setuptools

COPY ./requirements.txt /requirements.txt

WORKDIR /

RUN pip3 install -r requirements.txt

COPY ./ /

ENTRYPOINT [ "python3" ]

CMD [ "./main.py" ]

Please help here. thanks

CodePudding user response:

...What i am missing here so that its not saving images to host directory like /home/ubuntu/images/20220123 instead of inside docker container.

Presumed you meant you want to save images to a directory on the host and not inside the container. There's no need to mount /var/lib/docker/.... You need to ensure your program saved files to a path that is bind mounted to the host. Examples:

mkdir images # Create a directory on the host to hold the images

docker run -it --rm -v ~/images:/images alpine ash -c "mkdir /images/yesterday; mkdir /images/today; echo 'hello' > /images/today/msg.txt; echo 'done.'"

After the container exited, issue ls -ld images/* on the host will show you the 2 directories created; cat images/today/msg.txt will print you the content you saved via the container (simulate if you download images via the container).

  •  Tags:  
  • Related