前言

很多小伙伴可能在考虑 jenkins 拉取了 github 上的代码后,发现还越少 python3 环境,那能怎么办呢?

咨询了一位运维朋友给我的答案是,将 python3 挂载到容器工作目录上,或者直接复制。

一、浅谈容器内安装 python3 方案

1、将 python3 挂载到容器工作目录上;

2、直接复制 python3 目录到工作目录上;

3、jenkins 容器内安装 python3;

结果:1 、2均均阵亡,为何?明显系统不一样哈,虽然能在命令行中输入 python3 但是 pip3用不了的,执行脚本更是乱七八糟的报错。(所以不推荐)

最后只能看 3 的了。然后在试验的过程中,发现第3种又分为两种,

① weget 安装(不推荐,基本和现安装的方法差不多,先要安装一些依赖包,如 gcc 等)

② apk add 安装(此版本是alpine :apk add,能够快速安装完成)

最后注意系统版本问题:

如果是 centos 那即是 yum
如果是 ubuntu 那即是 apt-get
如果是 alpine 那即是 apk

二、apk add 安装

1、进入 jenkins 容器 bash:

[root@iZbp19lugf22zbsubsf1y6Z ~]# docker ps
CONTAINER ID        IMAGE                 COMMAND                  CREATED             STATUS                  PORTS                                              NAMES
fa81928c4dce        jenkinsci/blueocean   "/sbin/tini -- /usr/…"   40 minutes ago      Up 34 minutes           0.0.0.0:50000->50000/tcp, 0.0.0.0:3006->8080/tcp   jenkins
[root@iZbp19lugf22zbsubsf1y6Z ~]# docker exec -itu root jenkins /bin/bash
bash-4.4#

2、首先更改 apk 配置源,不然下载速度太慢了。配置文件路径:/etc/apk/repositories

bash-4.4# cd /etc/apk/
bash-4.4# echo "https://mirrors.ustc.edu.cn/alpine/v3.6/main/" > repositories
bash-4.4# echo "https://mirrors.ustc.edu.cn/alpine/v3.6/community/" >> repositories
bash-4.4#

3、更新 apk 源:apk update

bash-4.4# apk update
fetch https://mirrors.ustc.edu.cn/alpine/v3.6/main/x86_64/APKINDEX.tar.gz
fetch https://mirrors.ustc.edu.cn/alpine/v3.6/community/x86_64/APKINDEX.tar.gz
v3.6.5-44-gda55e27396 [https://mirrors.ustc.edu.cn/alpine/v3.6/main/]
v3.6.5-34-gf0ba0b43d5 [https://mirrors.ustc.edu.cn/alpine/v3.6/community/]
OK: 8533 distinct packages available

4、直接输入:apk add python3

现在 python 版本为:3.6.8

bash-4.4# apk add python3
(1/7) Downgrading readline (7.0.003-r1 -> 6.3.008-r5)
(2/7) Downgrading bash (4.4.19-r1 -> 4.3.48-r1)
Executing bash-4.3.48-r1.post-upgrade
(3/7) Installing libressl2.5-libcrypto (2.5.5-r2)
(4/7) Installing gdbm (1.12-r0)
(5/7) Installing xz-libs (5.2.3-r0)
(6/7) Installing libressl2.5-libssl (2.5.5-r2)
(7/7) Installing python3 (3.6.8-r0)
Executing busybox-1.29.3-r10.trigger
OK: 466 MiB in 96 packages

三、验证 python3

1、查看版本:python3 -V

bash-4.4# python3 -V
Python 3.6.8

2、查看 pip3 版本:pip3 -V

bash-4.4# pip3 -V
pip 18.1 from /usr/lib/python3.6/site-packages/pip (python 3.6)

3、更新pip3:pip3 install --upgrade pip

bash-4.4# pip3 install --upgrade pip
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)",)': /simple/pip/
Collecting pip
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x7f54e86969e8>: Failed to establish a new connection: [Errno -3] Try again',)': /packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)",)': /packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)",)': /packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl
  Downloading https://files.pythonhosted.org/packages/00/b6/9cfa56b4081ad13874b0c6f96af8ce16cfbc1cb06bedf8e9164ce5551ec1/pip-19.3.1-py2.py3-none-any.whl (1.4MB)
    100% |████████████████████████████████| 1.4MB 93kB/s 
Installing collected packages: pip
  Found existing installation: pip 18.1
    Uninstalling pip-18.1:
      Successfully uninstalled pip-18.1
