Official Python image with lxml installed for Alpine Linux + Debian Slim.
The pip installation of lxml requires g++ and the development libraries libxml2-dev + libxslt-dev. After the pip installation all of the build dependencies are removed from the image, leaving just the run-time libraries libxml2 and libxslt. The result is a lightweight image of Python and lxml.
Note: This image is not meant to be run directly since it does not include things such as a non-privileged user. It is really intended for use within builds of runtime containers for Python scripts.
The example Dockerfile for Alpine Linux converts a collection of Jupyter notebooks to regular Python scripts, building a lightweight runtime image. The employment of a multistage build ensures that the final image is kept nice and small.
# Base image versions
ARG NOTEBOOK_VERSION=latest
ARG PYTHON_VERSION=3.10
ARG ALPINE_VERSION=3.15
# Jupyter notebook image is used as the builder
FROM jupyter/base-notebook:${NOTEBOOK_VERSION} AS builder
# Copy the required project files
WORKDIR /home/jovyan/work/demo
COPY --chown=jovyan:users python/*.*py* ./python/
# Convert Jupyter notebooks to regular Python scripts
RUN jupyter nbconvert --to python python/*.ipynb && \
rm python/*.ipynb
# Ensure project file permissions are correct
RUN chmod 755 python/*.py
# Create final image from Python 3 + lxml on Alpine Linux
FROM logiqx/python-lxml:${PYTHON_VERSION}-alpine${ALPINE_VERSION}
# Note: Jovian is a fictional native inhabitant of the planet Jupiter
ARG PY_USER=jovyan
ARG PY_GROUP=jovyan
ARG PY_UID=1000
ARG PY_GID=1000
# Create the Python user and work directory
RUN addgroup -g ${PY_GID} ${PY_GROUP} && \
adduser -u ${PY_UID} --disabled-password ${PY_USER} -G ${PY_GROUP} && \
mkdir -p /home/${PY_USER}/work && \
chown -R ${PY_USER} /home/${PY_USER}
# Install Tini
RUN apk add --no-cache tini=~0.19
# Copy project files from the builder
USER ${PY_USER}
WORKDIR /home/${PY_USER}/work/demo
COPY --from=builder --chown=jovyan:jovyan /home/jovyan/work/demo/ ./
# Wait for CMD to exit, reap zombies and perform signal forwarding
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["python"]
Note: This example was derived from the Dockerfile in my wca-db project on GitHub.
To build a custom image for a specific version of the Python or Alpine Linux use the following syntax:
docker image build --build-arg PYTHON_VERSION=3.10 . -t python-lxml:3.10-alpine3.15
You can provide overrides for the following:
- PYTHON_VERSION - default of 3.10
- ALPINE_VERSION - default of 3.15
To build a custom image for a specific version of the Python or Debian Slim use the following syntax:
docker image build --build-arg PYTHON_VERSION=3.10 . -f Dockerfile-slim -t python-lxml:3.10-slim-bullseye
You can provide overrides for the following:
- PYTHON_VERSION - default of 3.10
- DEBIAN_VERSION - default of buster
Building lxml requires >1GB memory and does not work on tiny machines such as the t3.micro on AWS. To avoid the machine grinding to a standstill the build must be run on a larger instance type such as t3.small.