Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

Docker commands Guide – docker import with examples

Here’s a complete tutorial on docker import, covering what it does, examples, and use cases.


What is docker import?

docker import is a Docker command that creates a new image from a tarball containing a container’s filesystem. It is often used to restore containers from exported tar files or to create custom images from minimal filesystems.

Key Features:

  • Converts a tarball (container snapshot) into a new Docker image.
  • Restores backup tarballs created with docker export.
  • Allows for custom image creation from external filesystems.
  • Supports adding tags and metadata during the import process.

Basic Syntax

docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]

Options:

  • -c, --change: Apply Dockerfile instructions (e.g., CMD, ENV) to the image during import.
  • -m, --message: Add a commit message to the new image.

Examples of docker import

1. Import a Tarball as a New Image

docker import my_container.tar my_new_image:latest

This creates a new image called my_new_image:latest from the tarball my_container.tar.


2. Import a Tarball with a Commit Message

docker import -m "Restored from backup" my_container.tar restored_image:backup

This imports the tarball and adds a message describing the image.


3. Apply Dockerfile Instructions During Import

docker import -c "CMD /bin/bash" my_container.tar custom_image:1.0

This sets the default command (CMD) for the new image to /bin/bash.


4. Import from a Remote URL

docker import https://example.com/container_backup.tar my_remote_image:latest

This imports an image directly from a remote tarball URL.


5. Import and Set Multiple Instructions

docker import -c "ENV APP_ENV=production" -c "CMD /start.sh" my_backup.tar production_image:1.0

This sets an environment variable (APP_ENV) and a startup command (/start.sh).


6. Import a Tarball Using Standard Input

cat my_container.tar | docker import - custom_image:latest

This reads the tarball from standard input and creates a new image.


7. Rebuild a Base Image

docker import minimal_rootfs.tar minimal_image:latest

This creates a minimal image from a tarball containing a custom Linux filesystem (minimal_rootfs.tar).


8. Restore an Exported Container

Export the container:

docker export my_container -o my_container.tar

Import the tarball as a new image:

docker import my_container.tar restored_image:latest

9. Automate Import with a Script

#!/bin/bash
docker import -c "CMD /bin/bash" /backups/my_container.tar backup_image:$(date +%Y%m%d)
echo "Image imported with tag backup_image:$(date +%Y%m%d)"

Use Cases for docker import

1. Restoring Containers from Backups

  • Use docker import to restore a container’s filesystem from a backup tarball.
  • Example: Recover a database container from a previously exported snapshot.

2. Creating Minimal Base Images

  • Build custom base images by importing minimal filesystems (e.g., custom Alpine, Ubuntu).
  • Example: Create a stripped-down Linux image from a root filesystem tarball.

3. Custom Image Creation

  • Import tarballs with pre-installed software or configurations and turn them into reusable images.
  • Example: Import a tarball containing an application and its dependencies.

4. Transferring Containers Between Systems

  • Use docker export and docker import to move containers between environments.
  • Example: Transfer a container from a development machine to a production server.

5. Integrating with Legacy Systems

  • Import filesystems from non-Docker environments to create Docker images.
  • Example: Convert a physical server’s filesystem into a Docker image.

6. Simplifying CI/CD Pipelines

  • Pre-build images with specific dependencies and import them in CI/CD pipelines.

List of Common docker import Commands

CommandDescription
docker import my_container.tar my_image:latestImport a tarball as a new image
docker import -m "Backup on Feb 7" backup.tar my_backup:20250207Import with a commit message
docker import -c "CMD /bin/bash" container.tar custom_image:1.0Set the default command during import
docker import https://example.com/backup.tar remote_image:latestImport a tarball from a URL
`cat my_container.tardocker import – my_image`
docker import minimal_rootfs.tar minimal_imageCreate a minimal image from a custom filesystem
`docker export my_containerdocker import – restored_image`

Best Practices for Using docker import:

  1. Use docker import for restoring containers or creating custom base images—not for everyday image builds (prefer Dockerfiles for reproducibility).
  2. Combine with docker export for seamless backup and restore processes.
  3. Add Dockerfile instructions (--change) to set environment variables, commands, or default entrypoints.
  4. Use meaningful tags to track imported images (e.g., backup:20250207).
  5. Verify the imported image by running it and inspecting its configuration (docker inspect).

Common Errors and Solutions

  1. “No such file or directory”
    → Ensure the tarball exists and the path is correct.
  2. “Permission denied”
    → Check permissions on the tarball or run the command with sudo.
  3. “Invalid tar file”
    → Verify that the tarball is properly created and contains the correct filesystem structure.
  4. “Imported image does not start correctly”
    → Use -c to set the CMD or ENTRYPOINT during import.

Combining docker import with Other Commands

Export and Re-Import for Backup and Restore

Export the container:

docker export my_container -o my_container.tar

Re-import it as a new image:

docker import my_container.tar my_restored_image:latest
docker run -it my_restored_image bash

Build a Custom Base Image

docker import custom_rootfs.tar my_base_image:1.0

Check the Imported Image

docker inspect my_base_image:1.0
docker run -it my_base_image:1.0 bash

Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.

0
Would love your thoughts, please comment.x
()
x