linuxdockermacosdocker-composedocker-build

Why docker build isn't platform independent?


I have the following Dockerfile

FROM python:3.8-slim-buster

# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

ARG WORK_DIR=my-app

# Create a new directory to keep the project files
RUN mkdir $WORK_DIR

# Copy local contents into container
COPY ./ $WORK_DIR

# Setting newly created directory as PWD
WORKDIR $WORK_DIR

# Installing system dependencies
RUN apt-get update && apt-get install --no-install-recommends -y \
  # Packages for building python
  build-essential \
  # git CLI
  git

# Installing pip packages
RUN pip install pip -U && pip install psycopg2-binary==2.8.5

I built an image from it on my Ubuntu machine, without any issues (here is the complete build logs).

Now, I tried to build the image from another machine, macOS. Unfortunately, I got some errors

$ docker build -f Dockerfile . -t test-img
[+] Building 16.3s (10/10) FINISHED                                                                                                                                                          docker:desktop-linux
 => [internal] load build definition from Dockerfile                                                                                                                                                         0.0s
 => => transferring dockerfile: 654B                                                                                                                                                                         0.0s
 => [internal] load metadata for docker.io/library/python:3.8-slim-buster                                                                                                                                    2.5s
 => [internal] load .dockerignore                                                                                                                                                                            0.0s
 => => transferring context: 2B                                                                                                                                                                              0.0s
 => [1/6] FROM docker.io/library/python:3.8-slim-buster@sha256:8799b0564103a9f36cfb8a8e1c562e11a9a6f2e3bb214e2adc23982b36a04511                                                                              0.0s
 => [internal] load build context                                                                                                                                                                            0.0s
 => => transferring context: 5.16kB                                                                                                                                                                          0.0s
 => CACHED [2/6] RUN mkdir my-app                                                                                                                                                                            0.0s
 => [3/6] COPY ./ my-app                                                                                                                                                                                     0.0s
 => [4/6] WORKDIR my-app                                                                                                                                                                                     0.0s
 => [5/6] RUN apt-get update && apt-get install --no-install-recommends -y   build-essential   git                                                                                                          10.2s
 => ERROR [6/6] RUN pip install pip -U && pip install psycopg2-binary==2.8.5                                                                                                                                 3.5s 
------                                                                                                                                                                                                            
 > [6/6] RUN pip install pip -U && pip install psycopg2-binary==2.8.5:                                                                                                                                            
0.811 Requirement already satisfied: pip in /usr/local/lib/python3.8/site-packages (23.0.1)                                                                                                                       
0.932 Collecting pip                                                                                                                                                                                              
1.184   Downloading pip-24.0-py3-none-any.whl (2.1 MB)                                                                                                                                                            
1.496      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.1/2.1 MB 6.9 MB/s eta 0:00:00                                                                                                                               
1.536 Installing collected packages: pip
1.536   Attempting uninstall: pip
1.536     Found existing installation: pip 23.0.1
1.583     Uninstalling pip-23.0.1:
1.621       Successfully uninstalled pip-23.0.1
2.090 Successfully installed pip-24.0
2.090 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
2.719 Collecting psycopg2-binary==2.8.5
2.882   Downloading psycopg2-binary-2.8.5.tar.gz (381 kB)
3.055      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 381.7/381.7 kB 2.2 MB/s eta 0:00:00
3.106   Preparing metadata (setup.py): started
3.395   Preparing metadata (setup.py): finished with status 'error'
3.398   error: subprocess-exited-with-error
3.398   
3.398   × python setup.py egg_info did not run successfully.
3.398   │ exit code: 1
3.398   ╰─> [23 lines of output]
3.398       running egg_info
3.398       creating /tmp/pip-pip-egg-info-7ualc3qt/psycopg2_binary.egg-info
3.398       writing /tmp/pip-pip-egg-info-7ualc3qt/psycopg2_binary.egg-info/PKG-INFO
3.398       writing dependency_links to /tmp/pip-pip-egg-info-7ualc3qt/psycopg2_binary.egg-info/dependency_links.txt
3.398       writing top-level names to /tmp/pip-pip-egg-info-7ualc3qt/psycopg2_binary.egg-info/top_level.txt
3.398       writing manifest file '/tmp/pip-pip-egg-info-7ualc3qt/psycopg2_binary.egg-info/SOURCES.txt'
3.398       
3.398       Error: pg_config executable not found.
3.398       
3.398       pg_config is required to build psycopg2 from source.  Please add the directory
3.398       containing pg_config to the $PATH or specify the full executable path with the
3.398       option:
3.398       
3.398           python setup.py build_ext --pg-config /path/to/pg_config build ...
3.398       
3.398       or with the pg_config option in 'setup.cfg'.
3.398       
3.398       If you prefer to avoid building psycopg2 from source, please install the PyPI
3.398       'psycopg2-binary' package instead.
3.398       
3.398       For further information please check the 'doc/src/install.rst' file (also at
3.398       <https://www.psycopg.org/docs/install.html>).
3.398       
3.398       [end of output]
3.398   
3.398   note: This error originates from a subprocess, and is likely not a problem with pip.
3.399 error: metadata-generation-failed
3.399 
3.399 × Encountered error while generating package metadata.
3.399 ╰─> See above for output.
3.399 
3.399 note: This is an issue with the package mentioned above, not pip.
3.399 hint: See above for details.
------
Dockerfile:26
--------------------
  24 |     
  25 |     # Installing pip packages
  26 | >>> RUN pip install pip -U && pip install psycopg2-binary==2.8.5
--------------------
ERROR: failed to solve: process "/bin/sh -c pip install pip -U && pip install psycopg2-binary==2.8.5" did not complete successfully: exit code: 1

Question: Why the same Dockerfile is working in Linux without any issues but not in macOS?

Note: The error showing that pg config is missing and maybe something similar to this helps to resolve the error. But, that is not what I was originally looking for since the docker (or Dockerfile contents) has to be independent of the host machine and in my case, it is not.


Solution

  • Can you try this ?

    docker pull --platform linux/amd64 python:3.8-slim-buster
    
    docker build --platform linux/amd64 -t test-img .