1. Introduction
To install Python in Alpine Linux, we can build an Alpine Linux image that comes with a Python installation. Alternatively, we can install Python on a running container.
In this tutorial, we’ll discuss both these approaches to the installation of Python in Alpine Linux.
2. Building a Python Alpine Image
To build a Python Alpine Linux image, we’ll be using a Dockerfile. We’ll start with an Alpine Linux base image:
$ cat Dockerfile
FROM alpine:latest
After that, we’ll add the command that installs python3 and pip:
$ cat Dockerfile
FROM alpine:latest
RUN apk add --no-cache python3 py3-pip
We specified apk add with –no-cache to ensure apk does not cache the index. This way, we’re more likely to keep the image lightweight.
With the RUN instruction, we’re installing not only python3 but also pip. pip is perhaps the easiest way to install Python packages, so it may come in handy when using a container built from the ensuing image.
If we won’t be installing Python packages, we may install only the python3 executable.
2.1. Building the Image
As it is, our Dockerfile is ready for use. So, we’ll build an image from it using docker build:
$ docker build . -t python-alpine-linux
After that, we’ll create and run a container with docker run using our python-alpine-linux image:
$ docker run --name baeldung -dit python-alpine-linux
Adding the -d, -i, and -t options ensures that the created container is detached and comes with an interactive terminal.
2.2. Testing the Installation
Now that we’ve created a container from our image, let’s confirm the Python installation works. To do this, we’ll execute a python3 command in the container using docker exec:
$ docker exec baeldung python3 --version
Python 3.11.8
We ran a command to check for the Python version on the baeldung container. Since the Python installation was a success, we got the expected output.
Let’s try out pip as well:
$ docker exec baeldung pip --version
pip 23.3.1 from /usr/lib/python3.11/site-packages/pip (python 3.11)
The output confirms that pip is working.
We can add a command to the RUN instruction that creates a symlink from /usr/bin/python3 to /usr/bin/python:
$ cat Dockerfile
FROM alpine:latest
RUN apk add --no-cache python3 py3-pip \
&& ln -sf python3 /usr/bin/python
After that, we can run python3 as python.
3. Installing Python on an Alpine Container
We can install Python on an Alpine Linux container using the same command we passed to the RUN instruction in the Dockerfile. Let’s illustrate this by creating a container from alpine:latest, an official Alpine Docker image:
$ docker run --name baeldung2 -dit alpine
Before trying to install python3 on the baeldung2 container, let’s check if python3 is already installed:
$ docker exec baeldung2 python3 --version
OCI runtime exec failed: exec failed: unable to start container process: exec: "python3": executable file not found in $PATH: unknown
Running a python3 command in the container threw the error message above. The error message confirms that there’s no python3 installation in the container.
To fix this, let’s install python3 on this container using docker exec:
$ docker exec baeldung2 apk add --no-cache python3 py3-pip
Once again, let’s verify the Python installation:
$ docker exec baeldung2 python3 --version
Python 3.11.8
The steps above are applicable when the container is already running. But, if we want the Python installation in the container from the start, we can run the apk add command alongside docker run:
$ docker run --name baeldung3 -dit alpine apk add --no-cache python3 py3-pip
Once more, we’ll verify the installation:
$ docker exec baeldung3 python3 --version
Python 3.11.8
4. Conclusion
In this article, we discussed how to build an Alpine Linux image with Python installation. We also talked about how to install Python on a running Alpine Linux container. Then, we capped it off by highlighting how to install Python while creating an Alpine container.