skip to Main Content

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.

4

Answers


  1. 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
    
    Login or Signup to reply.
  2. 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

    Login or Signup to reply.
  3. 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
    
    Login or Signup to reply.
  4. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search