Backing Up and Restoring Docker Containers and Images

Hakan Bayraktar
4 min readSep 9, 2024

--

Docker is a platform that enables you to run applications and their dependencies in containers. However, when it comes to backing up, transferring, or sharing containers and images, specific commands are used. In this article, I’ll explain the process of backing up and restoring Docker images and containers, supported by examples and command outputs.

1. Creating an Nginx Container

First, let’s create a simple Nginx container

docker run -d -p 80:80 nginx
  • docker run: Creates and runs a container from the specified image.
  • -d: Runs the container in detached mode (in the background).
  • -p 80:80: Maps port 80 on the host to port 80 in the container.
  • nginx: The name of the image to be used.

After running this command, you’ll have a container running an Nginx web server.

2. Saving the Container as an Image (Commit)

To create an image from a running container, you can use the docker commit command:

docker commit <container_id> nginx-commit

For example:

docker commit 199 nginx-commit
  • docker commit: Captures the current state of a running container and saves it as an image.
  • <container_id>: The ID of the container you want to commit.
  • nginx-commit: The name of the new image.

You can check the new image with:

docker images

Then, you can run this new image:

docker run nginx-commit

3. Exporting the Container as a File

To export the file content of a container, use the docker export command:

docker export <container_id> > nginx-export.ta

For example:

docker export 19 > nginx-exporter.tar
  • docker export: Exports the file system of a container to an archive file.
  • nginx-export.tar: The archive file where the exported content will be saved.

To view the output:

ls -l

4. Saving the Image as a File

To export a Docker image, use the docker save command:

docker save -o nginx-save.tar nginx:latest
  • docker save: Saves one or more Docker images to a tar archive.
  • nginx-save.tar: The tar file where the saved image will be stored.

You can check the file after saving:

ls -l

5. Cleanup (Containers and System)

To clean up unused containers and Docker objects, you can use the following commands:

docker rm -f $(docker ps -qa)
  • docker rm -f: Stops and removes all running containers.

And for a more extensive cleanup:

docker system prune -a
  • docker system prune -a: Removes all stopped containers, unused images, networks, and temporary files.

6. Loading an Image from a File

To restore an image from a previously saved archive file, use the docker load command:

docker load < nginx-save.tar
  • docker load: Loads a Docker image from a tar archive.

To check the loaded image:

docker images

Then, you can run the loaded image:

docker run nginx

This guide should help you back up and restore Docker containers and images efficiently. Each command is explained with examples to make the process clear. Now you’re ready to manage your Docker backups and restores with confidence!

--

--

No responses yet