DEV Community

Leonard Soetedjo
Leonard Soetedjo

Posted on

1

Building container image in AWS CodeBuild with buildah

While I was trying out AWS CodeBuild, I realised that the available build images are only Amazon Linux 2 & Ubuntu 18.04. Being used to CentOS, I initially tested out using Amazon Linux 2.

However, Amazon Linux 2 is based on RHEL7. I faced issues in building image using buildah in Amazon Linux 2. As such, I decided to take the challenge to use custom image based on centos:latest, which buildah can be easily installed and configured.

When using custom image, there are a couple of things that need to be done as these will not be available in the image:

  1. install aws cli
  2. install buildah

1. Installing AWS CLI v2

As per AWS CLI's guide, we'll download & install the latest version:

curl https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip
unzip -q awscliv2.zip
./aws/install
Enter fullscreen mode Exit fullscreen mode

2. Installing buildah

Installing buildah is simpler in CentOS8:

dnf install -y buildah fuse-overlayfs
Enter fullscreen mode Exit fullscreen mode

However, the build process was giving an error:
failed to mount overlay for metacopy check with "nodev,metacopy=on" options: invalid argument. Googling around, I found this issue

Error: failed to mount overlay for metacopy check with "nodev,metacopy=on" options: invalid argument #8118

Is this a BUG REPORT or FEATURE REQUEST? (leave only one on its own line)

/kind bug

Description

Additional information you deem important (e.g. issue happens only occasionally):

Output of podman version:

podman version 2.1.1

Output of podman info --debug:

Error: failed to mount overlay for metacopy check with "nodev,metacopy=on" options: invalid argument

Package info (e.g. output of rpm -q podman or apt list podman):

(paste your output here)

Have you tested with the latest version of Podman and have you checked the Podman Troubleshooting Guide?

this is last version on fedora repo

Additional environment details (AWS, VirtualBox, physical, etc.): vps at provider of vps

. What needs to be done is to remove the metacopy=on in /etc/containers/storage.conf. Subsequently, the build is successful :)

Below is a summary of my CodeBuild's buildspec.yml (configured for terraform):

version: 0.2

phases:
  install:
    commands:
      - dnf -y update

      - echo "Install aws cli v2"
      - dnf -y install unzip
      - curl https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip -o awscliv2.zip
      - unzip -q awscliv2.zip
      - ./aws/install
      - export PATH=/usr/local/bin:$PATH

      - echo "Install buildah"
      - dnf install -y podman buildah fuse-overlayfs
      - sed -i 's/^mountopt =.*/mountopt = "nodev"/g' /etc/containers/storage.conf
  pre_build:
    commands:
      - echo "Login to ECR"
      - REPO_URI=${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com
      - aws ecr get-login-password --region ${AWS_DEFAULT_REGION} | buildah login --username AWS --password-stdin ${REPO_URI}
  build:
    commands:
      - echo "Build started on `date`"
      - cd ${CODEBUILD_SRC_DIR}
      - cat /etc/os-release
      - sh build-container.sh ${IMAGE_NAME}
  post_build:
    commands:
      - echo "Build completed on `date`"
      - TAG=`date +%Y%m%d_%H%M%S`
      - echo "Pushing image to repository ${IMAGE_NAME}"
      - buildah push ${IMAGE_NAME} ${REPO_URI}/${IMAGE_NAME}:${TAG}
Enter fullscreen mode Exit fullscreen mode

With the above, I'm able to build the container image using buildah and pushing it to my container repo.

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

AWS Security LIVE!

Hosted by security experts, AWS Security LIVE! showcases AWS Partners tackling real-world security challenges. Join live and get your security questions answered.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️