Refreshing settings in a Docker immutable image with Python and Flask
This is a trick to restart a Service that is running on a immutable Docker, with some change, and you need to refresh the values very quickly without having to roll the CI/CD Jenkins Pipeline and uploading a new image.
So why would you need to do that?.
I can think about possible scenarios like:
- Need to roll out an urgent fix in a time critical manner
- Jenkins is broken
- Somebody screw it on the git master branch
- Docker Hub is down
- GitHub is down
- Your artifactory is down
- The lines between your jumpbox or workstation and the secure Server are down and you have really few bandwidth
- You have to fix something critical and you only have a phone with you and SSH only
- Maybe the Dockerfile had latest, and the latest image has changed
FROM os:latest
The ideal is that if you work with immutable images, you roll out a new immutable image and that’s it.
But if for whatever reason you need to update this super fast, this trick may become really handy.
Let’s go for it!.
Normally you’ll start your container with a command similar to this:
docker run -d --rm -p 5000:5000 api_carlesmateo_com:v7 prod
The first thing we have to do is to stop the container.
So:
docker ps
Locate your container across the list of running containers and stop it, and then restart without the –rm:
docker stop container_name
docker run -d -p 5000:5000 api_carlesmateo_com:v7 prod
the –rm makes the container to cleanup. By default a container’s file system persists even after the container exits. So don’t start it with –rm.
Ok, so login to the container:
docker exec -it container_name /bin/sh
Edit the config you require to change, for example config.yml
If what you have to update is a password, and is encoded in base64, encode it:
echo -n "ThePassword" | base64 VGhlUGFzc3dvcmQ=
Stop the container. You can do it by stopping the container with docker stop or from inside the container, killing the listening process, probably a Python Flask.
If your Dockerfile ends with something like:
ENTRYPOINT ["./webservice.py"]
And webservice.py has Python Flask code similar to this:
#!/usr/bin/python3 # # webservice.py # # Author: Carles Mateo # Creation Date: 2020-05-10 20:50 GMT+1 # Description: A simple Flask Web Application # Part of the samples of https://leanpub.com/pythoncombatguide # More source code for the book at https://gitlab.com/carles.mateo/python_combat_guide # from flask import Flask, request import logging # Initialize Flask app = Flask(__name__) # Sample route so http://127.0.0.1/carles @app.route('/carles', methods=['GET']) def carles(): logging.critical("A connection was established") return "200" logging.info("Initialized...") if __name__ == "__main__": app.run(host='0.0.0.0', port=5000, debug=True)
Then you can kill the process, and so ending the container, from inside the container by doing:
ps -ax | grep webservice 5750 root 56:31 {webservice.py} /usr/bin/python /opt/webservice/webservice.py kill -9 5790
This will finish the container the same way as docker stop container_name.
Then start the container (not run)
docker start container_name
You can now test from outside or from inside the container. If from inside:
/opt/webservice # wget localhost:5000/carles Connecting to localhost:5000 (127.0.0.1:5000) carles 100% |**************************************************************************************************************| 3 0:00:00 ETA /opt/webservice # cat debug.log 2020-05-06 20:46:24,349 Initialized... 2020-05-06 20:46:24,359 * Running on http://0.0.0.0:5000/ (Press CTRL+C to quit) 2020-05-06 20:46:24,360 * Restarting with stat 2020-05-06 20:46:24,764 Initialized... 2020-05-06 20:46:24,771 * Debugger is active! 2020-05-06 20:46:24,772 * Debugger PIN: 123-456-789 2020-05-07 13:18:43,890 127.0.0.1 - - [07/May/2020 13:18:43] "GET /carles HTTP/1.1" 200 -
if you don’t use YAML files or what you need is to change the code, all this can be avoided as when you update the Python code, Flash realizes that and reloads. See this line in the logs:
2020-05-07 13:18:40,431 * Detected change in '/opt/webservice/wwebservice.py', reloading
The webservice.py autoreloads because we init Flask with debug set to on.
You can also start a container with shell directly:
sudo docker run -it ctop /bin/bash
Rules for writing a Comment