This repository provides a solution to back up and recreate Docker containers, networks, and volumes running on a host system. It generates the necessary docker commands to recreate the current state of all running containers, including their configurations, custom networks, and named volumes.
-
Generate
docker runCommands:- Recreates all running containers with their configurations, including:
- Container name
- Image name
- Volumes
- Environment variables
- Devices
- User
- Restart policy
- Network settings (including containers using another container's network)
- Hostname
- DNS settings
- Shared memory size (
--shm-size) if greater than 64MB - CPU limits (
--cpus) if not using all available CPUs
- Recreates all running containers with their configurations, including:
-
Generate
docker volume createCommands:- Recreates all named and CIFS volumes with their drivers and options.
-
Generate
docker network createCommands:- Recreates all custom networks with their drivers, options, and IPAM configurations (e.g., subnet, gateway).
-
Output to a Single Script:
- All generated commands are saved in a single shell script (
docker_commands.sh) for easy execution.
- All generated commands are saved in a single shell script (
- Docker must be installed and running on the host system.
- The Docker socket (
/var/run/docker.sock) must be accessible to the container running this script.
- Clone the Repository:
git clone https://github.com/sudipmandal/docker-containers-backup.git cd docker-containers-backup/src - Build the Docker Container
docker build -t docker-containers-backup:v1.0 . - Run the Container
docker run --rm \ -v /var/run/docker.sock:/var/run/docker.sock:ro \ -v /path/to/backup/folder/on/host:/output \ docker-containers-backup:v1.0
- View the Output
The generated commands will be saved in the path specified in the above command to a file named docker_commands.sh. You can inspect the file:
Ensure to give execute permission to the above file before running it.
cat /path/to/backup/folder/on/host/docker_commands.sh
- Ensure the Docker socket is mounted as a read-only volume (/var/run/docker.sock) when running the container.
- The script handles containers using another container's network (--network container:<container_name>).
- Shared memory size (--shm-size) and CPU limits (--cpus) are included in the docker run commands if applicable.
- The script does not back up container data stored in volumes. Ensure you back up data in volumes separately. The script will simply recreate all named volumes, networks and containers as they were at the time the backup was run.
- Currently only tested on Linux based server, Windows or other OS may need changes to the script
- Contributions are welcome! Feel free to submit pull requests to improve this repository.
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.
