Home > Blockchain >  Exporting files from a Docker container inside a dockerfile
Exporting files from a Docker container inside a dockerfile

Time:08-03

I have a python script which is creating some files that I need (i.e when I do python3 myscript.py -O repository_path, it creates a repository with the files I need at /container_repo). The python script works correctly outside a docker container. I have to put it into a dockerfile. I would like the dockerfile to automatically export the created files on my computer without having to execute external commands (like docker cp for example) at some directory, let's say /home/axel.garreau/my_repo. I tried to run the dockerfile with volumes :

docker run -v /home/axel.garreau/my_repo:/container_repo my_image

But it doesn't seem to work, I get an Error :

OSError: [Errno 16] Device or resource busy: '/container_repo'

Could anybody help me with this matter ?

For information, my dockerfile looks like this :

FROM python:3.10.4
RUN apt-get update
RUN apt-get update && apt-get upgrade -y
COPY ./requirements.txt /app/requirements.txt
WORKDIR /app
RUN python3 -m pip install --upgrade pip
RUN pip install -r requirements.txt
COPY . /app
EXPOSE 5000
CMD python3 myscript.py -O /container_repo' 

CodePudding user response:

I ran a quick, successful test with a minimal Dockerfile just to make sure the basic stuff is OK. If you can do a similar test in your environment, that would be great.

I tested this by creating my_repo inside my personal temp-directory. If test fails in your environment, you may have to change access to your host directory.

FROM python:3.10.4
RUN apt-get update
CMD echo "Hello" > /container_repo/hello.txt

Build & run

docker build -t me/docker-test .
docker run -v /home/axel.garreau/my_repo:/container_repo me/docker-test

If run went well, there should be a hello.txt in directory /home/axel.garreau/my_repo

When changing either host- or container directory to a non-existing directory:

/bin/sh: 1: cannot create /container_repo/hello.txt: Directory nonexistent

Test on Ubuntu 20.04, Docker version 20.10.17, build 100c701

CodePudding user response:

Thank you all for your answers, they were helpfull in solving my problem. It came from a fonction in the python script : I use shutil.rmtree(), and it seem that it was the source of the problem. I removed it, and now the dockerfile is working correctly. I don't know why it didn't work, but I'll look into it.

  • Related