0

I am new to docker. I build a docker image using the below code

FROM ubuntu

ADD requirements.txt .

RUN apt-get update && \
    apt-get install -y python3 && \
    apt install python3-pip -y && \
    apt-get install -y libglib2.0-0 \
                       libsm6 \
                       libxrender1 \
                       libxext6
RUN python3 -m pip install -r requirements.txt

COPY my_code /container/home/user

ENV PYTHONPATH /container/home/user/program_dir_1

RUN apt install -y libgl1

WORKDIR /container/home/user

CMD python3 program_dir_1/program_dir_2/program_dir_3/main.py

Now I have a local dir /home/host/local_dir. I want to write/copy all the file that the program creates during the runtime to this local dir.

I am using the below command to bind the volume

docker run -it --volume /home/host/local_dir:/container/home/user my_docker_image

It is giving me an error that

program_dir_1/program_dir_2/program_dir_3/main.py [Errno 2] No such file or directory

When I run the below command

docker run -it --volume /home/host/local_dir:/container/home/user my_docker_image pwd

It is giving the path to the host dir which I linked. It seems like it is also switching the working directory to the host volume to which I am linking.

Can anyone please help me to understand how to copy all the files and data generated using the working directory of the container to the directory of the host?

PS: I have used the below StackOverflow link and tried to understand but didn't get any success How to write data to host file system from Docker container # Found one solution but got the error that

docker run -it --rm --volume v_mac:/home/host/local_dir --volume v_mac:/container/home/user my_docker_image cp -r /home/host/local_dir /container/home/user

Docker: Copying files from Docker container to host # This is not much of use as I assume the container should be in a running state. In my mine it exited after completion of the program

learner
  • 828
  • 2
  • 19
  • 36
  • 1
    Since you're bind-mounting the volume over the directory you install your application into, it hides everything in the image, _including your application code_. You should use a data directory somewhere separate from the application-code directory. (Or, better still, store data somewhere like a relational database that doesn't require the filesystem at all.) – David Maze Sep 05 '22 at 18:42
  • When you are saying "data directory somewhere" you mean instead of binding to "/container/home/user" if I bound to "/container/home/data" then I am able to run my program ? – learner Sep 05 '22 at 18:49
  • 1
    I might even use a shorter container path, `-v "$PWD:/data"`. But the important thing is that the container path not be the directory containing your application code (or a parent directory of it). – David Maze Sep 05 '22 at 18:51

0 Answers0