Solve the problem with error: Container ID cannot be mapped to a host ID

Solve the problem with error: Container ID cannot be mapped to a host ID

I'm using a multi-stage Dockerfile to build an image for a Java application started with run-java.sh. Everything works fine locally when building with Docker. However, when the code is pushed and run with Bitbucket Pipelines, I saw the following error:

failed to copy files: failed to copy file: Container ID 165636 cannot be mapped to a host ID

Below is the Dockerfile I'm using.

FROM curlimages/curl:7.79.1 AS downloader
ARG RUN_JAVA_VERSION=1.3.8
RUN curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /tmp/run-java.sh

FROM adoptopenjdk/openjdk11:jdk-11.0.13_8-alpine

ENV LANG='en_US.UTF-8' LANGUAGE='en_US:en'

RUN mkdir /deployments \
    && chown 1001 /deployments \
    && chmod "g+rwX" /deployments \
    && chown 1001:root /deployments

COPY --from=downloader /tmp/run-java.sh /deployments/run-java.sh

RUN chown 1001 /deployments/run-java.sh \
    && chmod 540 /deployments/run-java.sh


COPY --chown=1001 target/build-output/ /deployments/

EXPOSE 8080
USER 1001

ENV JAVA_INIT_MEM_RATIO=40
ENV JAVA_MAX_MEM_RATIO=60
ENV JAVA_MAIN_CLASS=com.myapp.Main
ENV JAVA_CLASSPATH=.:/deployments/classes:/deployments/lib/*

ENTRYPOINT [ "/deployments/run-java.sh" ]

Solution

It turns out this is a known issue. The solution is to change the owner of those files to root:root.

The downloader image curlimages/curl doesn't allow me to use chown due to permission error. So I have to switch to ubuntu image.

Below is the updated Dockerfile with ubuntu image.

FROM ubuntu:20.04 AS downloader
ARG RUN_JAVA_VERSION=1.3.8
RUN apt update \
    && apt install curl -y \
    && curl https://repo1.maven.org/maven2/io/fabric8/run-java-sh/${RUN_JAVA_VERSION}/run-java-sh-${RUN_JAVA_VERSION}-sh.sh -o /tmp/run-java.sh \
    && chown root:root /tmp/run-java.sh

After this change, the build can be run successfully using Bitbucket Pipelines. 🎉

© 2021 VividCode