How to Import a Container in Docker: Simple Steps
To import a container in Docker, use the
docker import command followed by the tarball file containing the container filesystem. This command creates a new Docker image from the imported container data.Syntax
The basic syntax to import a container in Docker is:
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]
Here:
file|URL|-is the path to the tarball file or URL containing the container filesystem.REPOSITORY[:TAG]is an optional name and tag for the new image.OPTIONScan include flags like-cto apply Dockerfile instructions.
bash
docker import [OPTIONS] file|URL|- [REPOSITORY[:TAG]]Example
This example shows how to import a container filesystem saved as a tarball and create a new Docker image named myimportedimage:latest.
bash
docker import mycontainer.tar myimportedimage:latestOutput
sha256:3a1b2c4d5e6f7890abcdef1234567890abcdef1234567890abcdef1234567890
Common Pitfalls
Common mistakes when importing containers include:
- Using
docker importto import a container instead of an image tarball.docker importexpects a filesystem archive, not a saved image. - Confusing
docker importwithdocker load. Usedocker loadto load images saved withdocker save. - Not specifying a repository name or tag, which results in an image with a random ID and no easy reference.
bash
Wrong way: docker import myimage.tar Right way: docker import mycontainer.tar myimage:latest
Quick Reference
Remember these tips when importing containers:
- Use
docker importfor filesystem tarballs. - Use
docker loadfor Docker images saved withdocker save. - Always tag your imported image for easy use.
Key Takeaways
Use
docker import to create an image from a container filesystem tarball.Specify a repository and tag to name your imported image clearly.
Do not confuse
docker import with docker load; they serve different purposes.Imported images do not include Docker history or metadata, only the filesystem.
Always verify the tarball source and contents before importing.