1

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]

  • Why can't you use `docker cp`? It works with stopped containers (to and from): https://stackoverflow.com/a/38356081/407651 – mzjn Oct 10 '21 at 15:31
  • @mzjn, it shows `Error: No such container:path: cont_name:/app/example.json` when i use this command: `docker run -d -it --name test [image] \ && docker cp cont_name:/app/example.json /local/path`. Cant use `&&` together? –  Oct 10 '21 at 15:44
  • Your container name is `test` so it should be `docker cp test:/app/example.json /local/path` – Mikael Kjær Oct 10 '21 at 15:49
  • Mikael, yes, I did, `cont_name` is just for me to define container name. –  Oct 10 '21 at 15:52

2 Answers2

2

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.

Mikael Kjær
  • 670
  • 3
  • 6
  • Mikael, I tried, but I cant see the output file in my local. :( –  Oct 10 '21 at 15:45
  • Which directory should the output be in? – Mikael Kjær Oct 10 '21 at 15:46
  • In container, the file store in /app directory. docker run -d -it -v $PWD:/app/example.json --name test [image] –  Oct 10 '21 at 15:50
  • In that case my solution does't work. Let me change the answer a little bit – Mikael Kjær Oct 10 '21 at 15:52
  • Hi Mikael, it works if it execute separately, but when I combine the command like this: `docker run -d -it --name test [image] \ && docker cp test:/app/example.json .` , it shows this err: `Error: No such container:path: test:/app/example.json`. The reason is because the command is run in terraform, so i need to add `&&`. –  Oct 10 '21 at 16:01
  • Seems like the file doesn't exist then. Are you sure that's the output name? – Mikael Kjær Oct 10 '21 at 16:16
  • It works well if I execute `docker run` then `docker cp`, but if add `&&` then it shows `Error: No such container:path: test:/app/example.json` –  Oct 10 '21 at 16:21
  • Did you try with `;` instead of `&&` in case the container doesn't exit nicely? – Mikael Kjær Oct 10 '21 at 16:31
  • Yes i did try `;`, but same error i got. Thank you for your solution, appreciate. –  Oct 10 '21 at 16:36
0

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
samuelcolt
  • 253
  • 1
  • 5