Export and Import Docker images and container with asp.net core application

3 minute read

Sometimes we need to work in a disconnected environment. In that situation, we need to export/import docker images and container. Here I will show how to export and import images and container.

Save and load docker images

Step 1: Create a sample asp.net core application - name CatalogApp

Step 2: Containerize CatalogApp

  • Add a dockerfile in the root directory - here dockerfile and sln file exists in the same directory.
  • You see how to Containerize a .NET Core app
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY *.sln .
COPY CatalogApp/*.csproj ./CatalogApp/
RUN dotnet restore

# copy everything else and build app
COPY CatalogApp/. ./CatalogApp/
WORKDIR /source/CatalogApp
RUN dotnet publish -c release -o /app --no-restore

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "CatalogApp.dll"]

Step 3: Run in linux container

  • Navigate to the Dockerfile folder
  • Run the following commands to build the application in docker
    docker build -t catalogapp .
    
  • Now you will see an image name catalogapp is created. To check images, run the following command.
    docker images
    
  • If you want to run the application (images) in docker, run the following command
    docker run -it --rm -p 5000:80 --name catalogapp_container catalogapp
    
  • Browse application at http://localhost:5000/ to see output

Step 3: Save docker images

  • To export “catalogapp” image run the following command
    docker save -o D:\Images\catalogapp.tar catalogapp
    
  • You will see a catalogapp.tar file in D:\Images\ directory

Step 4: Now load docker images

  • You may run the following command to remove all docker images
    docker rmi $(docker images -q)
    
  • Go to to the directory D:\Images\
  • Run the following command to load images. You will see two images is created with dev and latest tag.
    PS D:\Images> docker load -i catalogapp.tar
    

Run loaded image

  • Now run the following command to run the catalogapp images with latest tag.
    docker run -it --rm -p 5000:80 --name catalogapp_container catalogapp:latest
    
  • Browse application at http://localhost:5000/ to see output

Export and import docker container

Step 1: Create a sample asp.net core application as before - name CatalogApp

Step 2: Containerize CatalogApp

  • Add a dockerfile in the root directory - here dockerfile and sln file exists in the same directory.
  • You see how to Containerize a .NET Core app
# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /source

# copy csproj and restore as distinct layers
COPY *.sln .
COPY CatalogApp/*.csproj ./CatalogApp/
RUN dotnet restore

# copy everything else and build app
COPY CatalogApp/. ./CatalogApp/
WORKDIR /source/CatalogApp
RUN dotnet publish -c release -o /app --no-restore

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=build /app ./
ENTRYPOINT ["dotnet", "CatalogApp.dll"]

Step 3: Run in linux container

  • Navigate to the Dockerfile folder
  • Run the following commands to build the application in docker
    docker build -t catalogapp .
    
  • Now you will see an image name catalogapp is created. To check images, run the following command.
    docker images
    
  • If you want to run the application (images) in docker, run the following command
    docker run -it --rm -p 5000:80 --name catalogapp_container catalogapp
    
  • Browse application at http://localhost:5000/ to see output

Step 3: Export docker container

  • To export “catalogapp_container” container run the following command
    docker export -o D:\Containers\catalogapp_container.tar catalogapp_container
    
  • You will see a catalogapp_container file in D:\Containers\ directory

Step 4: Now import docker container

  • You may run the following command to remove all docker images
    docker rmi $(docker images -q)
    
  • Go to to the directory D:\Containers\
  • Run the following command to load images. You will see two images is created with dev and latest tag.
    PS D:\Containers> docker import catalogapp_container.tar catalogapp:imp
    

Run Imported image

  • Now run the following command to run the catalogapp images with latest tag.
    docker run -it --rm -p 5000:80 --name catalogapp_container catalogapp:imp
    
  • Browse application at http://localhost:5000/ to see output

Summary

Let’s summarize the idea of image and container export and import.

  • Image load is just copying and container import is just like turn container to image.
  • To export and import images, use docker save and load command.
  • To export and import container, use docker export and import command.
  • Exported tar file is smaller than saved tar file. Because the exported file is a container, when the export file is imported, all the history of the image (that is, the information of each layer) cannot be retained, and the rollback operation cannot be performed. And save is based on the mirror image, so you can completely retain the information of each layer when importing.
  • It’s not possible to rename loaded images but you can do it for imported image like catalogapp:imp

Source Code

Comments