Using poetry within Docker container without venv

George Shuklin
OpsOps
Published in
1 min readApr 3, 2022

We recently switched from pip to poetry, mostly because of poetry.lock, absence of which is saddening miss for pip.

Poetry is a bit more involved than I expected, and one of issues I found is that you can’t just say ‘poetry install’ in your docker file and have it working.

The reason is that poetry by default uses venv-style installation. Moreover, it manages venv by itself. We used pip to install system things (not dependencies), like ansible and testinfra, so having venv in Docker was like ‘why?’.

After some search and trying I found a way to disable venvs in poetry.

poetry has config, and there is a value there virtualenvs.create, which can be set to false. Doing so causing poetry to do system install (site-packages).

Final line for Dockerfile is looks like this:

RUN /bin/true\
&& /usr/bin/poetry config virtualenvs.create false \
&& poetry install --no-interaction \
&& rm -rf /root/.cache/pypoetry

Caveat

I found that poetry using dist-packages, not site-packages. For Mitogen that meant switching from

/usr/local/lib/python3.9/site-packages/ansible_mitogen/plugins/strategy

to

/usr/local/lib/python3.9/dist-packages/ansible_mitogen/plugins/strategy

--

--

George Shuklin
OpsOps

I work at Servers.com, most of my stories are about Ansible, Ceph, Python, Openstack and Linux. My hobby is Rust.