Why am I not able to delete a directory when building a container with podman?

  buildah, docker, gitlab, podman, ubuntu

I am building a custom image with rootless podman from a Dockerfile within a CI/CD pipeline in GitLab.

In this container, I would like to remove a directory to slim down the size of the resulting image.

For example like this:

FROM ubuntu:20.04

# Disable apt cache
COPY ./02nocache /etc/apt/apt.conf.d/02nocache
RUN rm -rf /var/cache/apt/archives

When I run this it says the following:

STEP 6: RUN rm -rf /var/cache/apt/archives
rm: cannot remove '/var/cache/apt/archives/lock': File exists
rm: cannot remove '/var/cache/apt/archives/partial': File exists
error building at step {Env:[PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin DEBIAN_FRONTEND=noninteractive] Command:run Args:[rm -rf /var/cache/apt/archives] Flags:[] Attrs:map[] Message:RUN rm -rf /var/cache/apt/archives Original:RUN rm -rf /var/cache/apt/archives}: error while running runtime: exit status 1

I call the build like this:

    stage: prepare
    image: buildah/buildah
        - podman build -t $CONTAINER_TEST_IMAGE .

So far I tried to use sudo to call podman, I tried running the rm with sudo but to no avail. When searching for this issue on the web I did not find anything that helped me out.

Does anyone know how to fix this issue?

Source: Docker Questions