How to Export and Import Docker Containers and images
Last Updated :
21 Jun, 2024
In software development, flexibility and portability are important. Suppose you’ve built a perfect application in a Docker container on your development machine and now you need to move this same setup to a colleague’s machine or a production server. How do you ensure your application, and all its dependencies and config, moves without a hitch? The answer is in exporting and importing Docker containers and images.
This post will walk you through the steps to export and import Docker containers and images so you can move your work across environments. Whether you’re sharing with a team, moving to a new server, or just backing up your containers, you must master these techniques. Let’s get started and learn how to make your Dockerized applications as portable as possible.
Key Terminologies
Docker
Docker is an open-source platform designed to automate the deployment of applications as lightweight, portable containers that run virtually anywhere. Docker is a platform that uses containerization to streamline software development.
Docker Image
A Docker image is a read-only template used to create containers. It is a lightweight, executable, stand-alone package that includes everything needed to run the software, including the code, a runtime, libraries, environment variables, and config files.
Container
A container is a runtime instance of an image. It provides a way to run an application in an isolated environment, encapsulating all the necessary dependencies and libraries, and eliminating potential conflicts between applications.
Dockerfile
A Dockerfile is a simple text file that contains a set of commands that Docker uses sequentially to build an image. To read more about Dockerfile, you can read this article.
Exporting and Importing Docker Images
Step-by-Step Process to Export a Docker Image
1. Identify the Image ID or Name: To export an image, you need its ID or name. List all images using:
docker images
2. Save the Image: Use the docker save command to save the image to a tarball.
docker save -o <filename>.tar <image_name>
3. Verify the Saved Image: Ensure that the tarball has been created successfully in the specified location.
Step-by-Step Process to Import a Docker Image
1. Transfer the Tarball: Move the tarball to the target system where you want to import the image.
2. Load the Image: Use the docker load command to load the image from the tarball.
docker load -i <filename>.tar
3. Verify the Loaded Image: List all images to verify the image has been loaded.
docker images
Exporting and Importing Docker Containers
Step by Step Process to Export a Docker Container
1. Identify the Container ID or Name: To export a container, you need its ID or name. You can list all running containers using the following command:
docker ps
2. Export the Container: Use the docker export command to export the container to a tarball.
docker export <container_id> -o <filename>.tar
3. Verify the Exported File: Ensure that the tarball has been created successfully in the specified location.
Step by Step Process to Import a Docker Container
1. Transfer the Tarball: Move the tarball to the target system where you want to import the container.
2. Import the Container: Use the docker import command to import the tarball into Docker as an image.
docker import <filename>.tar <new_image_name>
Note: Importing a Docker container involves converting the exported tarball of a container's filesystem back into a Docker image instead. This process does not directly create a new running container but provides an image from which you can launch new containers.
3. Run a New Container: Create and run a new container from the imported image.
Example: Exporting and Importing Docker Images and Containers
Project Setup
Let's setup a basic express server to illustrate the whole process from spinning up a container to exporting it:
1. Create a package.json file to manage the project dependencies.
File: package.json
{
"name": "basic-express-server",
"version": "1.0.0",
"description": "Demo for exporting and importing docker containers/images",
"main": "./src/app.js",
"scripts": {
"start": "node ./src/app.js"
},
"author": "Your Name",
"license": "ISC",
"dependencies": {
"express": "^4.19.2"
}
}
Run npm install to install the dependencies.
2. Create a file named app.js in src folder.
File: src/app.js
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});
3. Let's create a Dockerfile to containerize the Express server.
File: Dockerfile
FROM node
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
CMD [ "npm", "start" ]
EXPOSE 3000
4. Create a .dockerignore file to prevent copying unnecessary files to the Docker image.
File: .dockerignore
node_modules
npm-debug.log
Folder Structure
This is how your folder structure should look like after following the above steps:

5. Let's build an image of our Express server by running this command:
docker build -t basic-express-server .
6. Verify the created image by listing all the images:
docker image ls
.png)
Exporting Docker Image
1. Save the image to a tarball:
docker save -o demo-image.tar basic-express-server
where,
-o, --output string Write to a file, instead of STDOUT
Check if the tarball file is generated or not.

2. Let's first our already built image so that we can load the image again using the tarball file. You can remove the image using this command:
docker rmi <image_id>
Check whether the image is deleted or not by listing all the images.
Importing Docker Image
3. Now, let's load the image again using the tarball file:
docker load -i demo-image.tar

Again, list all the images to see the newly loaded image.
4. Let's now run a container using this image:
docker run -p 3000:3000 -d basic-express-server
You can list down the running containers using:
docker ps
.png)
Exporting Docker Container
5. To export this container, run:
docker export cff6965045e1 -o container.tar
A tarball file will be created named container.tar:

Importing Docker Container
6. Import the Docker container by running:
docker import container.tar new-image

List all the images and verify if the new image is present there or not.
7. Run a container using the new image created by importing the tarball file and check if everything is working fine.
Similar Reads
How to Push a Container Image to a Docker Repository?
In this article we will look into how you can push a container image to a Docker Repository. We're going to use Docker Hub as a container registry, that we're going to push our Docker image to. Follow the below steps to push container Image to Docker repository:Step 1: Create a Docker Account The f
3 min read
Docker - Remove All Containers and Images
In Docker, if we have exited a container without stopping it, we need to manually stop it as it has not stopped on exit. Similarly, for images, we need to delete them from top to bottom as some containers or images might be dependent on the base images. We can download the base image at any time. So
10 min read
How To Create a Docker Container from an Existing Image?
Docker is an open-source software, that is used to contanerize our applications. Containerizing applications makes deployment a lot easier. For containerizing applications, docker uses Docker images, which act like templates for making containers. Today we will learn how to create a container from a
9 min read
How to Use Ansible for Docker Container Management
Containerization has become one of the foundations for realizing scalable, reliable, and efficient application deployments in modern DevOps practice. Docker, as a leading containerization platform, enables developers to package applications and all dependencies into containers for consistency across
9 min read
How To Push A Docker Image To Amazon ECR?
We go over how to submit a Docker image to the Amazon Elastic Container Registry (ECR) in this tutorial. By offering a safe, scalable registry for storing and distributing Docker images inside the AWS ecosystem, Amazon ECR streamlines container management. To upload and maintain your containerized a
4 min read
How to Remove Docker Containers
Docker is a platform that provides a set of PaaS (Platform as a Service) products that help developers containerize applications, allowing them to run consistently across various supported environments. The core component of Docker is Docker Engine which consists of: Docker Daemon: The process respo
2 min read
How To Deploy A Container To Azure Container Instances ?
In layman's terms, how will it feel when a developer develops an app and a member of the testing team tries to test the application on his/her OS? They will have to install the packages, and libraries required to run the app. Well, in this case, the container makes it easy and faster to rapidly depl
8 min read
How To See Docker Image Contents?
In the evolving world of containerization, Docker has emerged as a tool for packaging and deploying applications. While creating and sharing Docker images has streamlined the development and deployment process. Contents of a Docker image are important for troubleshooting, optimizing, and ensuring se
3 min read
Connecting Two Docker Containers Over the Same Network
Whenever we expose a container's port in docker, it creates a network path from the outside of that machine, through the networking layer, and enters that container. In this way, other containers can connect to it by going out to the host, turning around, and coming back in along that path.Docker of
3 min read
Copying Files to and from Docker Containers
While working on a Docker project, you might require copying files to and from Docker Containers and your Local Machine. Once you have built the Docker Image with a particular Docker build context, building it again and again just to add small files or folders inside the Container might be expensive
9 min read