jenkins容器内安装python3

前言

很多小伙伴可能在考虑 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

 

 

二、Debian系统使用 apt-get 命令安装

1、进入 jenkins 容器 命令:docker exec -itu root 容器ID或者名称

 

 2、查看容器内系统版本:cat /etc/issue

 

 3、更新apt-get : apt-get update

 

 4、创建python3安装目录:mkdir python3

 

5、下载python3.8.6版本的压缩包:wget https://www.python.org/ftp/python/3.8.6/Python-3.8.6.tgz

 

 6、解压:tar -xvf Python-3.6.8.tgz

 

 7、执行 . /configure --prefix=/var/jenkins_home/python3 --with-ssl  报错缺少依赖包

 

 

8、安装依赖包:

apt-get install build-essential -y

apt-get install libncurses5-dev libncursesw5-dev libreadline6-dev -y

apt-get install libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev -y

apt-get install libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev -y

apt-get install libssl-dev openssl -y

 

 9、make编译安装 在/var/jenkins_home/python3/Python-3.8.6目录执行make和make install 安装

   a、./configure --prefix=/var/jenkins_home/python3 --with-ssl make make install

  b、make

  c、make install

10、添加python3软连接:

  ln -s /var/jenkins_home/python3/bin/python3.8  /usr/bin/python3

  ln -s /var/jenkins_home/python3/bin/pip3 /usr/bin/pip3

11、检查环境

输入pip3 和python3检查环境

 

 

 

 

三、alpine 系统使用 apk命令安装

1、进入 jenkins 容器 命令:docker exec -itu root 容器ID或者名称

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

保存退出:

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

 

 五、kernel系统安装

 

posted @ 2020-03-16 21:19  末年926  阅读(3269)  评论(1编辑  收藏  举报