0

We are using python:3.9 image for the base, and run some command on that.

Base Image

########################
#  Base Image Section  #
########################
#
# Creates an image with the common requirements for a flask app pre-installed


# Start with a smol OS
FROM python:3.9

# Install basic requirements
RUN apt-get -q update -o Acquire::Languages=none && apt-get -yq install --no-install-recommends \
    apt-transport-https \
    ca-certificates && \
    apt-get autoremove -yq && apt-get clean && rm -rf "/var/lib/apt/lists"/*

# Install CA certs
# Prefer the mirror for package downloads
COPY ["ca_certs/*.crt", "/usr/local/share/ca-certificates/"]
RUN update-ca-certificates && \
    mv /etc/apt/sources.list /etc/apt/sources.list.old && \
    printf 'deb https://mirror.company.com/debian/ buster main contrib non-free\n' > /etc/apt/sources.list && \
    cat /etc/apt/sources.list.old >> /etc/apt/sources.list


# Equivalent to `cd /app`
WORKDIR /app

# Fixes a host of encoding-related bugs
ENV LC_ALL=C.UTF-8

# Tells `apt` and others that no one is sitting at the keyboard
ENV DEBIAN_FRONTEND=noninteractive

# Set a more helpful shell prompt
ENV PS1='[\u@\h \W]\$ '

#####################
#  ONBUILD Section  #
#####################
#
# ONBUILD commands take effect when another image is built using this one as a base.
# Ref: https://docs.docker.com/engine/reference/builder/#onbuild
#
#
# And that's it! The base container should have all your dependencies and ssl certs pre-installed,
# and will copy your code over when used as a base with the "FROM" directive.
ONBUILD ARG BUILD_VERSION
ONBUILD ARG BUILD_DATE


# Copy our files into the container
ONBUILD ADD . .

# pre_deps: packages that need to be installed before code installation and remain in the final image
ONBUILD ARG pre_deps
# build_deps: packages that need to be installed before code installation, then uninstalled after
ONBUILD ARG build_deps
# COMPILE_DEPS: common packages needed for building/installing Python packages. Most users won't need to adjust this,
# but you could specify a shorter list if you didn't need all of these.
ONBUILD ARG COMPILE_DEPS="build-essential python3-dev libffi-dev libssl-dev python3-pip libxml2-dev libxslt1-dev zlib1g-dev g++ unixodbc-dev"
# ssh_key: If provided, writes the given string to ~/.ssh/id_rsa just before Python package installation,
# and deletes it before the layer is written.
ONBUILD ARG ssh_key

# If our python package is installable, install system packages that are needed by some python libraries to compile
# successfully, then install our python package. Finally, delete the temporary system packages.
ONBUILD RUN \
if [ -f setup.py ] || [ -f requirements.txt ]; then \
    install_deps="$pre_deps $build_deps $COMPILE_DEPS" && \
    uninstall_deps=$(python3 -c 'all_deps=set("'"$install_deps"'".split()); to_keep=set("'"$pre_deps"'".split()); print(" ".join(sorted(all_deps-to_keep)), end="")') && \
    apt-get -q update -o Acquire::Languages=none && apt-get -yq install --no-install-recommends $install_deps && \
    if [ -n "${ssh_key}" ]; then \
        mkdir -p ~/.ssh && chmod 700 ~/.ssh && printf "%s\n" "${ssh_key}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa && \
        printf "%s\n" "StrictHostKeyChecking=no" > ~/.ssh/config && chmod 600 ~/.ssh/config || exit 1 ; \
    fi ; \
    if [ -f requirements.txt ]; then \
        pip3 install --no-cache-dir --compile -r requirements.txt || exit 1 ; \
    elif [ -f setup.py ]; then \
        pip3 install --no-cache-dir --compile --editable . || exit 1 ; \
    fi ; \
    if [ -n "${ssh_key}" ]; then \
        rm -rf ~/.ssh || exit 1 ; \
    fi ; \
fi

We build this image last year, and it was working fine, but we decided to use latest changes and build new base image, once we build it, it start failing for last RUN command.

DEBU[0280] Deleting in layer: map[]                     
INFO[0281] Cmd: /bin/sh                                 
INFO[0281] Args: [-c if [ -f setup.py ] || [ -f requirements.txt ]; then     install_deps="$pre_deps $build_deps $COMPILE_DEPS" &&     uninstall_deps=$(python3 -c 'all_deps=set("'"$install_deps"'".split()); to_keep=set("'"$pre_deps"'".split()); print(" ".join(sorted(all_deps-to_keep)), end="")') &&     apt-get -q update -o Acquire::Languages=none && apt-get -yq install --no-install-recommends $install_deps &&     if [ -n "${ssh_key}" ]; then         mkdir -p ~/.ssh && chmod 700 ~/.ssh && printf "%s\n" "${ssh_key}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa &&         printf "%s\n" "StrictHostKeyChecking=no" > ~/.ssh/config && chmod 600 ~/.ssh/config || exit 1 ;     fi ;     if [ -f requirements.txt ]; then         pip3 install --no-cache-dir --compile -r requirements.txt || exit 1 ;     elif [ -f setup.py ]; then         pip3 install --no-cache-dir --compile --editable . || exit 1 ;     fi ;     if [ -n "${ssh_key}" ]; then         rm -rf ~/.ssh || exit 1 ;     fi ; fi] 
INFO[0281] Running: [/bin/sh -c if [ -f setup.py ] || [ -f requirements.txt ]; then     install_deps="$pre_deps $build_deps $COMPILE_DEPS" &&     uninstall_deps=$(python3 -c 'all_deps=set("'"$install_deps"'".split()); to_keep=set("'"$pre_deps"'".split()); print(" ".join(sorted(all_deps-to_keep)), end="")') &&     apt-get -q update -o Acquire::Languages=none && apt-get -yq install --no-install-recommends $install_deps &&     if [ -n "${ssh_key}" ]; then         mkdir -p ~/.ssh && chmod 700 ~/.ssh && printf "%s\n" "${ssh_key}" > ~/.ssh/id_rsa && chmod 600 ~/.ssh/id_rsa &&         printf "%s\n" "StrictHostKeyChecking=no" > ~/.ssh/config && chmod 600 ~/.ssh/config || exit 1 ;     fi ;     if [ -f requirements.txt ]; then         pip3 install --no-cache-dir --compile -r requirements.txt || exit 1 ;     elif [ -f setup.py ]; then         pip3 install --no-cache-dir --compile --editable . || exit 1 ;     fi ;     if [ -n "${ssh_key}" ]; then         rm -rf ~/.ssh || exit 1 ;     fi ; fi] 
error building image: error building stage: failed to execute command: starting command: fork/exec /bin/sh: exec format error

We label the image, based on date, to know when it was working, we have base image, build on 12-09-22 works fine.

Something new in python:3.9 cause this issue. Same script was working.

Nilesh
  • 20,521
  • 16
  • 92
  • 148
  • Is it possible the image was built on an x86 system and run on an ARM system (like an M1 Mac), or _vice versa_? That would cause an `exec format error` message. – David Maze Feb 07 '23 at 16:12
  • yes, we build image in Mac, is there any way to make image which will work on x86 from Mac ? – Nilesh Feb 07 '23 at 16:22
  • 1
    ["exec format error" when running containers build with Apple M1 Chip (ARM based systems)](https://stackoverflow.com/questions/66920645/exec-format-error-when-running-containers-build-with-apple-m1-chip-arm-based) describes how to build an x86-architecture image on an M1 Mac (`docker build --platform=linux/amd64`). Is that enough to get you going? – David Maze Feb 07 '23 at 16:53
  • Thanks David for help. – Nilesh Feb 07 '23 at 17:33

0 Answers0