Managing Images with Buildah and Skopeo for Podman

As containerization becomes increasingly popular, container management tools are becoming more and more important. Two of the most popular container management tools are Buildah and Skopeo, which provide developers with a powerful set of tools for building and managing containers.

Skopeo

Skopeo is a powerful container management tool, designed to help developers manage container images across different container registries. Skopeo provides a command-line tool for inspecting, copying, and distributing container images, allowing developers to manage container images across different environments and container registries.

One of the key advantages of Skopeo is that it provides a powerful set of features for inspecting and analyzing container images. Skopeo can be used to analyze container images for vulnerabilities, ensuring that developers are aware of any security risks associated with the container images they are using. Skopeo can also be used to verify the integrity of container images, ensuring that they have not been tampered with or altered in any way.

Skopeo inspect

Skopeo can be used to inspect a container image without pulling it to the machine. skopeo inspect with the image name can  be used to inspect the image

[root@localhost ~]# skopeo inspect docker://registry.access.redhat.com/ubi8
{
"Name": "registry.access.redhat.com/ubi8",
"Digest": "sha256:8be695c0f81d39eaaf674186183210a8b36e914a9a89420085629f2235aa5f7d",
"RepoTags": [
<trimmed>
"maintainer": "Red Hat, Inc.",
"name": "ubi8",
"release": "1054.1675788412",
"summary": "Provides the latest release of Red Hat Universal Base Image 8.",
"url": "https://access.redhat.com/containers/#/registry.access.redhat.com/ubi8/images/8.7-1054.1675788412",
"vcs-ref": "a995512a05037e3b60bbb1bf9fa6e394063131c3",
"vcs-type": "git",
"vendor": "Red Hat, Inc.",
"version": "8.7"
},
"Architecture": "amd64",
"Os": "linux",
"Layers": [
"sha256:ea0a20a2c44861ba5e4cd80a32868b6f8265cd3ee81310d4d493a1b50d1c0d8c"
],
"Env": [
"PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
"container=oci"
]

Skopeo copy

To copy a container image from one registry to another, we can use the skopeo copy command followed by the source and destination registry locations, in the format

<transport>://<source-image-name>:<source-tag> <transport>://<destination-image-name>:<destination-tag>

skopeo copy docker://quay.io/skopeo/stable:latest docker://registry.example.com/skopeo:latest

Exporting a container image to a file

To export a container image to a file on your local system, we can use the skopeo copy command with the dir: transport followed by the name of the image and the path to the output file:

 skopeo copy docker://docker.io/library/nginx:latest dir:/path/to/nginx.tar

Authenticating with a container registry

skopeo login command followed by the registry location and login credentials:

$ skopeo login docker://registry.example.com --username myusername --password mypassword

Buildah

  • Buildah is a container building tool that enables users to build and modify container images without the need for a Docker daemon. Unlike Docker, Buildah does not require a separate daemon process to run container builds, which makes it lightweight and easy to use. Buildah allows developers to create container images using a variety of inputs, including Dockerfiles, OCI images, and tarball archives.
  • Buildah has the advantage that it includes a scripting language, which allows you to build an image from scratch, such that is not based on any base image

One of the most significant advantages of Buildah is its flexibility. Buildah provides a wide range of options for building container images, including the ability to:

  • Build container images from scratch
  • Build images from existing Dockerfiles or OCI image specifications
  • Build images using custom build scripts and commands
  • Build images in a completely isolated environment
  • Build images with rootless capabilities

Building an Image from a Dockerfile

One of the most common ways to build a container image is by using a Dockerfile. Buildah provides a simple and flexible way to build images from Dockerfiles.

Below code snippet shows an example of how to use Buildah to build an image from a Dockerfile:

# Create a new container image build context 
buildah bud -t myimage:latest . 
 # List the available images 
buildah images 
# Run a container from the newly built image
 buildah run myimage:latest echo "Hello, world!"

In this example, the buildah bud command is used to build a container image from the current directory, which contains a Dockerfile. The -t option is used to specify the name and tag of the new image. Once the image is built, the buildah images command can be used to list the available images, and the buildah run command can be used to run a container from the newly built image.

Building an Image from an OCI Image Specification

Buildah can also build container images from OCI image specifications, which provide a standardized format for container images.

Here's an example of how to use Buildah to build an image from an OCI image specification:

# Pull the base image from a registry
buildah from docker://docker.io/library/alpine:latest

# Copy files into the container image
buildah copy ldhcontainer /path/to/files /usr/local/bin/

# Set the default command for the container
buildah config --cmd "/usr/local/bin/myapp" ldhcontainer

# Commit the changes to a new container image
buildah commit ldhcontainer myimage:latest

# List the available images
buildah images

# Run a container from the newly built image
buildah run myimage:latest echo "Hello, world!"

In this example,

  • the buildah from command is used to pull the base image from a registry
  • the buildah copy command is used to copy files into the container image
  • the buildah config command is used to set the default command for the container
  • Finally, the buildah commit command is used to commit the changes to a new container

Search on LinuxDataHub

Leave a Comment