Successfully installed pip-19.3.1

4、生成 requirements.txt:

  • ①生成方法:pip freeze > requirements.txt
  • ②使用方法:pip install -r requirements.txt

5、我是直接粘贴上去:vi requirements.txt

保存退出:

Docker学习7-jenkins容器内安装python3

 6、执行:pip3 install -r requirements.txt

bash-4.4# pip3 install -r requirements.txt
Collecting ddt==1.2.2
  Downloading https://files.pythonhosted.org/packages/85/f3/44aea9a98e15e01d276618955dd78229dbc1500ec64146cf215022b84615/ddt-1.2.2-py2.py3-none-any.whl
Collecting PyMySQL==0.9.3
  Downloading https://files.pythonhosted.org/packages/ed/39/15045ae46f2a123019aa968dfcba0396c161c20f855f11dea6796bcaae95/PyMySQL-0.9.3-py2.py3-none-any.whl (47kB)
     |████████████████████████████████| 51kB 163kB/s 
Collecting requests==2.22.0
  Downloading https://files.pythonhosted.org/packages/51/bd/23c926cd341ea6b7dd0b2a00aba99ae0f828be89d72b2190f27c11d4b7fb/requests-2.22.0-py2.py3-none-any.whl (57kB)
     |████████████████████████████████| 61kB 17kB/s 
Collecting urllib3==1.25.7
  Downloading https://files.pythonhosted.org/packages/b4/40/a9837291310ee1ccc242ceb6ebfd9eb21539649f193a7c8c86ba15b98539/urllib3-1.25.7-py2.py3-none-any.whl (125kB)
     |████████████████████████████████| 133kB 19kB/s 
Collecting xlrd==1.2.0
  Downloading https://files.pythonhosted.org/packages/b0/16/63576a1a001752e34bf8ea62e367997530dc553b689356b9879339cf45a4/xlrd-1.2.0-py2.py3-none-any.whl (103kB)
     |████████████████████████████████| 112kB 8.1kB/s 
Collecting xlutils==2.0.0
  Downloading https://files.pythonhosted.org/packages/c7/55/e22ac73dbb316cabb5db28bef6c87044a95914f713a6e81b593f8a0d2f79/xlutils-2.0.0-py2.py3-none-any.whl (55kB)
     |████████████████████████████████| 61kB 15kB/s 
Collecting xlwt==1.3.0
  Downloading https://files.pythonhosted.org/packages/44/48/def306413b25c3d01753603b1a222a011b8621aed27cd7f89cbc27e6b0f4/xlwt-1.3.0-py2.py3-none-any.whl (99kB)
     |████████████████████████████████| 102kB 7.2kB/s 
Collecting chardet<3.1.0,>=3.0.2
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
     |████████████████████████████████| 143kB 4.9kB/s 
Collecting idna<2.9,>=2.5
  Downloading https://files.pythonhosted.org/packages/14/2c/cd551d81dbe15200be1cf41cd03869a46fe7226e7450af7a6545bfc474c9/idna-2.8-py2.py3-none-any.whl (58kB)
     |████████████████████████████████| 61kB 13kB/s 
Collecting certifi>=2017.4.17
  Downloading https://files.pythonhosted.org/packages/b9/63/df50cac98ea0d5b006c55a399c3bf1db9da7b5a24de7890bc9cfd5dd9e99/certifi-2019.11.28-py2.py3-none-any.whl (156kB)
     |████████████████████████████████| 163kB 13kB/s 
Installing collected packages: ddt, PyMySQL, chardet, idna, certifi, urllib3, requests, xlrd, xlwt, xlutils
Successfully installed PyMySQL-0.9.3 certifi-2019.11.28 chardet-3.0.4 ddt-1.2.2 idna-2.8 requests-2.22.0 urllib3-1.25.7 xlrd-1.2.0 xlutils-2.0.0 xlwt-1.3.0

最后总结:docker 容器内如果安装其它环境会比较复杂点,如遇到 gcc 等依赖包,突然给你报个错,不知所措了。

输入pip,如果提示没有找到命令,那么可以输入:python3 -m ensurepip --default-pip

python3 -m ensurepip --default-pip

不像我们在阿里云的服务器一样,给你安装好那么多工具,小龙遇到过:C编译问题、apt-get 安装问题、安装完没有pip3等等。用apt-get也成功过,但是还是比较推荐上述这种一步到位!

欢迎来QQ交流群:482713805