A Dockerfile itself can not immediately comprise a number of Python variations, as every Docker picture is usually primarily based on a single working system surroundings with a single model of Python put in. Nevertheless, you possibly can create a Docker picture that features a number of Python variations by manually putting in them and configuring your surroundings accordingly.
Listed here are a couple of approaches to realize this:
Some base pictures, like jessestuart/multi-python
, are particularly designed to incorporate a number of Python variations. You should utilize such a picture as your base after which configure your Dockerfile to make use of the specified Python model.
1FROM jessestuart/multi-python
2
3# Set the default Python model (e.g., Python 3.8)
4RUN update-alternatives --set python /usr/bin/python3.8
5
6# Set up dependencies
7RUN pip set up --upgrade pip
8RUN pip set up some-package
You possibly can manually set up a number of Python variations in a single Docker picture. This strategy requires extra setup and configuration.
1FROM ubuntu:20.04
2
3# Set up dependencies for constructing Python
4RUN apt-get replace && apt-get set up -y
5 software-properties-common
6 build-essential
7 wget
8 curl
9 libssl-dev
10 zlib1g-dev
11 libbz2-dev
12 libreadline-dev
13 libsqlite3-dev
14 llvm
15 libncurses5-dev
16 libncursesw5-dev
17 xz-utils
18 tk-dev
19 libffi-dev
20 liblzma-dev
21 python3-openssl
22 git
23
24# Set up Python 3.8
25RUN add-apt-repository ppa:deadsnakes/ppa &&
26 apt-get replace &&
27 apt-get set up -y python3.8 python3.8-dev python3.8-venv
28
29# Set up Python 3.9
30RUN apt-get set up -y python3.9 python3.9-dev python3.9-venv
31
32# Set default Python model (e.g., Python 3.8)
33RUN update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
34
35# Set up pip for each variations
36RUN curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py &&
37 python3.8 get-pip.py &&
38 python3.9 get-pip.py
39
40# Clear up
41RUN rm get-pip.py && apt-get clear
42
43# Instance: Set up a package deal with Python 3.8
44RUN python3.8 -m pip set up some-package
45
46# Instance: Set up a package deal with Python 3.9
47RUN python3.9 -m pip set up another-package
You should utilize pyenv
to handle a number of Python variations inside a Docker container. This strategy is extra versatile however requires further setup.
1FROM ubuntu:20.04
2
3# Set up dependencies
4RUN apt-get replace && apt-get set up -y
5 curl
6 git
7 build-essential
8 libssl-dev
9 zlib1g-dev
10 libbz2-dev
11 libreadline-dev
12 libsqlite3-dev
13 wget
14 llvm
15 libncurses5-dev
16 libncursesw5-dev
17 xz-utils
18 tk-dev
19 libffi-dev
20 liblzma-dev
21 python3-openssl
22
23# Set up pyenv
24RUN curl https://pyenv.run | bash
25
26# Set surroundings variables for pyenv
27ENV PATH="/root/.pyenv/bin:/root/.pyenv/shims:${PATH}"
28RUN echo 'eval "$(pyenv init --path)"' >> ~/.bashrc
29
30# Set up Python variations
31RUN pyenv set up 3.8.10
32RUN pyenv set up 3.9.5
33
34# Set world Python model
35RUN pyenv world 3.8.10
36
37# Set up packages
38RUN pip set up some-package
Whereas a Dockerfile itself doesn’t inherently help a number of Python variations, you possibly can configure a Docker picture to incorporate and handle a number of variations utilizing the strategies described above. Every strategy has its personal trade-offs by way of complexity and suppleness, so select the one that most closely fits your wants.