Navigating Common Docker and Python Environment Errors

Muhammad Ilyas
4 min readMay 25, 2024

--

Navigating Common Docker and Python Environment Errors

Encountering Docker and Python environment errors can be a significant hurdle for developers aiming to maintain a smooth workflow. This blog provides essential solutions for overcoming such technical challenges.

Moreover, it highlights how GYB Commerce, with our expertise in CTO-as-a-service, helps startups scale efficiently. If you’re exploring how to find a website developer or need custom software development services, our insights will guide you through resolving issues and choosing the right technical partner.

1. Dealing with Docker API Compatibility Issues

Error Description: When interfacing with Docker, developers may encounter a TypeError related to the requests and urllib3 libraries, indicative of a compatibility issue that impedes successful API communications. This typically manifests as:

Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/docker/api/client.py", line 214, in _retrieve_server_version
return self.version(api_version=False)["ApiVersion"]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/docker/api/daemon.py", line 181, in version
return self._result(self._get(url), json=True)
^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/docker/utils/decorators.py", line 46, in inner
return f(self, *args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/docker/api/client.py", line 237, in _get
return self.get(url, **self._set_request_timeout(kwargs))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 602, in get
return self.request("GET", url, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 589, in request
resp = self.send(prep, **send_kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/requests/sessions.py", line 703, in send
r = adapter.send(request, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/requests/adapters.py", line 486, in send
resp = conn.urlopen(
^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 791, in urlopen
response = self._make_request(
^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 497, in _make_request
conn.request(
TypeError: HTTPConnection.request() got an unexpected keyword argument 'chunked'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/usr/bin/docker-compose", line 33, in <module>
sys.exit(load_entry_point('docker-compose==1.29.2', 'console_scripts', 'docker-compose')())
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/compose/cli/main.py", line 81, in main
command_func()
File "/usr/lib/python3/dist-packages/compose/cli/main.py", line 200, in perform_command
project = project_from_options('.', options)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/compose/cli/command.py", line 60, in project_from_options
return get_project(
^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/compose/cli/command.py", line 152, in get_project
client = get_client(
^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/compose/cli/docker_client.py", line 41, in get_client
client = docker_client(
^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/compose/cli/docker_client.py", line 170, in docker_client
client = APIClient(use_ssh_client=not use_paramiko_ssh, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/docker/api/client.py", line 197, in __init__
self._version = self._retrieve_server_version()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/lib/python3/dist-packages/docker/api/client.py", line 221, in _retrieve_server_version
raise DockerException(
docker.errors.DockerException: Error while fetching server API version: HTTPConnection.request() got an unexpected keyword argument 'chunked'

Solution:

  • Update Docker and Docker Compose: To mitigate such compatibility issues, keeping Docker and Docker Compose updated is crucial. You can achieve this with the following commands:
sudo apt-get update
sudo apt-get install docker-ce docker-ce-cli containerd.io
sudo curl -L "https://github.com/docker/compose/releases/download/1.29.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose
  • Manage Dependency Versions: Regular checks and adjustments of requests and urllib3 versions can also prevent such issues:
pip show requests urllib3
pip install --upgrade requests urllib3

2. Handling Python Package Management in Externally-Managed Environments

Error Description: Python environments that are externally managed often restrict global package installations, leading to errors such as:

ubuntu@ip:~$ pip install --upgrade requests urllib3
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
python3-xyz, where xyz is the package you are trying to
install.

If you wish to install a non-Debian-packaged Python package,
create a virtual environment using python3 -m venv path/to/venv.
Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
sure you have python3-full installed.

If you wish to install a non-Debian packaged Python application,
it may be easiest to use pipx install xyz, which will manage a
virtual environment for you. Make sure you have pipx installed.

See /usr/share/doc/python3.12/README.venv for more information.

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.
hint: See PEP 668 for the detailed specification.

This complicates efforts to install or upgrade packages globally.

Solution:

  • Use APT for System Packages: Leverage APT to install Python packages available in your system’s repositories:
sudo apt update && sudo apt install python3-requests python3-urllib3
  • Create a Virtual Environment: For more controlled package management, setting up a virtual environment is advisable:
python3 -m venv path/to/venv 
source path/to/venv/bin/activate
pip install package_name

Conclusion:

Navigating Docker and Python errors effectively is crucial for maintaining project timelines and ensuring developmental efficiency.

About the Author:

I’m Muhammad Ilyas, CTO and Co-Founder of GYB Commerce, with a rich history at Elastica (acquired by Symantec), AeroGlobe.pk, Voxlabs.io, and Zapdas Technologies. At GYB Commerce, we partner with startups to revolutionise their business operations. Connect with Us

Looking to solve complex technical challenges or scale your startup? Contact us today to discover how our expert services can transform these challenges into opportunities for growth.

--

--