This is my personal base container for Docker, now with Python! Maybe you'll find it helpful too...
The container will probably not be used directly, but rather as a for building
other (Docker) containers on. To do that, specify this as your base image (in
your Dockerfile
):
FROM ghcr.io/minchinweb/python
# ... and the rest
This packages Python 3.12.
You also probably want to set the UID and GID (User ID number and Group ID
number). This can be done through the environmental variables PUID
and GUID
(either the -e
option at the command line, or the environment
key in your
docker-compose.yaml file).
There is also a folders at /app
, /config
, /defaults
that are owned by the
user. The idea is to have your application use this /config
volume for all
its persist-able data, and do this by mounting this as a volume outside of your
container (either the -v
option at the command line, or the volumes
key in
your docker-compose.yaml file). /app
is a logical location for the Python
program you are trying to run.
or, What Problems is This Trying to Solve?
After solving the issues with user permissions and PID 1 with my base container, I wanted to keep those solutions in place for my Python Docker images.
The first thing I tried was to install Python 3.7 using apt
. While there is a
python3.7
package that will happily install, there is no corresponding apt
package for
pip
(which is needed to install other Python libraries).
Next I tried to compile Python from source myself. This seemed to work, but it took over an hour. This didn't seem to the most desirable solution.
Finally, I realized I could "borrow" a pre-compiled version of Python from the official image via a "multi-stage build", using the official image as my "build stage". This seems to work, the build time is much better, although a few extra tweaks were needed.
- add various tags as per label-schema.org
- the locale is set (by my base image) to Canadian English
pip
doesn't cache downloaded packages- images are tagged with the major Python version, major+minor Python version, Major+Minor+patch Python version (so "3", "3.7", and "3.7.3", as appropriate), plus the commit id.
This builds on my base image.
This also wouldn't work without the official Python image.
- additional
apt
packages may be needed to support building C extensions withpip
or to allow pre-compiled wheels to work. - if you use this as a base image, you will need to set
ENV S6_KEEP_ENV=1
in yourDockerfile
if you want your default script to have access to your environmental variables.