Search by Tags

Modifying Our Debian Based Container Images

 
Applicable for

Article updated at 17 Feb 2020
Compare with Revision


Subscribe for this article updates

Introduction

Toradex provides Debian-based images. In onder to take full advantage of them, this article will show you how to modify them to fit your necessities, here are two ways to modify our container images:

  • With Docker commit command (useful ONLY to develop the Dockerfile or for quick prototyping)

  • Write Dockerfile (most commonly used method)

This article complies to the Typographic Conventions for Torizon Documentation.

Prerequisites

To have a better understanding of this article the following prerequisites are proposed:

Build a Container based on our Debian-based Docker images

Dockerfile

Create a file named Dockerfile. In this case we are going to add the nano text editor:

Dockerfile
FROM torizon/arm32v7-debian-base RUN apt update && apt install nano -y
Dockerfile
FROM torizon/arm64v8-debian-base RUN apt update && apt install nano -y

Note: a standard Dockerfile has no extension, therefore make sure your file is not named Dockerfile.txt, especially since file extensions are hidden by default. Consult this lesson's FAQ for details about naming.

After creating the file, you can log in to your Docker account:

$ docker login

Now that you are logged to your credentials and the file was created, it's time to finally build your image:

$ docker build -t <username>/my-dockerfile .

If you want, you can upload your image to Docker Hub for future use:

$ docker push <username>/my-dockerfile

Note that <username> is your Docker username.

Docker commit

Warning: you should use a Dockerfile and avoid the approach documented in this section.

  • Pull and run our Debian based container interactively

  • Customize the container to your needs

  • Use docker commit (command allows users to take a running container and save its current state as a container image)

How to commit, save and copy container image from one host and load it to another without using repository:

Firstly, run a container:

$ docker run -it --name test-container torizon/arm32v7-debian-base

Install the applications you want, in this case, we are going to install nano as a simple example.

$$ apt update && apt install nano

After that, you are available to exit from the container by a (simply type exit).

List the stopped containers and make sure to write down your container ID:

$ docker ps -a 

  • Docker container ID

    Docker container ID

After the changes were made, you can commit them by using the command below:

$ docker commit <container_ID> <image_name> 

Now that changes were commited, we are going to save the image to a .tar file:

$ docker save -o <path_for_generated_tar_file> <image_name>

The scp command will be used for transferring the image.tar to another host.

$ scp <path_for_generated_tar_file> <user>@<target_host>:/<folder> 

Last but not least, we will use SSH, which makes available for you to access the host and load your image.

$ ssh <user>@<target_host> 'docker load -i <tar_file>

For information about SSH please refer to SSH Article.

Docker Multi-stage Build

Downloading the final image takes a lot of time and space, so it's very important to keep it tiny. With multi-stage builds, you can build a docker image in stages with multiple FROM instructions in your Dockerfile. Each FROM instruction starts a new build stage and you can copy only the layers which are necessary from the previous stage. This is very useful to avoid including build dependencies in your final image and it also becomes easier to understand.

For more information, please refer to Docker Multistage documentation.

Next Steps