Chapter 20. Working with containers using Buildah
With Buildah, you can do several operations on a container image or container from the command line. Examples of operations are: create a working container from scratch or from a container image as a starting point, create an image from a working container or using a Containerfile, configure a container’s entrypoint, labels, port, shell, and working directory. You can mount working containers directories for filesystem manipulation, delete a working container or container image, and more.
You can then create an image from a working container and push the image to the registry.
20.1. Running commands inside of the container Copy linkLink copied to clipboard!
Use the buildah run command to execute a command from the container.
Prerequisites
-
The
container-toolsmodule is installed. - A pulled image is available on the local system.
Procedure
Display the operating system version:
buildah run ubi-working-container cat /etc/redhat-release Red Hat Enterprise Linux release 8.4 (Ootpa)
# buildah run ubi-working-container cat /etc/redhat-release Red Hat Enterprise Linux release 8.4 (Ootpa)Copy to Clipboard Copied! Toggle word wrap Toggle overflow
20.2. Inspecting containers and images with Buildah Copy linkLink copied to clipboard!
Use the buildah inspect command to display information about a container or image.
Prerequisites
-
The
container-toolsmodule is installed. - An image was built using instructions from Containerfile. For details, see section Building an image from a Containerfile with Buildah.
Procedure
Inspect the image:
To inspect the myecho image, enter:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow To inspect the working container from the
myechoimage:Create a working container based on the
localhost/myechoimage:buildah from localhost/myecho
# buildah from localhost/myechoCopy to Clipboard Copied! Toggle word wrap Toggle overflow Inspect the
myecho-working-containercontainer:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
20.3. Modifying a container using buildah mount Copy linkLink copied to clipboard!
Use the buildah mount command to display information about a container or image.
Prerequisites
-
The
container-toolsmodule is installed. - An image built using instructions from Containerfile. For details, see section Building an image from a Containerfile with Buildah.
Procedure
Create a working container based on the
registry.access.redhat.com/ubi8/ubiimage and save the name of the container to themycontainervariable:mycontainer=$(buildah from localhost/myecho) echo $mycontainer
# mycontainer=$(buildah from localhost/myecho) # echo $mycontainer myecho-working-containerCopy to Clipboard Copied! Toggle word wrap Toggle overflow Mount the
myecho-working-containercontainer and save the mount point path to themymountvariable:mymount=$(buildah mount $mycontainer) echo $mymount
# mymount=$(buildah mount $mycontainer) # echo $mymount /var/lib/containers/storage/overlay/c1709df40031dda7c49e93575d9c8eebcaa5d8129033a58e5b6a95019684cc25/mergedCopy to Clipboard Copied! Toggle word wrap Toggle overflow Modify the
myechoscript and make it executable:echo 'echo "We modified this container."' >> $mymount/usr/local/bin/myecho chmod +x $mymount/usr/local/bin/myecho
# echo 'echo "We modified this container."' >> $mymount/usr/local/bin/myecho # chmod +x $mymount/usr/local/bin/myechoCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create the
myecho2image from themyecho-working-containercontainer:buildah commit $mycontainer containers-storage:myecho2
# buildah commit $mycontainer containers-storage:myecho2Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
List all images in local storage:
buildah images
# buildah images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/library/myecho2 latest 4547d2c3e436 4 minutes ago 234 MB localhost/myecho latest b28cd00741b3 56 minutes ago 234 MBCopy to Clipboard Copied! Toggle word wrap Toggle overflow Run the
myecho2container based on thedocker.io/library/myecho2image:podman run --name=myecho2 docker.io/library/myecho2
# podman run --name=myecho2 docker.io/library/myecho2 This container works! We even modified it.Copy to Clipboard Copied! Toggle word wrap Toggle overflow
20.4. Modifying a container using buildah copy and buildah config Copy linkLink copied to clipboard!
Use buildah copy command to copy files to a container without mounting it. You can then configure the container using the buildah config command to run the script you created by default.
Prerequisites
-
The
container-toolsmodule is installed. - An image built using instructions from Containerfile. For details, see section Building an image from a Containerfile with Buildah.
Procedure
Create a script named
newechoand make it executable:cat newecho echo "I changed this container" chmod 755 newecho
# cat newecho echo "I changed this container" # chmod 755 newechoCopy to Clipboard Copied! Toggle word wrap Toggle overflow Create a new working container:
buildah from myecho:latest
# buildah from myecho:latest myecho-working-container-2Copy to Clipboard Copied! Toggle word wrap Toggle overflow Copy the newecho script to
/usr/local/bindirectory inside the container:buildah copy myecho-working-container-2 newecho /usr/local/bin
# buildah copy myecho-working-container-2 newecho /usr/local/binCopy to Clipboard Copied! Toggle word wrap Toggle overflow Change the configuration to use the
newechoscript as the new entrypoint:buildah config --entrypoint "/bin/sh -c /usr/local/bin/newecho" myecho-working-container-2
# buildah config --entrypoint "/bin/sh -c /usr/local/bin/newecho" myecho-working-container-2Copy to Clipboard Copied! Toggle word wrap Toggle overflow Optional: Run the
myecho-working-container-2container whixh triggers thenewechoscript to be executed:buildah run myecho-working-container-2 -- sh -c '/usr/local/bin/newecho'
# buildah run myecho-working-container-2 -- sh -c '/usr/local/bin/newecho' I changed this containerCopy to Clipboard Copied! Toggle word wrap Toggle overflow Commit the
myecho-working-container-2container to a new image calledmynewecho:buildah commit myecho-working-container-2 containers-storage:mynewecho
# buildah commit myecho-working-container-2 containers-storage:mynewechoCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
List all images in local storage:
buildah images
# buildah images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/library/mynewecho latest fa2091a7d8b6 8 seconds ago 234 MBCopy to Clipboard Copied! Toggle word wrap Toggle overflow
20.5. Pushing containers to a private registry Copy linkLink copied to clipboard!
Use buildah push command to push an image from local storage to a public or private repository.
Prerequisites
-
The
container-toolsmodule is installed. - An image was built using instructions from Containerfile. For details, see section Building an image from a Containerfile with Buildah.
Procedure
Create the local registry on your machine:
podman run -d -p 5000:5000 registry:2
# podman run -d -p 5000:5000 registry:2Copy to Clipboard Copied! Toggle word wrap Toggle overflow Push the
myecho:latestimage to thelocalhostregistry:Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
List all images in the
localhostrepository:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Inspect the
docker://localhost:5000/myecho:latestimage:Copy to Clipboard Copied! Toggle word wrap Toggle overflow Pull the
localhost:5000/myechoimage:podman pull --tls-verify=false localhost:5000/myecho2 podman run localhost:5000/myecho2
# podman pull --tls-verify=false localhost:5000/myecho2 # podman run localhost:5000/myecho2 This container works!Copy to Clipboard Copied! Toggle word wrap Toggle overflow
20.6. Pushing containers to the Docker Hub Copy linkLink copied to clipboard!
Use your Docker Hub credentials to push and pull images from the Docker Hub with the buildah command.
Prerequisites
-
The
container-toolsmodule is installed. - An image built using instructions from Containerfile. For details, see section Building an image from a Containerfile with Buildah.
Procedure
Push the
docker.io/library/myecho:latestto your Docker Hub. Replaceusernameandpasswordwith your Docker Hub credentials:buildah push --creds username:password \ docker.io/library/myecho:latest docker://testaccountXX/myecho:latest
# buildah push --creds username:password \ docker.io/library/myecho:latest docker://testaccountXX/myecho:latestCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Get and run the
docker.io/testaccountXX/myecho:latestimage:Using Podman tool:
podman run docker.io/testaccountXX/myecho:latest
# podman run docker.io/testaccountXX/myecho:latest This container works!Copy to Clipboard Copied! Toggle word wrap Toggle overflow Using Buildah and Podman tools:
buildah from docker.io/testaccountXX/myecho:latest podman run myecho-working-container-2
# buildah from docker.io/testaccountXX/myecho:latest myecho2-working-container-2 # podman run myecho-working-container-2Copy to Clipboard Copied! Toggle word wrap Toggle overflow
20.7. Removing containers with Buildah Copy linkLink copied to clipboard!
Use the buildah rm command to remove containers. You can specify containers for removal with the container ID or name.
Prerequisites
-
The
container-toolsmodule is installed. - At least one container has been stopped.
Procedure
List all containers:
buildah containers
# buildah containers CONTAINER ID BUILDER IMAGE ID IMAGE NAME CONTAINER NAME 05387e29ab93 * c37e14066ac7 docker.io/library/myecho:latest myecho-working-containerCopy to Clipboard Copied! Toggle word wrap Toggle overflow Remove the myecho-working-container container:
buildah rm myecho-working-container
# buildah rm myecho-working-container 05387e29ab93151cf52e9c85c573f3e8ab64af1592b1ff9315db8a10a77d7c22Copy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
Ensure that containers were removed:
buildah containers
# buildah containersCopy to Clipboard Copied! Toggle word wrap Toggle overflow