Skip to content

Instantly share code, notes, and snippets.

Created August 2, 2023 20:35
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Update outdated python packages in docker container and write out new requirements.txt
#!/usr/bin/env bash
#set -euo pipefail
# no set -e has diff results in non-zero
echo "Checking Inside of Container For Outdated Pip"
hostdatetime=$(date +"%d-%m-%y-%H-%M-%S")
docker exec -i yourcontainer /bin/sh -c 'python3 -m pip list --outdated'
echo "Press y to Try Upgrading Inside of Container (any other to skip)"
read -r -p "Upgrade? [y/N] " response
if [[ "$response" =~ ^([yY])$ ]]; then
echo "will upgrade"
docker exec --env hdt=$hostdatetime -i yourcontainer /bin/sh -c 'python3 -m pip list --outdated |grep -v "^\-e" |cut -d = -f 1 | xargs -n1 python3 -m pip install --upgrade --use-deprecated=legacy-resolver ; wait ; python3 -m pip freeze > requirements-$hdt.txt'
# copy out from container is unnecessary in dev
# as we are mounted so the change shows locally.
echo "this will take a few minutes, when done you should see requirements-date/time appear"
while [[ ! -f ./requirements-${hostdatetime}.txt ]]; do sleep 1; done
echo "here is your diff"
curdiff=$(diff ./requirements.txt ./requirements-${hostdatetime}.txt)
echo $curdiff
echo "if the new reqs looks good we can backup and overwrite requirements"
read -r -p "BU and Overwrite? [y/N] " buresponse
if [[ "$buresponse" =~ ^([yY])$ ]]; then
cp ./requirements.txt ./requirements-$hostdatetime.backup
mv ./requirements-$hostdatetime.txt ./requirements.txt
echo "moved over and backedup"
echo "not backed-up or moved - you can choose later..."
echo "skipping"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment