EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Docker Export

By Sarabjeet SinghSarabjeet Singh

Home » Software Development » Software Development Tutorials » Docker Tutorial » Docker Export

Docker Export

Introduction to Docker Export

Docker export is a command that is used to export the container’s file system as an archive i.e. tar that can be imported when required as a Docker image using the Docker import command. It includes all the files and folders created in that container, however, it does not export the data of volumes that are mounted on that container. It is very useful to create a single layer or flat Docker image to improve performance or to share the Docker image with others without the need for the Docker registry. It is like creating a template as we create a template of VMs to speed up the process.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

docker export [OPTIONS] container

Options:

-o, –output – It is a string type and used to write the output to a file, instead of STDOUT.

Command:

docker export --help

Docker Export Example 1

Explanation: We use ‘–help’ to know all about the ‘docker export’ as shown above. We can see that this command has only one option ‘-o’ for redirecting the output to a file.

How Does Export Work in Docker?

It takes at least one argument that is a container name or container ID. When we run this command it actually saves the container’s file system as an archive.  It creates a flat Docker image that makes the Docker image slightly smaller in size, however, it lost its history and metadata which means we cannot perform any rollback to a previous layer if we import a Docker image using any exported tar file.

Below is a snapshot of a folder structure of an exported container:

Command:

tree -L 1 alpine-export

Output:

Docker Export Example 2

Explanation: We export a Docker container in a tar file and extracted the tar file in a folder ‘alpine-export’. The above example is the tree structure of that folder.

Popular Course in this category
Docker Training (4 Courses, 3 Projects)4 Online Courses | 3 Hands-on Projects | 11+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (4,831 ratings)
Course Price

View Course

Related Courses
Python Training Program (36 Courses, 13+ Projects)All in One Software Development Bundle (600+ Courses, 50+ projects)

Example to Implement Docker Export

Below are the examples:

Scenario 1: Update any File of the Docker Image

We have built an nginx Docker Image and a container is running using this Docker image however when we browse the web application, we realized that something missing on the homepage. We need to update the homepage and create a new Docker image but building the image from scratch using Dockerfile is going to take more time so we are going to make the changes in the index.html file when the container is running and we export the container and then import it as a Docker image. So let’s get started:

1. Below is the snippet of Dockerfile and index.html files:

index.html

Code:

<!DOCTYPE html>
<html>
<head>
<title>Docker Export</title>
</head>
<body>
<h1>Example of Docker export</h1>
</body>
</html>

Dockerfile

FROM ubuntu
RUN apt-get update && apt-get install -y nginx
add index.html /var/www/html
CMD ["nginx", "-g", "daemon off;"]

2. Build the docker image named ‘my-nginx’ using below command:

docker build -t my-nginx

Note: if Dockerfile is not in the current working directory then use the full path of the Dockerfile.

Output:

Docker Export Example 3
3. Run a container using the above Docker image as below:

docker run -d -p 80:80 my-nginx

Output:

Docker Export Example 4 Docker Export Example 5

4. Now, let’s assume that we got a requirement to change the background color of the heading ‘Example of Docker Export’ to blue, so we will update the index.html file in the running container and export it as an archive using below command:

Command:

sudo docker exec -it 44 sh

Replace the line “<h1>Example of Docker export</h1>” with below line in the index.html file as shown in the snapshot:

<h1 style="background-color:DodgerBlue;">Example of Docker export</h1>

Output:

exec -it 44 sh Example 6

5. Export the running container after making the changes using below command and import it as a Docker image named ‘my-nginx:v2’;

Command:

docker export 44 > my-nginx.tar
docker import -c ‘CMD [“nginx”, “-g”, “daemon off;”]’ my-nginx.tar my-nginx:v2
docker run -d -p 8081:80 my-nginx:v2

Output:

Docker Export Example 7

Explanation: Run a new container using the imported Docker file to verify the changes are successfully applied as expected and when we browse the default page, we can see that the background color of the heading has been changed to blue in the below image.Docker Export Example 8

Scenario 2: Create a Single Layer Docker Image

When we create a Docker image it has multiple layers as each instruction in a Dockerfile creates a layer while building the image. If we want to create a single layer Docker image to enhance the performance of the container, we export the container and import it as a Docker image as shown in the below steps:

1. Check the layers of any Docker image using below Command:

Syntax:

docker image history <image_name>

Command:

docker image history my-nginx

Output:

Image History Example 1

2. Now, again run a container using this image and export it as an archive:

Command:

docker run -d -p 80:80 --name nginx-container my-nginx
docker export nginx-container > my-nginx.tar

Output:

nginx Example 2

3. Import the exported archive file or tar file with the command and message as below and check the history of newly imported Docker image:

Command:

docker import -c 'CMD ["nginx", "-g", "daemon off;"]' -m "single layer Docker image" my-nginx.tar my-nginx:v3
docker image history my-nginx:v3

Output:

Tar File Example 3

Explanation: In the above snapshot, we can see that there is only one layer showing after importing the exported archive. It helps to improve the performance, however, it is not recommended by Docker as Docker encourages to build a multilayer Docker image to use the cache functionality of the image building process.

Advantages of Docker Export

  1. Speed: Docker export is faster than re-building the Docker image if any minor changes required.
  2. Image Sharing: As we know if we want to share the Docker image we need to push it to a registry, however, we can use Docker export to export it as an archive and we can share it with others like we share files.
  3. Performance: When we import the exported archive using Docker import it creates a single layer Docker image that improves the performance of the container.

Conclusion

Docker export is a command-line tool to export any container as an archive and archive contains a regular Linux filesystem with all the container data except data of mounted volumes. docker export and docker import both commands mostly work together.

Recommended Articles

This is a guide to Docker Export. Here we discuss the Introduction of Docker Export and its advantages along with its Examples and Code Implementation. You can also go through our other suggested articles to learn more –

  1. LXD vs Docker – Top Differences
  2. Top 7 Advantages of Docker
  3. Introduction to Docker Swarm Architecture
  4. Docker Architecture with Benefits

Docker Training (4 Courses, 2 Projects)

4 Online Courses

3 Hands-on Projects

11+ Hours

Verifiable Certificate of Completion

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Docker Tutorial
  • Docker
    • What is Docker in Linux
    • Docker Architecture
    • Install Docker
    • Advantages of Docker
    • Docker Commands
    • Docker Toolbox
    • Docker Storage Drivers
    • Docker Push
    • Docker run Command
    • Dockerfile
    • Docker Containers
    • Docker Container Linking
    • Docker Stop Container
    • Docker List Containers
    • Docker Registry
    • Docker Volume
    • Docker Images
    • Docker Export
    • Docker Import
    • Docker Systemd
    • Docker Stack
    • Docker Privileged
    • Docker Pull
    • Docker ENTRYPOINT
    • Docker Start
    • Docker system prune
    • Docker Hosts
    • Docker Logging
    • Docker Save
    • Docker Commands Cheat Sheet
  • Docker Swarm
    • What is Docker Swarm
    • Docker Swarm Architecture
  • Interview Questions
    • Docker Interview Questions

Related Courses

Docker Training Course

Python Training Course

Software Development Course Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Docker Training (4 Courses, 2 Projects) Learn More