user8750453
user8750453

Reputation:

Copy files from container to local in Docker

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]

Upvotes: 1

Views: 3055

Answers (2)

samuelcolt
samuelcolt

Reputation: 263

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

Upvotes: 0

Mikael Kjær
Mikael Kjær

Reputation: 700

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.

Upvotes: 2

Related Questions