Technology Security Analyst
Introduction to Docker Import:
Docker import is a Docker order to make a Docker picture by bringing in the substance from a document or tarball which is made by sending out a holder. We can indicate URL or '-'
to import information or content from the document. The URL can highlight where chronicle record is available and '-'
(run) is utilized to import information straightforwardly from the STDIN for example standard information. We can apply Dockerfile guidelines while making the picture utilizing Docker import anyway there are restricted Dockerfile directions that are upheld here.
Syntax:
docker import [options] file|URL|- [REPOSITORY[:TAG]]
Options:
Here is the snapshot of the ‘docker import –help’
output that tells what we just discussed above.
How to Import Docker Image:
To import a Docker picture, first, we should have a traded document record of a holder. So when we trade any compartment, it sends out holders as a customary Linux document framework in a chronicle record; then, at that point, we need to import this file document as a Docker picture and when we run any compartment utilizing this new imported Docker picture then it works similarly as it was working in the old holder. This saves our opportunity to construct the Docker picture without any preparation on the off chance that there are any progressions are required. Docker import just backings these Dockerfile directions: CMD, ENTRYPOINT, ENV, EXPOSE, ONBUILD, USER, VOLUME, WORKDIR.
1. First, create a Docker image using the below Dockerfile:
docker build -t my-image:v2.
2. Run this image as a container using the below command: –
docker run my-image:v2
docker ps –a
3. Export the container in a tar file named helloworld.tar using the below command: –
Syntax:
docker export <container_ID> > <file_name>
docker export <container_name> > <file_name>
Example:
docker export 3673f8996e1a > helloworld.tar
Explanation:
In the above snapshot, we have created a Docker image named ‘my-image:v2’
and started a container using it, and the container just displayed output “Hello World!!!”
that is mentioned in the Docker image and it gets stopped. Then, we have exported this container as a tar file named “helloworld.tar”
.
4. Now create a folder ‘HelloWorld’
and extract the content of the tar file to this folder as below: –
mkdir HelloWorld
tar -xf helloworld.tar -C HelloWorld
5. And if we check the folder structure of HelloWorld, it looks like a regular Linux filesystem.
tree –L 1 HelloWorld
6. Let’s import this tar file as a Docker image using the below command: –
Syntax:
docker import <archive_name> <Image_name>
cat <archive_name> | docker import - <image_name>
Example:
docker import helloworld.tar my-imported-img:v1
cat helloworld.tar | docker import – my-imported-img:v2
docker import history my-imported-IMG:v1
Explanation:
In the above snapshot, we will see there is no difference in the images so we can use any of the above commands to import the Docker image from an archive.
7. We can also pass comments while importing the Docker image using the ‘-m’
option as below: –
Syntax:
cat <archive_name> | docker import -m <comment or message> - <image_name>
Example:
cat helloworld.tar | docker import -m “New Image imported” – my-imported-image:v3
8. Run a container using this new Docker image to verify if it is working as the earlier container:
docker run --it my-imported-img:v1 sh
Explanation:
In the above example, we can see that we need to specify the command while running a container using the imported Docker image as there is no command to run so we get an error if we run the command.
docker run my-imported-IMG:v1
9. We can apply Dockerfile instruction while importing the Docker image using the ‘-ca'
option as below: –
Syntax:
docker -c <Dockerfile Instruction> <archive_file> <image_name>
Example:
docker -c ‘CMD echo “HelloWOrld!!!”’ helloworld.tar my-imported-img:v4
10. Presently, suppose we need to add a record to the last picture. We can do this in two ways, the first is to alter the Dockerfile and modify the picture or run a compartment utilizing this picture and make the document and product the holder and afterward import it as another Docker picture. The simple way is to run a holder and roll out the expected improvements and product it and import it as a Docker picture.
docker run -it my-image:v1 sh
Explanation:
In the above example, created a new container using the ‘my-image:v1’
and connected to it; created a testfile.txt and populated some data in it. Exported that container and imported as a Docker image named ‘my-imported-IMG:v5’
and ran a new container using this newly imported image and when we can see that the ‘testfile.txt’
exists in this container as well.
Advantages:
docker image history my-image:v1
docker image history my-imported-IMG:v5
Explanation:
In the above snapshot, we can see that the ‘my-image:v1’
image has 3 layers however the imported image ‘my-imported-IMG:v5’
has only one layer.
Conclusion:
Docker Import is an incredible instrument that assists us with saving time by keeping away from the re-construct interaction of Docker picture for minor changes and further the Docker picture with others without Docker library, notwithstanding, we have 'burden'
and 'save'
order to reinforcement or offer the Docker picture. It just works with the compartment. We generally utilize this to work on the exhibition of the compartment as it makes a solitary layer Docker picture.