0

My intention is to dump data from Django app (running inside docker container) directly on the disk and not inside the container. Any ideas how to do it? I tried this:

docker exec -it app bash -c "python manage.py dumpdata > data.json"

but the data is saved inside container.

cristian
  • 518
  • 5
  • 20

4 Answers4

2

First, run your container with the -v option:

docker run -v $(pwd):/data-out <app-image>

This will map your current directory (pwd) to the container's /data-out dir (it will create this dir inside the container if it doesn't exist). You can change $(pwd) to /something/else if you want.

Second, execute your command:

docker exec -it app bash -c "python manage.py dumpdata > /data-out/data.json"

Notice that the output goes to the data-out dir.

Finally, view your file locally:

cat data.json
Anton
  • 1,793
  • 10
  • 20
  • Thanks @Anton, I guess there isn't a way without stopping/starting the docker container? – cristian Mar 08 '23 at 13:43
  • 1
    @cristian see if this helps - https://stackoverflow.com/questions/28302178/how-can-i-add-a-volume-to-an-existing-docker-container – Anton Mar 08 '23 at 13:50
1

Redirect output of docker command, not python.

docker exec -it app bash -c "python manage.py dumpdata" > data.json

UPD: if manage.py is in working directory you can also omit bash invocation:

docker exec -it app python manage.py dumpdata > data.json
dimich
  • 1,305
  • 1
  • 5
  • 7
0

You can use a volume parameter with the docker run command:

docker run -v /app/logs:/app/logs ...

Reference: https://docs.docker.com/engine/reference/commandline/run/#volume

  • This is correct in that volumes are a good way to keep a container's data accessible in relatively simple ways, but the question implies that a container already exists and we want to get the data it already has. – tari Mar 13 '23 at 02:03
-2

You can use a volume when running docker run

docker exec -it app bash -c "python manage.py dumpdata > /data/data.json" -v $pwd:/data
joesturge
  • 1
  • 1