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]]
Code language: JavaScript (javascript)
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
Code language: CSS (css)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: JavaScript (javascript)
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
Code language: CSS (css)
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
Code language: JavaScript (javascript)
Import the tarball as a new image:
docker import my_container.tar restored_image:latest
Code language: CSS (css)
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)"
Code language: JavaScript (javascript)
Use Cases for docker import
1. Restoring Containers from Backups
- Use
docker importto 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 exportanddocker importto 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
| Command | Description |
|---|---|
docker import my_container.tar my_image:latest | Import a tarball as a new image |
docker import -m "Backup on Feb 7" backup.tar my_backup:20260207 | Import with a commit message |
docker import -c "CMD /bin/bash" container.tar custom_image:1.0 | Set the default command during import |
docker import https://example.com/backup.tar remote_image:latest | Import a tarball from a URL |
| `cat my_container.tar | docker import – my_image` |
docker import minimal_rootfs.tar minimal_image | Create a minimal image from a custom filesystem |
| `docker export my_container | docker import – restored_image` |
Best Practices for Using docker import:
- Use
docker importfor restoring containers or creating custom base imagesโnot for everyday image builds (prefer Dockerfiles for reproducibility). - Combine with
docker exportfor seamless backup and restore processes. - Add Dockerfile instructions (
--change) to set environment variables, commands, or default entrypoints. - Use meaningful tags to track imported images (e.g.,
backup:20260207). - Verify the imported image by running it and inspecting its configuration (
docker inspect).
Common Errors and Solutions
- “No such file or directory”
โ Ensure the tarball exists and the path is correct. - “Permission denied”
โ Check permissions on the tarball or run the command withsudo. - “Invalid tar file”
โ Verify that the tarball is properly created and contains the correct filesystem structure. - “Imported image does not start correctly”
โ Use-cto set theCMDorENTRYPOINTduring 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
Code language: JavaScript (javascript)
Re-import it as a new image:
docker import my_container.tar my_restored_image:latest
docker run -it my_restored_image bash
Code language: CSS (css)
Build a Custom Base Image
docker import custom_rootfs.tar my_base_image:1.0
Code language: CSS (css)
Check the Imported Image
docker inspect my_base_image:1.0
docker run -it my_base_image:1.0 bash
Code language: CSS (css)
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals