Poetry install fails inside docker with “error: externally-managed-environment”

I am trying to install the dependencies for a project which uses Poetry, inside a docker container.

If I remove the line /root/.local/bin/poetry config virtualenvs.create false Poetry installs, but then then project is installed inside a venv. Because this is inside a docker container, I don’t need or want the packages installed in a venv.

The reason for this is I want other apps and tools inside the container to have access to the installed dependencies. If the deps installed by poetry are in a venv, one has to run a program using poetry run foo to access those deps, but if foo forks new threads or subprocess, they aren’t inside the poetry shell environment and can’t won’t have access to the deps, so a multi-threaded or multi-process application started with poetry run foo doesn’t work.

Therefore I just want to install the poetry deps globally (which is fine, because this is contained in a container).

This is the Dockerfile:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>FROM ubuntu:24.04
SHELL ["/bin/bash", "-c"] # Poetry doesn't work with sh
RUN apt-get update &&
apt-get install -y ca-certificates build-essential python3-dev pipx gpg libenchant-2-2
RUN pipx install poetry && pipx ensurepath
COPY ./pyproject.toml /pyproject.toml
COPY ./poetry.lock /poetry.lock
RUN /root/.local/bin/poetry config virtualenvs.create false &&
/root/.local/bin/poetry install -vv --no-interaction
</code>
<code>FROM ubuntu:24.04 SHELL ["/bin/bash", "-c"] # Poetry doesn't work with sh RUN apt-get update && apt-get install -y ca-certificates build-essential python3-dev pipx gpg libenchant-2-2 RUN pipx install poetry && pipx ensurepath COPY ./pyproject.toml /pyproject.toml COPY ./poetry.lock /poetry.lock RUN /root/.local/bin/poetry config virtualenvs.create false && /root/.local/bin/poetry install -vv --no-interaction </code>
FROM ubuntu:24.04
SHELL ["/bin/bash", "-c"] # Poetry doesn't work with sh

RUN apt-get update && 
apt-get install -y ca-certificates build-essential python3-dev pipx gpg libenchant-2-2

RUN pipx install poetry && pipx ensurepath

COPY ./pyproject.toml /pyproject.toml
COPY ./poetry.lock /poetry.lock

RUN /root/.local/bin/poetry config virtualenvs.create false && 
/root/.local/bin/poetry install -vv --no-interaction

The docker-compose file is very basic:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>services:
app:
build:
context: ../
dockerfile: docker/Dockerfile
</code>
<code>services: app: build: context: ../ dockerfile: docker/Dockerfile </code>
services:
  app:
    build:
      context: ../
      dockerfile: docker/Dockerfile

Build with: docker compose build

