Python: Docker image build -- save to and load from *.tar files.

behainguyen

Be Hai Nguyen

Posted on July 27, 2022

Python: Docker image build -- save to and load from *.tar files.

We can save Docker images to local .tar files, and later load and run those Docker images from local .tar files. I'm documenting my learning experimentations in this post.

I'm aware that we can create a local registry and push Docker images into this registry instead of the public ones. I like local files, I'm exploring this option first.

The code used in this post is the exact code developed for Python: Docker image build -- “the Werkzeug” problem 🤖! It can be cloned with:



git clone -b v1.0.2 https://github.com/behai-nguyen/flask-restx-demo.git


Enter fullscreen mode Exit fullscreen mode

Please note, in this post, all Docker images are built on Windows 10 Pro.

Table of contents

Environments

  1. Synology DS218 -- DSM 7.1-42661 Update 3.
  2. Windows 10 Pro -- version 10.0.19044 build 19044.
  3. Windows “docker” CLI -- version 20.10.12, build e91ed57.
  4. Windows Docker Desktop -- version 4.4.3. The latest version is 4.10.1.
  5. Synology DS218 -- it's accessed via its device name omphalos-nas-01 instead of its IP address.

Build and Save Windows images

Please see also this official document docker save.

To make it clean, I also removed all related containers and images before testing.

❶ Build as normal:



F:\flask_restx_demo\>docker build --tag flask-restx-demo .


Enter fullscreen mode Exit fullscreen mode

We can save images by using standard output redirection > or by the --output option. Images can be identified by their names, i.e. the values in the REPOSITORY column when listing images, or by their Ids.

❷ Save by image name:



F:\flask_restx_demo\>docker save flask-restx-demo > E:\docker-images\flask_restx_demo-win-by-name.tar


Enter fullscreen mode Exit fullscreen mode

❸ Save by image Id:

Images' Ids can be obtained via command:



docker images


Enter fullscreen mode Exit fullscreen mode

Saving the same image using its Id and --output option:



F:\flask_restx_demo\>docker save 1bbe6c6752a2 --output E:\docker-images\flask_restx_demo-win-by-id.tar


Enter fullscreen mode Exit fullscreen mode

The docker save command does not seem to print out anything:

031-01-windows-windows-save-to-files-a.png

The two ( 2 ) output files and their sizes. Please note the slight difference in sizes:

031-01-windows-windows-save-to-files-b.png

Build and Save linux/arm64 images

We've previously discussed how to prepare for and to build multi-arch images on Windows 10 Pro. Please see Synology DS218: unsupported Docker installation and usage... | Test installation -- points ❶ and ❸.

Basically, to prepare multi-arch build profile mybuilder, run:



C:\>docker buildx create --name mybuilder --driver-opt network=host --use


Enter fullscreen mode Exit fullscreen mode

Then to build, and to push to https://hub.docker.com/ after finishing building:



F:\some_project>docker buildx build --platform linux/arm64 --tag username/some-project --push .


Enter fullscreen mode Exit fullscreen mode

❶ Try building an image for linux/arm64 only, that is, do not push the image onto the registry afterward:



F:\flask_restx_demo>docker buildx build --platform linux/arm64 --tag flask-restx-demo-arm64 .


Enter fullscreen mode Exit fullscreen mode

No image loaded: this should make sense, since this build is for linux/arm64 not Windows.

While research for the problem, I found this post Where did the built multi-platform image go? #166, answers by user barcus provide the solution.

❷ To build and to save locally:



F:\flask_restx_demo>docker buildx build --platform linux/arm64 --output "type=docker,push=false,name=flask-restx-demo-arm64,dest=E:\docker-images\flask-restx-demo-arm64.tar" .


Enter fullscreen mode Exit fullscreen mode

Image file E:\docker-images\flask-restx-demo-arm64.tar, its size is less than half of the previous two ( 2 ):

031-02-windows-linux-arm64-save-to-files.png

Load and run Windows images

Please see also this official document docker load.

The two image files flask_restx_demo-win-by-id.tar and flask_restx_demo-win-by-name.tar should be the same. I'm testing them both anyway.

flask_restx_demo-win-by-id.tar

❶ Start clean. Remove any existing related containers and images.

❷ To load an image file, use:



F:\>docker load --input E:\docker-images\flask_restx_demo-win-by-id.tar


Enter fullscreen mode Exit fullscreen mode

The loaded image has the same Id with the original image's: 1bbe6c6752a2 -- please see the screen capture below:

031-03-load-win-image-by-id-on-windows.png

Since the loaded image does not have an image name assigned, we have to run it via its image Id.

❸ Run the loaded image by its Id: 1bbe6c6752a2



F:\>docker run --publish 8000:8000 --rm 1bbe6c6752a2


Enter fullscreen mode Exit fullscreen mode

We run it in none detached mode, that is, without option -d, so that we can see the output on command line screen:

031-04-run-win-image-by-id-on-windows.png

❹ We can access the container via the following URLs:

Swagger UI URL:



http://localhost:8000/api/v1/ui


Enter fullscreen mode Exit fullscreen mode

API URL:



http://localhost:8000/api/v1/trees


Enter fullscreen mode Exit fullscreen mode

flask_restx_demo-win-by-name.tar

❶ Start clean. Stop and remove previous test container.

❷ Remove any existing related images.

❸ To load:



F:\>docker load --input E:\docker-images\flask_restx_demo-win-by-name.tar


Enter fullscreen mode Exit fullscreen mode

This time, the loaded image has both REPOSITORY and TAG properties defined:

031-05-load-win-image-by-tag-on-windows.png

❹ Run the loaded image with a different port to the previous run --publish 9010:8000:



F:\>docker run --publish 9010:8000 --rm flask-restx-demo


Enter fullscreen mode Exit fullscreen mode

❺ As before, we can access the container via the following URLs, please note the port:



http://localhost:9010/api/v1/ui
http://localhost:9010/api/v1/trees


Enter fullscreen mode Exit fullscreen mode

Load and run linux/arm64 image on Synology DS218

Recall from section Build and Save linux/arm64 images, the image file has been saved to E:\docker-images\flask-restx-demo-arm64.tar.

❶ On the Synology DS218 box, start the Docker daemon if not already started:



$ sudo dockerd &


Enter fullscreen mode Exit fullscreen mode

❷ Start clean. On the Synology DS218 box, remove all related containers and images.

❸ Copy E:\docker-images\flask-restx-demo-arm64.tar to Synology DS218 box's $HOME/Test/.

❹ Load the image file with:



behai@omphalos-nas-01:~/Test$ sudo docker load --input flask-restx-demo-arm64.tar


Enter fullscreen mode Exit fullscreen mode

The image loads successfully, please see verifications in the screen capture below:

031-06-load-linux-arm64-on-ds218.png

❺ Run the loaded image with:



behai@omphalos-nas-01:~/Test$ sudo docker run --network=host -v "/run/docker.sock:/var/run/docker.sock" --rm flask-restx-demo-arm64


Enter fullscreen mode Exit fullscreen mode

It runs successfully:

031-07-run-linux-arm64-on-ds218.png

❻ As before, we can access the container via the following URLs from Windows 10 Pro:



http://omphalos-nas-01:8000/api/v1/ui
http://omphalos-nas-01:8000/api/v1/trees


Enter fullscreen mode Exit fullscreen mode

Please note, the repo now includes a test HTML page using JQuery test_client_app\jquery-ajax\TreeAPIClient.html, just copy it to a web site or a virtual web directory, and run it from there, for API URL use http://omphalos-nas-01:8000/api/v1/trees.

✿✿✿

I do enjoy learning and blogging about this feature of Docker, I find this feature very useful... Thank you for reading, and I hope you find the info in this post useful also.

💖 💪 🙅 🚩
behainguyen
Be Hai Nguyen

Posted on July 27, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related