Home > Blockchain >  Copy files from container to local in Docker
Copy files from container to local in Docker

Time:10-11

I want to copy a file from container to my local. The file is generated after execute python script, but due to then ENTRYPOINT, the container exited right after it run, and cant be able to use docker cp command. Any idea on how to prevent the container from exit before manage to copy the file? Below is my Dockerfile:

FROM python:3.9-alpine3.12

WORKDIR /app

COPY . /app/

RUN pip install --no-cache-dir -r requirements.txt && \
    rm -f /var/cache/apk/*

ENTRYPOINT ["python3", "main.py"]

I use this command to run the image: docker run -d -it --name test [image]

CodePudding user response:

If the output file is stored in it's own directory (say /app/output) you can run: docker run -d -it -v $PWD/output:/app/output/ --name test [image] and the file will be in the output directory of the current directory.

If it's not, then run the container with: docker run -d -it --name test [image]
Then copy the file to your own filesystem using docker cp test:/app/example.json . to copy it to the current directory.

CodePudding user response:

If running a container in background is unnecessary then you can copy a file from stdout

docker run -it [image] cat /app/example.json > out_example.json
  • Related