The is the error I get at build time:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>...
2.506 - Installing sphinxcontrib-serializinghtml (1.1.5)
2.532 - Installing wrapt (1.16.0)
2.561 - Installing yarl (1.11.1)
3.539
3.539 Stack trace:
3.540
3.540 2 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/base_env.py:338 in _run
3.540 output = subprocess.check_output(
3.540
3.540 1 /usr/lib/python3.12/subprocess.py:466 in check_output
3.540 return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
3.540
3.540 CalledProcessError
3.540
3.540 Command '['/usr/bin/python3.12', '/root/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/virtualenv/seed/wheels/embed/pip-24.2-py3-none-any.whl/pip', 'uninstall', 'packaging', '-y']' returned non-zero exit status 1.
3.540
3.540 at /usr/lib/python3.12/subprocess.py:571 in run
3.564 567# We don't call process.wait() as .__exit__ does that for us.
3.564 568│ raise
3.564 569│ retcode = process.poll()
3.564 570if check and retcode:
3.565571│ raise CalledProcessError(retcode, process.args,
3.565 572│ output=stdout, stderr=stderr)
3.565 573return CompletedProcess(process.args, retcode, stdout, stderr)
3.565 574
3.565 575
3.565
3.565 The following error occurred when trying to handle this error:
3.565
3.565
3.565 Stack trace:
3.565
3.565 9 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:285 in _execute_operation
3.565 result = self._do_execute_operation(operation)
3.565
3.565 8 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:395 in _do_execute_operation
3.566 result: int = getattr(self, f"_execute_{method}")(operation)
3.566
3.566 7 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:527 in _execute_update
3.566 status_code = self._update(operation)
3.566
3.566 6 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:586 in _update
3.566 return self._install(operation)
3.566
3.566 5 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:576 in _install
3.566 self._remove(operation.initial_package)
3.566
3.566 4 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:596 in _remove
3.566 return self.run_pip("uninstall", package.name, "-y")
3.566
3.566 3 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:418 in run_pip
3.567 self._env.run_pip(*args, **kwargs)
3.567
3.567 2 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/base_env.py:310 in run_pip
3.567 return self._run(cmd, **kwargs)
3.567
3.567 1 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/generic_env.py:99 in _run
3.567 return super(VirtualEnv, self)._run(cmd, **kwargs)
3.567
3.567 EnvCommandError
3.567
3.567 Command ['/usr/bin/python3.12', '/root/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/virtualenv/seed/wheels/embed/pip-24.2-py3-none-any.whl/pip', 'uninstall', 'packaging', '-y'] errored with the following return code 1
3.567
3.567 Output:
3.567 error: externally-managed-environment
3.567
3.567 × This environment is externally managed
3.567 ╰─> To install Python packages system-wide, try apt install
3.567 python3-xyz, where xyz is the package you are trying to
3.567 install.
3.567
3.567 If you wish to install a non-Debian-packaged Python package,
3.567 create a virtual environment using python3 -m venv path/to/venv.
3.567 Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
3.567 sure you have python3-full installed.
3.567
3.567 If you wish to install a non-Debian packaged Python application,
3.567 it may be easiest to use pipx install xyz, which will manage a
3.567 virtual environment for you. Make sure you have pipx installed.
3.567
3.567 See /usr/share/doc/python3.12/README.venv for more information.
3.567
3.567 note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
3.567 hint: See PEP 668 for the detailed specification.
3.567
3.567
3.567 at ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/base_env.py:342 in _run
3.572 338│ output = subprocess.check_output(
3.572 339│ cmd, stderr=stderr, env=env, text=True, **kwargs
3.572 340)
3.572 341except CalledProcessError as e:
3.572342│ raise EnvCommandError(e)
3.572 343
3.572 344return output
3.572 345
3.572 346def execute(self, bin: str, *args: str, **kwargs: Any) -> int:
3.572
3.572 Cannot install packaging.
</code>
<code>... 2.506 - Installing sphinxcontrib-serializinghtml (1.1.5) 2.532 - Installing wrapt (1.16.0) 2.561 - Installing yarl (1.11.1) 3.539 3.539 Stack trace: 3.540 3.540 2 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/base_env.py:338 in _run 3.540 output = subprocess.check_output( 3.540 3.540 1 /usr/lib/python3.12/subprocess.py:466 in check_output 3.540 return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, 3.540 3.540 CalledProcessError 3.540 3.540 Command '['/usr/bin/python3.12', '/root/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/virtualenv/seed/wheels/embed/pip-24.2-py3-none-any.whl/pip', 'uninstall', 'packaging', '-y']' returned non-zero exit status 1. 3.540 3.540 at /usr/lib/python3.12/subprocess.py:571 in run 3.564 567│ # We don't call process.wait() as .__exit__ does that for us. 3.564 568│ raise 3.564 569│ retcode = process.poll() 3.564 570│ if check and retcode: 3.565 → 571│ raise CalledProcessError(retcode, process.args, 3.565 572│ output=stdout, stderr=stderr) 3.565 573│ return CompletedProcess(process.args, retcode, stdout, stderr) 3.565 574│ 3.565 575│ 3.565 3.565 The following error occurred when trying to handle this error: 3.565 3.565 3.565 Stack trace: 3.565 3.565 9 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:285 in _execute_operation 3.565 result = self._do_execute_operation(operation) 3.565 3.565 8 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:395 in _do_execute_operation 3.566 result: int = getattr(self, f"_execute_{method}")(operation) 3.566 3.566 7 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:527 in _execute_update 3.566 status_code = self._update(operation) 3.566 3.566 6 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:586 in _update 3.566 return self._install(operation) 3.566 3.566 5 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:576 in _install 3.566 self._remove(operation.initial_package) 3.566 3.566 4 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:596 in _remove 3.566 return self.run_pip("uninstall", package.name, "-y") 3.566 3.566 3 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:418 in run_pip 3.567 self._env.run_pip(*args, **kwargs) 3.567 3.567 2 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/base_env.py:310 in run_pip 3.567 return self._run(cmd, **kwargs) 3.567 3.567 1 ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/generic_env.py:99 in _run 3.567 return super(VirtualEnv, self)._run(cmd, **kwargs) 3.567 3.567 EnvCommandError 3.567 3.567 Command ['/usr/bin/python3.12', '/root/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/virtualenv/seed/wheels/embed/pip-24.2-py3-none-any.whl/pip', 'uninstall', 'packaging', '-y'] errored with the following return code 1 3.567 3.567 Output: 3.567 error: externally-managed-environment 3.567 3.567 × This environment is externally managed 3.567 ╰─> To install Python packages system-wide, try apt install 3.567 python3-xyz, where xyz is the package you are trying to 3.567 install. 3.567 3.567 If you wish to install a non-Debian-packaged Python package, 3.567 create a virtual environment using python3 -m venv path/to/venv. 3.567 Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make 3.567 sure you have python3-full installed. 3.567 3.567 If you wish to install a non-Debian packaged Python application, 3.567 it may be easiest to use pipx install xyz, which will manage a 3.567 virtual environment for you. Make sure you have pipx installed. 3.567 3.567 See /usr/share/doc/python3.12/README.venv for more information. 3.567 3.567 note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages. 3.567 hint: See PEP 668 for the detailed specification. 3.567 3.567 3.567 at ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/base_env.py:342 in _run 3.572 338│ output = subprocess.check_output( 3.572 339│ cmd, stderr=stderr, env=env, text=True, **kwargs 3.572 340│ ) 3.572 341│ except CalledProcessError as e: 3.572 → 342│ raise EnvCommandError(e) 3.572 343│ 3.572 344│ return output 3.572 345│ 3.572 346│ def execute(self, bin: str, *args: str, **kwargs: Any) -> int: 3.572 3.572 Cannot install packaging. </code>
...
2.506   - Installing sphinxcontrib-serializinghtml (1.1.5)
2.532   - Installing wrapt (1.16.0)
2.561   - Installing yarl (1.11.1)
3.539 
3.539   Stack trace:
3.540 
3.540   2  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/base_env.py:338 in _run
3.540        output = subprocess.check_output(
3.540 
3.540   1  /usr/lib/python3.12/subprocess.py:466 in check_output
3.540        return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
3.540 
3.540   CalledProcessError
3.540 
3.540   Command '['/usr/bin/python3.12', '/root/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/virtualenv/seed/wheels/embed/pip-24.2-py3-none-any.whl/pip', 'uninstall', 'packaging', '-y']' returned non-zero exit status 1.
3.540 
3.540   at /usr/lib/python3.12/subprocess.py:571 in run
3.564        567│             # We don't call process.wait() as .__exit__ does that for us.
3.564        568│             raise
3.564        569│         retcode = process.poll()
3.564        570│         if check and retcode:
3.565     →  571│             raise CalledProcessError(retcode, process.args,
3.565        572│                                      output=stdout, stderr=stderr)
3.565        573│     return CompletedProcess(process.args, retcode, stdout, stderr)
3.565        574│ 
3.565        575│ 
3.565 
3.565 The following error occurred when trying to handle this error:
3.565 
3.565 
3.565   Stack trace:
3.565 
3.565   9  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:285 in _execute_operation
3.565        result = self._do_execute_operation(operation)
3.565 
3.565   8  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:395 in _do_execute_operation
3.566        result: int = getattr(self, f"_execute_{method}")(operation)
3.566 
3.566   7  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:527 in _execute_update
3.566        status_code = self._update(operation)
3.566 
3.566   6  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:586 in _update
3.566        return self._install(operation)
3.566 
3.566   5  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:576 in _install
3.566        self._remove(operation.initial_package)
3.566 
3.566   4  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:596 in _remove
3.566        return self.run_pip("uninstall", package.name, "-y")
3.566 
3.566   3  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/installation/executor.py:418 in run_pip
3.567        self._env.run_pip(*args, **kwargs)
3.567 
3.567   2  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/base_env.py:310 in run_pip
3.567        return self._run(cmd, **kwargs)
3.567 
3.567   1  ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/generic_env.py:99 in _run
3.567        return super(VirtualEnv, self)._run(cmd, **kwargs)
3.567 
3.567   EnvCommandError
3.567 
3.567   Command ['/usr/bin/python3.12', '/root/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/virtualenv/seed/wheels/embed/pip-24.2-py3-none-any.whl/pip', 'uninstall', 'packaging', '-y'] errored with the following return code 1
3.567   
3.567   Output:
3.567   error: externally-managed-environment
3.567   
3.567   × This environment is externally managed
3.567   ╰─> To install Python packages system-wide, try apt install
3.567       python3-xyz, where xyz is the package you are trying to
3.567       install.
3.567       
3.567       If you wish to install a non-Debian-packaged Python package,
3.567       create a virtual environment using python3 -m venv path/to/venv.
3.567       Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
3.567       sure you have python3-full installed.
3.567       
3.567       If you wish to install a non-Debian packaged Python application,
3.567       it may be easiest to use pipx install xyz, which will manage a
3.567       virtual environment for you. Make sure you have pipx installed.
3.567       
3.567       See /usr/share/doc/python3.12/README.venv for more information.
3.567   
3.567   note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
3.567   hint: See PEP 668 for the detailed specification.
3.567   
3.567 
3.567   at ~/.local/share/pipx/venvs/poetry/lib/python3.12/site-packages/poetry/utils/env/base_env.py:342 in _run
3.572       338│                 output = subprocess.check_output(
3.572       339│                     cmd, stderr=stderr, env=env, text=True, **kwargs
3.572       340│                 )
3.572       341│         except CalledProcessError as e:
3.572     → 342│             raise EnvCommandError(e)
3.572       343│ 
3.572       344│         return output
3.572       345│ 
3.572       346│     def execute(self, bin: str, *args: str, **kwargs: Any) -> int:
3.572 
3.572 Cannot install packaging.

What is interesting about this, is if I start a blank Ubuntu container with docker run -it --rm ubuntu:22.04 and copy and paste all the commands in the Dockerfile into the shell, everything works. So maybe this is some Docker issue?

From the error I guess that poetry is using pip or pipx behind the scenes and there is no way that I can find to pass the --break-system-packages option.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật