Search code examples
mysqldatabasedockerlocalhostbackup

Backup DB from docker to real machine


SOLVED !

Good morning :)

Did anyone make a db backup from docker to host machine? I have a MySQL database but I would like to clone it into my real machine because every time I run container it creates a new image, but I want to use this db every time (so I want db to persist in real machine).

Thanks a lot !

enter image description here


Solution

  • When you have a Docker container that is already configured to use a MySQL database inside the container, you can make a database backup with the mysqldump inside the container, like this:

    mysqldump database > database.sql
    

    (NOTE! You run this inside your container)

    You can then use the docker container cp command to copy files between the container and the local filesystem. For instance:

    docker container cp <containerId>:/file/path/within/container /host/path/target
    

    (NOTE! You run this in your local filesystem)

    So using this example, the commandline would become:

    docker container cp <containerId>:/full/path/to/database.sql .
    

    That copies the file database.sql to your current directory in your local filesystem.